-
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
base: main
Are you sure you want to change the base?
Conversation
Remove an `is AnsiRgbCode` type check and allow for general `AnsiCode` values to consist of multiple codes.
Package publishing
Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation. |
PR HealthLicense Headers ✔️
All source files should start with a license header. Unrelated files missing license headers
This check can be disabled by tagging the PR with API leaks ✔️The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
This check can be disabled by tagging the PR with Breaking changes ✔️
This check can be disabled by tagging the PR with Unused Dependencies ✔️
For details on how to fix these, see dependency_validator. This check can be disabled by tagging the PR with
Coverage
|
| File | Coverage |
|---|---|
| pkgs/io/example/example.dart | 💔 Not covered |
| pkgs/io/lib/src/ansi_code.dart | 💔 82 % ⬇️ 3 % |
This check for test coverage is informational (issues shown here will not fail the PR).
This check can be disabled by tagging the PR with skip-coverage-check.
Changelog Entry ✔️
| Package | Changed Files |
|---|
Changes to files need to be accounted for in their respective changelogs.
This check can be disabled by tagging the PR with skip-changelog-check.
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.
Code Review
This pull request introduces support for 24-bit RGB colors for ANSI escape codes, which is a great enhancement. The implementation includes a new AnsiRgbCode class, a convenient rgb factory function, and updates to wrapWith to handle composite codes. The accompanying tests and example are helpful.
I've found a bug in the new example code that can lead to a runtime error, and I have a couple of suggestions for improving the code's maintainability in ansi_code.dart. Overall, this is a solid contribution.
| final length = text.length; | ||
| final buffer = StringBuffer(); | ||
| for (var i = 0; i < length; i++) { | ||
| final ratio = i / (length - 1); |
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.length is 1. length - 1 would be 0, and i / (length - 1) would result in NaN. This leads to a runtime error when rgb() is called with NaN values because they cannot be assigned to int. You should handle this edge case.
| final ratio = i / (length - 1); | |
| final ratio = length > 1 ? i / (length - 1) : 0.0; |
pkgs/io/lib/src/ansi_code.dart
Outdated
| @override | ||
| String get escape => '$_ansiEscapeLiteral[${codes.join(';')}m'; | ||
|
|
||
| @override | ||
| String get escapeForScript => '$_ansiEscapeForScript[${codes.join(';')}m'; | ||
| } |
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.
While these overrides are necessary with the current base class implementation, consider modifying the escape and escapeForScript getters in the AnsiCode base class to use codes.join(';'). This would make them work for both single and composite codes, and you could then remove these overrides, reducing code duplication and simplifying AnsiRgbCode.
Example change in AnsiCode:
// In AnsiCode
String get escape => '$_ansiEscapeLiteral[${codes.join(';')}m';
String get escapeForScript => '$_ansiEscapeForScript[${codes.join(';')}m';
pkgs/io/lib/src/ansi_code.dart
Outdated
| break; | ||
| } | ||
| } | ||
| // myCodes.sort((a, b) => a.code.compareTo(b.code)); |
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.
| AnsiCodeType type = AnsiCodeType.foreground, | ||
| }) { | ||
| if (red < 0 || red > 255) { | ||
| throw ArgumentError.value(red, 'red', 'Must be between 0 and 255.'); |
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.
@lrhn - would RangeError be preferred here?
Add an
RgbAnsiCodeclass which hold the red, green, and blue channelsas separate fields.
Add a
codesfield toAnsiCodeand update the comment for thecodefield indicating that some ansi codes with more than one integer code
will return
-1for thecodefield. All uses should go throughcodes.