-
Notifications
You must be signed in to change notification settings - Fork 77
[io] support rgb colours for ansi escape codes #2287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,38 @@ class AnsiCode { | |
| String toString() => '$name ${type._name} ($code)'; | ||
| } | ||
|
|
||
| /// An ANSI escape code for RGB colours. | ||
| /// | ||
| /// Represents a true colour (24-bit RGB) escape sequence that can be used for | ||
| /// both foreground and background colours. | ||
| /// | ||
| /// Use [rgb] to create an instance of this class. | ||
| /// | ||
| /// [See also](https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit) | ||
| class AnsiRgbCode extends AnsiCode { | ||
| /// The red value (0-255). | ||
| final int red; | ||
|
|
||
| /// The green value (0-255). | ||
| final int green; | ||
|
|
||
| /// The blue value (0-255). | ||
| final int blue; | ||
|
|
||
| /// Creates an RGB [AnsiCode] for the given [type]. | ||
| AnsiRgbCode._(this.red, this.green, this.blue, AnsiCodeType type) | ||
| : super._('rgb($red,$green,$blue)', type, -1, resetAll); | ||
|
|
||
| int get _prefix => type == AnsiCodeType.background ? 48 : 38; | ||
|
|
||
| @override | ||
| String get escape => '$_ansiEscapeLiteral[$_prefix;2;$red;$green;${blue}m'; | ||
|
|
||
| @override | ||
| String get escapeForScript => | ||
| '$_ansiEscapeForScript[$_prefix;2;$red;$green;${blue}m'; | ||
| } | ||
|
|
||
| /// Returns a [String] formatted with [codes]. | ||
| /// | ||
| /// If [forScript] is `true`, the return value is an unescaped literal. The | ||
|
|
@@ -127,6 +159,9 @@ String? wrapWith(String? value, Iterable<AnsiCode> codes, | |
| return value; | ||
| } | ||
|
|
||
| final numericCodes = <int>[]; | ||
| AnsiRgbCode? ansiRgbCode; | ||
|
|
||
| var foreground = 0, background = 0; | ||
| for (var code in myCodes) { | ||
| switch (code.type) { | ||
|
|
@@ -149,15 +184,62 @@ String? wrapWith(String? value, Iterable<AnsiCode> codes, | |
| // Ignore. | ||
| break; | ||
| } | ||
|
|
||
| if (code is AnsiRgbCode) { | ||
| ansiRgbCode = code; | ||
| } else if (code.code != -1) { | ||
| numericCodes.add(code.code); | ||
|
Comment on lines
+188
to
+191
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The if (code is AnsiRgbCode) {
if (code.type == AnsiCodeType.foreground) {
foreground++;
} else if (code.type == AnsiCodeType.background) {
background++;
}
ansiRgbCode = code;
} else if (code.code != -1) {
numericCodes.add(code.code);
} |
||
| } | ||
| } | ||
|
|
||
| numericCodes.sort(); | ||
| final codeParts = numericCodes.map((c) => c.toString()).toList(); | ||
|
|
||
| if (ansiRgbCode != null) { | ||
| final prefix = ansiRgbCode.type == AnsiCodeType.background ? 48 : 38; | ||
| codeParts.add( | ||
| '$prefix;2;${ansiRgbCode.red};' | ||
| '${ansiRgbCode.green};${ansiRgbCode.blue}', | ||
| ); | ||
| } | ||
|
|
||
| final sortedCodes = myCodes.map((ac) => ac.code).toList()..sort(); | ||
| final escapeValue = forScript ? _ansiEscapeForScript : _ansiEscapeLiteral; | ||
|
|
||
| return "$escapeValue[${sortedCodes.join(';')}m$value" | ||
| return "$escapeValue[${codeParts.join(';')}m$value" | ||
| '${resetAll._escapeValue(forScript: forScript)}'; | ||
| } | ||
|
|
||
| /// Creates an [AnsiRgbCode] with the given RGB colour values. | ||
| /// | ||
| /// The [red], [green], and [blue] parameters must be between 0 and 255. | ||
| /// | ||
| /// By default, it creates a foreground colour. Pass [type] as | ||
| /// [AnsiCodeType.background] to create a background colour. | ||
| /// | ||
| /// Throws an [ArgumentError] if any colour value is outside the 0-255 range or | ||
| /// if [type] is neither foreground nor background. | ||
| AnsiCode rgb( | ||
| int red, | ||
| int green, | ||
| int blue, { | ||
| AnsiCodeType type = AnsiCodeType.foreground, | ||
| }) { | ||
| if (red < 0 || red > 255) { | ||
| throw ArgumentError.value(red, 'red', 'Must be between 0 and 255.'); | ||
| } | ||
| if (green < 0 || green > 255) { | ||
| throw ArgumentError.value(green, 'green', 'Must be between 0 and 255.'); | ||
| } | ||
| if (blue < 0 || blue > 255) { | ||
| throw ArgumentError.value(blue, 'blue', 'Must be between 0 and 255.'); | ||
| } | ||
| if (type != AnsiCodeType.foreground && type != AnsiCodeType.background) { | ||
| throw ArgumentError.value( | ||
| type, 'type', 'Must be either foreground or background.'); | ||
| } | ||
| return AnsiRgbCode._(red, green, blue, type); | ||
| } | ||
|
|
||
| // | ||
| // Style values | ||
| // | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_gradientfunction can produceNaNforratiowhentextcontains a single character (e.g.,lengthis 1), leading to a runtime error due to division by zero. This can be fixed by handling the single-character case explicitly to prevent this issue, improving the robustness of the function.