Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 80 additions & 23 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class ColorParser implements IReader<StringTokenValue> {
ColorToken? _getToken() {
var token = ColorToken.empty();
final char = reader.peek();
// print('');
// print('char: ${_debugString(char ?? '<null>')}');
// print('');

switch (char) {
case null:
Expand All @@ -63,37 +61,96 @@ class ColorParser implements IReader<StringTokenValue> {

ColorToken _consumeStyleToken(ColorToken token) {
// print('Consuming style token for $token');
final color = _consumeUntil('m');
final args = _consumeUntil('m');
reader.read();
final colors = color.split(';');
final first = int.parse(colors[0]);
final second = colors.length > 1 ? int.tryParse(colors[1]) ?? 0 : 0;
final third = colors.length > 2 ? int.tryParse(colors[2]) ?? 0 : 0;
// print('first: $first, second: $second, third: $third');
int fg;
int bg;
if (first < 30) {
token.setStyle(first);
fg = second;
bg = third;
token = _parseStyleToken(token, args.split(';'));
return token;
}

ColorToken _parseStyleToken(ColorToken token, List<String> colors) {
final colorNums = colors.map((n) => int.parse(n)).toList();
// Special cases
if (_checkPair(colorNums, 38, 2) || _checkPair(colors, 48, 2)) {
// RGB format is 38;2;R;G;B or 48;2;R;G;B
token = _consumeRgbToken(token, colors);
colors.removeRange(0, 5);
} else if (_checkPair(colorNums, 38, 5) || _checkPair(colorNums, 48, 5)) {
// Xterm256 format is 38;5;N or 48;5;N
token = _consumeXterm256Token(token, colors);
colors.removeRange(0, 3);
} else {
if (first == 38 && second == 5) {
token.xterm256 = true;
fg = third;
bg = 0;
} else {
fg = first;
bg = second;
// Other style codes
for (var color in colorNums) {
if (color < 30) {
token.setStyle(color);
} else if (_checkBetween(color, 30, 37) ||
_checkBetween(color, 90, 97)) {
token.fgColor = color;
if (_checkBetween(color, 90, 97)) {
token.brightFg = true;
}
} else if (_checkBetween(color, 40, 47) ||
_checkBetween(color, 100, 107)) {
token.bgColor = color;
if (_checkBetween(color, 100, 107)) {
token.brightBg = true;
}
} else {
// Catch arbitrary/unhandled codes
token.setStyle(color);
}
colors.removeAt(0);
}
}
token.fgColor = token.hasFgColor ? token.fgColor : fg;
token.bgColor = token.hasBgColor ? token.bgColor : bg;

if (reader.peek() == Consts.esc) {
return _consumeEscSequence(token);
}

if (colors.isNotEmpty) {
token = _parseStyleToken(token, colors);
}
return token;
}

ColorToken _consumeRgbToken(ColorToken token, List<String> colors) {
if (colors.length < 5) {
return token;
}
final rgb = '${colors[2]};${colors[3]};${colors[4]}';
if (colors[0] == '38') {
token.rgbFg = true;
token.rgbFgColor = rgb;
} else if (colors[0] == '48') {
token.rgbBg = true;
token.rgbBgColor = rgb;
}
return token;
}

ColorToken _consumeXterm256Token(ColorToken token, List<String> colors) {
if (colors.length < 3) {
return token;
}
final color = int.parse(colors[2]);
if (colors[0] == '38') {
token.xterm256 = true;
token.fgColor = color;
} else if (colors[0] == '48') {
token.xterm256 = true;
token.bgColor = color;
}
return token;
}

bool _checkPair<T>(List<T> arr, T v1, T v2) {
return arr.length > 1 && arr[0] == v1 && arr[1] == v2;
}

bool _checkBetween<T extends num>(T value, T min, T max) {
return min <= value && value <= max;
}

ColorToken _consumeText(ColorToken token) {
// print('Consuming text for $token');
token.text += _consumeUntil(Consts.esc);
Expand Down
29 changes: 29 additions & 0 deletions lib/src/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class ColorToken {

/// The foreground color code.
int fgColor;
String rgbFgColor;

/// The background color code.
int bgColor;
String rgbBgColor;

/// Whether the text is bold.
bool get bold => styles.contains(TermStyle.bold);
Expand Down Expand Up @@ -54,6 +56,18 @@ class ColorToken {
/// Whether the text is an xterm256 color code. Otherwise, it is an ANSI color code.
bool xterm256;

/// Whether the text is using r;g;b for foreground
bool rgbFg;

/// Whether the text is using r;g;b for background
bool rgbBg;

/// Whether the text is using bright foreground colors
bool brightFg;

/// Whether the text is using bright background colors
bool brightBg;

/// The styles applied to the text.
late Set<TermStyle> styles;

Expand All @@ -62,6 +76,12 @@ class ColorToken {
required this.fgColor,
required this.bgColor,
this.xterm256 = false,
this.rgbFg = false,
this.rgbBg = false,
this.rgbFgColor = "",
this.rgbBgColor = "",
this.brightFg = false,
this.brightBg = false,
Set<TermStyle>? styles,
}) : styles = styles ?? {};

Expand Down Expand Up @@ -95,6 +115,13 @@ class ColorToken {
if (bgColor != 0) {
colorCodes += ';48;5;$bgColor';
}
} else if (rgbFg || rgbBg) {
if (rgbFgColor != "") {
colorCodes = '38;2;$rgbFgColor';
}
if (rgbBgColor != "") {
colorCodes += ';48;2;$rgbBgColor';
}
} else {
colorCodes = fgColor == 0 ? '' : '$fgColor';
if (bgColor != 0) {
Expand All @@ -121,6 +148,8 @@ class ColorToken {
'fgColor: $fgColor',
'bgColor: $bgColor',
'xterm256: $xterm256',
'rgbFG: $rgbFgColor',
'rgbBG: $rgbBgColor',
'styles: ${styles.map((s) => s.name)}',
];

Expand Down
Loading