-
Notifications
You must be signed in to change notification settings - Fork 84
[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
Draft
natebosch
wants to merge
8
commits into
main
Choose a base branch
from
rgb-ansi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−6
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d77b6b4
[io] support rgb colours for ansi escape codes
Lexedia fd36698
add tests
Lexedia 3ca09d2
Adds a changelog entry
Lexedia ad41251
remove overriden field
Lexedia 6884a21
Add a `codes` files to avoid type check
natebosch 9779b77
Merge branch 'refs/heads/main' into rgb-ansi
natebosch 13d6977
Cleanup
natebosch 090d8b6
Use _test helper to override ansi enabled
natebosch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| /// | ||
| /// 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 | ||
| 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 |
||
| } | ||
| 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 | ||
| // | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.