[PATCH v3] Add support for hex color code escapes
Export this patch
Hex color codes are easy to support and are useful for eg bridges in
order to match the nick color of the other user.
---
Fixed according to your comments :)
lib/ansi.dart | 35 +++++++++++++++++++++++++++++++----
1 file changed, 31 insertions(+), 4 deletions(-)
diff --git a/lib/ansi.dart b/lib/ansi.dart
index f3fcee8..bfb01ce 100644
--- a/lib/ansi.dart
+++ b/lib/ansi.dart
@@ -127,15 +127,26 @@ List<TextSpan> applyAnsiFormatting(String s, TextStyle base) {
bgColor = bg == 99 ? null : Color(colorHexCodes[bg]);
}
break;
+ case '\x04': // hex color
+ fgColor = _parseHexColorCode(s.substring(i + 1));
+ if (fgColor == null) {
+ bgColor = null;
+ break;
+ }
+ i += 6;
+ if (s.length > i + 1 && s[i + 1] == ',') {
+ var color = _parseHexColorCode(s.substring(i + 2));
+ if (color != null) {
+ bgColor = color;
+ i += 7;
+ }
+ }
+ break;
case '\x11': // monospace
case '\x1E': // strike-through
case '\x16': // reverse color
// ignore, rarely used
break;
- case '\x04': // hex color
- i += 6;
- // ignore, rarely used
- break;
default:
current.write(ch);
}
@@ -146,3 +157,19 @@ List<TextSpan> applyAnsiFormatting(String s, TextStyle base) {
bool _isDigit(String ch) {
return '0'.codeUnits.first <= ch.codeUnits.first && ch.codeUnits.first <= '9'.codeUnits.first;
}
+
+Color? _parseHexColorCode(String s) {
+ if (s.length < 6) {
+ return null;
+ }
+ s = s.substring(0, 6);
+ if (s[0] == '+' || s[0] == '-') {
+ // disallow color codes starting with a sign
+ return null;
+ }
+ var color = int.tryParse(s, radix: 16);
+ if (color == null) {
+ return null;
+ }
+ return Color(color | 0xFF000000);
+}
base-commit: 37f968d5b030230a37b569c50f859959dd9ca3c3
--
2.36.1
Pushed, thanks!