-
Notifications
You must be signed in to change notification settings - Fork 95
[io] Add support for RGB ansi codes #2323
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
d77b6b4
fd36698
3ca09d2
ad41251
6884a21
9779b77
13d6977
090d8b6
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 |
|---|---|---|
|
|
@@ -60,8 +60,17 @@ class AnsiCodeType { | |
| /// [Source](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) | ||
| class AnsiCode { | ||
| /// The numeric value associated with this code. | ||
| /// | ||
| /// `-1` if this code is a composite code with multiple integer values. | ||
| /// See [codes]. | ||
| final int code; | ||
|
|
||
| /// The numeric values associated with this code. | ||
| /// | ||
| /// A composite code may have more than one integer value, in which case the | ||
| /// [code] property will be `-1`. | ||
| Iterable<int> get codes => [code]; | ||
|
|
||
| /// The [AnsiCode] that resets this value, if one exists. | ||
| /// | ||
| /// Otherwise, `null`. | ||
|
|
@@ -76,10 +85,10 @@ class AnsiCode { | |
| const AnsiCode._(this.name, this.type, this.code, this.reset); | ||
|
|
||
| /// Represents the value escaped for use in terminal output. | ||
| String get escape => '$_ansiEscapeLiteral[${code}m'; | ||
| String get escape => '$_ansiEscapeLiteral[${codes.join(';')}m'; | ||
|
|
||
| /// Represents the value as an unescaped literal suitable for scripts. | ||
| String get escapeForScript => '$_ansiEscapeForScript[${code}m'; | ||
| String get escapeForScript => '$_ansiEscapeForScript[${codes.join(';')}m'; | ||
|
|
||
| String _escapeValue({bool forScript = false}) => | ||
| forScript ? escapeForScript : escape; | ||
|
|
@@ -104,6 +113,34 @@ class AnsiCode { | |
| String toString() => '$name ${type._name} ($code)'; | ||
| } | ||
|
|
||
| /// An ANSI escape code for RGB colours. | ||
|
Member
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. Do we need this class, or it being public. Could all class AnsiCode {
/// The numeric value associated with this code.
///
/// `-1` if this code is a composite code with multiple integer values.
/// See [codes].
final int code;
/// The numeric values associated with this code.
///
/// A composite code may have more than one integer value, in which case the
/// [code] property will be `-1`.
Iterable<int> get codes => _codes ??= [code];
AnsiCode._rgb(int red, int green, int blue, AnsiCodeType type) :
code = -1,
_codes = List.unmodifiable([
type == AnsiCodeType.background ? 48 : 38,
2,
RangeError.checkValueInInterval(red, 0, 255, 'red'),
RangeError.checkValueInInterval(green, 0, 255, 'green'),
RangeError.checkValueInInterval(blue, 0, 255, 'blue'),
]);Does |
||
| /// | ||
| /// 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 { | ||
|
Member
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. How well-supported is RGB colors in ANSI escapes these days? I can see xterm and rxvt seem to supports all colors, so does the Windows terminal.
Member
Author
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. Yes, I think it has wide enough support to be worth landing. |
||
| /// 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 | ||
| Iterable<int> get codes => [_prefix, 2, red, green, blue]; | ||
| } | ||
|
|
||
| /// Returns a [String] formatted with [codes]. | ||
| /// | ||
| /// If [forScript] is `true`, the return value is an unescaped literal. The | ||
|
|
@@ -150,14 +187,45 @@ String? wrapWith(String? value, Iterable<AnsiCode> codes, | |
| break; | ||
| } | ||
| } | ||
| final codeParts = myCodes.expand((c) => c.codes).map((c) => c.toString()); | ||
|
|
||
| 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.'); | ||
|
Member
Author
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. @lrhn - would
Member
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. It's relevant, so And even use the helper function: RangeError.checkValueInInterval(red, 0, 255, 'red');
RangeError.checkValueInInterval(green, 0, 255, 'green');
RangeError.checkValueInInterval(blue, 0, 255, 'blue');It's not important which |
||
| } | ||
| 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.
There's a potential division by zero here if
text.lengthis 1.length - 1would be 0, andi / (length - 1)would result inNaN. This leads to a runtime error whenrgb()is called withNaNvalues because they cannot be assigned toint. You should handle this edge case.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.
Or start with
if (length == 0) return '';.