Skip to content

Commit a1f8f7c

Browse files
committed
fix: logic updates
1 parent 4c8c9f3 commit a1f8f7c

File tree

1 file changed

+9
-111
lines changed

1 file changed

+9
-111
lines changed

lib/src/parser.dart

Lines changed: 9 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class ColorParser implements IReader<StringTokenValue> {
3636
ColorToken? _getToken() {
3737
var token = ColorToken.empty();
3838
final char = reader.peek();
39-
// print('');
4039
// print('char: ${_debugString(char ?? '<null>')}');
41-
// print('');
4240

4341
switch (char) {
4442
case null:
@@ -97,6 +95,9 @@ class ColorParser implements IReader<StringTokenValue> {
9795
if (_checkBetween(color, 100, 107)) {
9896
token.brightBg = true;
9997
}
98+
} else {
99+
// Catch arbitrary/unhandled codes
100+
token.setStyle(color);
100101
}
101102
colors.removeAt(0);
102103
}
@@ -113,6 +114,9 @@ class ColorParser implements IReader<StringTokenValue> {
113114
}
114115

115116
ColorToken _consumeRgbToken(ColorToken token, List<String> colors) {
117+
if (colors.length < 5) {
118+
return token;
119+
}
116120
final rgb = '${colors[2]};${colors[3]};${colors[4]}';
117121
if (colors[0] == '38') {
118122
token.rgbFg = true;
@@ -125,6 +129,9 @@ class ColorParser implements IReader<StringTokenValue> {
125129
}
126130

127131
ColorToken _consumeXterm256Token(ColorToken token, List<String> colors) {
132+
if (colors.length < 3) {
133+
return token;
134+
}
128135
final color = int.parse(colors[2]);
129136
if (colors[0] == '38') {
130137
token.xterm256 = true;
@@ -144,51 +151,6 @@ class ColorParser implements IReader<StringTokenValue> {
144151
return min <= value && value <= max;
145152
}
146153

147-
// ColorToken _consumeStyleToken(ColorToken token) {
148-
// // print('Consuming style token for $token');
149-
// final color = _consumeUntil('m');
150-
// reader.read();
151-
//
152-
// if (!color.contains(';')) {
153-
// //single number color [30-37] / [40-47]
154-
// // or [90-97] / [100-107], fg / bg
155-
// // e.g. ^[40m
156-
// // or just style ? e.g. ^[1m
157-
//
158-
// // TODO: this seems to crash on ^[40m, don't understand exactly why
159-
// // doesn't seem to be the 40m, that one gets interpreted well (black bg)
160-
//
161-
// int colorValue = -1;
162-
// try {
163-
// colorValue = int.parse(color);
164-
// } on FormatException {
165-
// // ignore, then??
166-
// // print("failing color= " + color);
167-
// // TODO: it keeps logging thousands of empty "failing colors" ?
168-
// }
169-
// if (colorValue > -1) { // init safely, ignore if nothing ?
170-
// if ((30 <= colorValue) && (colorValue <= 37) ||
171-
// (90 <= colorValue) && (colorValue <= 97)) {
172-
// token.fgColor = colorValue;
173-
// } else if ((40 <= colorValue) && (colorValue <= 47) ||
174-
// (100 <= colorValue) && (colorValue <= 107)) {
175-
// token.bgColor = colorValue;
176-
// } else if (colorValue < 30) { // style ?
177-
// token.setStyle(colorValue);
178-
// }
179-
// }
180-
// }
181-
// // things like ^[1;38;2;114;150;50;48;2;125;70;22m TEXT ^[0m
182-
// else { // multi number madness, trying recursive parser ?
183-
// final colors = color.split(';');
184-
// processTokenStyle(colors, token); //really hope this works by reference
185-
// }
186-
// if (reader.peek() == Consts.esc) {
187-
// return _consumeEscSequence(token);
188-
// }
189-
// return token;
190-
// }
191-
192154
ColorToken _consumeText(ColorToken token) {
193155
// print('Consuming text for $token');
194156
token.text += _consumeUntil(Consts.esc);
@@ -238,70 +200,6 @@ class ColorParser implements IReader<StringTokenValue> {
238200
return result;
239201
}
240202

241-
// processTokenStyle(List<String> colors, ColorToken token) {
242-
// if (colors.isNotEmpty) {
243-
// //if it's already empty, do nothing more
244-
// int first = int.parse(colors[0]);
245-
// if (first < 30) {
246-
// // bold, underline, etc?
247-
// token.setStyle(first);
248-
// colors.removeAt(0);
249-
// } else if ((30 <= first) && (first <= 37) ||
250-
// (90 <= first) && (first <= 97) ||
251-
// (40 <= first) && (first <= 47) ||
252-
// (100 <= first) && (first <= 107)) {
253-
// if ((30 <= first) && (first <= 37) || (90 <= first) && (first <= 97)) {
254-
// token.fgColor = first;
255-
// colors.removeAt(0);
256-
// } else if ((40 <= first) && (first <= 47) ||
257-
// (100 <= first) && (first <= 107)) {
258-
// token.bgColor = first;
259-
// colors.removeAt(0);
260-
// }
261-
// } else {
262-
// int second = int.parse(colors[1]);
263-
// if (first == 38 && second == 5) {
264-
// token.xterm256 = true;
265-
// int third = int.parse(colors[2]);
266-
// token.fgColor = third;
267-
// colors.removeRange(0, 3);
268-
// // bg = 0;
269-
// } else if (first == 48 && second == 5) {
270-
// token.xterm256 = true;
271-
// int third = int.parse(colors[2]);
272-
// token.bgColor = third;
273-
// colors.removeRange(0, 3);
274-
// // bg = 0;
275-
// } else {
276-
// if (first == 38 && second == 2) {
277-
// //rgb
278-
// String red = colors[2];
279-
// String green = colors[3];
280-
// String blue = colors[4];
281-
// token.rgbFg = true;
282-
// token.rgbFgColor = "$red;$green;$blue";
283-
// colors.removeRange(0, 5);
284-
// } else if (first == 48 && second == 2) {
285-
// //rgb
286-
// String red = colors[2];
287-
// String green = colors[3];
288-
// String blue = colors[4];
289-
// token.rgbBg = true;
290-
// token.rgbBgColor = "$red;$green;$blue";
291-
// colors.removeRange(0, 5);
292-
// } else {
293-
// return;
294-
// }
295-
// }
296-
// }
297-
// //pass the rest of the color codes, hope for the best
298-
// if (colors.isNotEmpty) {
299-
// processTokenStyle(
300-
// colors, token); // really really hoping these go by reference
301-
// }
302-
// }
303-
// }
304-
305203
// ignore: unused_element
306204
_debugString(String string) => string.replaceAll('\x1B', '\\x1B');
307205

0 commit comments

Comments
 (0)