Skip to content

Commit e5c0921

Browse files
committed
Improve VT100 support
1 parent 4b61791 commit e5c0921

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

llamafile/color_bleeder.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ ColorBleeder::ColorBleeder(Highlight *h) : h_(h) {
4949
ColorBleeder::~ColorBleeder() {
5050
}
5151

52+
void ColorBleeder::restore(std::string *r) {
53+
bool got_some = false;
54+
if (!intensity_ && !inverted_ && !foreground_ && !background_)
55+
return;
56+
*r += "\033[";
57+
if (intensity_) {
58+
*r += std::to_string(intensity_);
59+
got_some = true;
60+
}
61+
if (inverted_) {
62+
if (got_some)
63+
*r += ';';
64+
*r += '7';
65+
got_some = true;
66+
}
67+
if (foreground_) {
68+
if (got_some)
69+
*r += ';';
70+
*r += std::to_string(foreground_);
71+
got_some = true;
72+
}
73+
if (background_) {
74+
if (got_some)
75+
*r += ';';
76+
*r += std::to_string(background_);
77+
}
78+
*r += 'm';
79+
}
80+
5281
void ColorBleeder::relay(std::string *r, const std::string &s) {
5382
for (char c : s) {
5483
*r += c;
@@ -61,32 +90,7 @@ void ColorBleeder::relay(std::string *r, const std::string &s) {
6190
t_ = ESC;
6291
break;
6392
case '\n':
64-
if (intensity_ || inverted_ || foreground_ || background_) {
65-
bool got_some = false;
66-
*r += "\033[";
67-
if (intensity_) {
68-
*r += std::to_string(intensity_);
69-
got_some = true;
70-
}
71-
if (inverted_) {
72-
if (got_some)
73-
*r += ';';
74-
*r += '7';
75-
got_some = true;
76-
}
77-
if (foreground_) {
78-
if (got_some)
79-
*r += ';';
80-
*r += std::to_string(foreground_);
81-
got_some = true;
82-
}
83-
if (background_) {
84-
if (got_some)
85-
*r += ';';
86-
*r += std::to_string(background_);
87-
}
88-
*r += 'm';
89-
}
93+
restore(r);
9094
break;
9195
}
9296
break;
@@ -114,6 +118,7 @@ void ColorBleeder::relay(std::string *r, const std::string &s) {
114118
x_ = 0;
115119
}
116120
} else if (c == 'm') {
121+
bool vt100dirty = false;
117122
if (n_ < sizeof(sgr_codes_)) {
118123
sgr_codes_[n_++] = x_;
119124
x_ = 0;
@@ -129,22 +134,30 @@ void ColorBleeder::relay(std::string *r, const std::string &s) {
129134
intensity_ = g;
130135
} else if (g == 22) {
131136
intensity_ = 0;
137+
vt100dirty = true;
132138
} else if (g == 7) {
133139
inverted_ = 1;
134140
} else if (g == 27) {
135141
inverted_ = 0;
142+
vt100dirty = true;
136143
} else if ((30 <= g && g <= 37) || //
137144
(90 <= g && g <= 97)) {
138145
foreground_ = g;
139146
} else if (g == 39) {
140147
foreground_ = 0;
148+
vt100dirty = true;
141149
} else if ((40 <= g && g <= 47) || //
142150
(100 <= g && g <= 107)) {
143151
background_ = g;
144152
} else if (g == 49) {
145153
background_ = 0;
154+
vt100dirty = true;
146155
}
147156
}
157+
if (vt100dirty) {
158+
*r += HI_RESET;
159+
restore(r);
160+
}
148161
t_ = NORMAL;
149162
} else {
150163
t_ = NORMAL;

llamafile/highlight.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class ColorBleeder : public Highlight {
175175

176176
private:
177177
void relay(std::string *r, const std::string &s);
178+
void restore(std::string *r);
178179
unsigned char t_ = 0;
179180
unsigned char x_ = 0;
180181
unsigned char n_ = 0;

0 commit comments

Comments
 (0)