-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] [Console] Support "named", "ansi", "hex", and "rgb" color support to console formatter #26576
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
Conversation
0a5e358
to
94d991f
Compare
- enables namespaces colors, including: - normal: 4-bit normal colors - bright: 4-bit bright colors - mode : format "[0-9]" for 8-bit color or format "[0-9]+,[0-9]+,[0-9]+" for 24-bit RGB color - examples of supported foreground/background colors: - green - bright:blue - mode:178 - mode:20,0,100 - examples of supported format markup and resulting output: - "<bg=bright-blue;>[str]</>" -> "\033[104m[str]\033[49m"1 - "<fg=bright-green;options=bold>[str]</>" -> "\033[92;1m[str]\033[39;22m" - "<fg=mode-54;bg=mode-253>[str]</>" -> "\033[38;5;54;48;5;253m[str]\033[39;49m" - "<fg=mode-7;bg=bright:red;options=reverse>[str]</>" -> "\033[38;5;7;101m[str]\033[39;49m" - "<fg=mode-0,255,0;bg=bright-cyan>[str]</>" -> "[38;2;0;255;0;106m[test][39;49m"
94d991f
to
eb1a92c
Compare
I'm planning to put some substantial effort into re-writing (basically) this complete pull request. While I'm generally happy with how the new color functionality in this PR works from the user-perspective, I cannot say the I'm particularly stoked about the actual underlying implementation I've cobbled together. My changes were built around the existing So, I'm'a re-write it. :-) I plan to strip the I believe it would be sensible to build this class's color handling with first-class Before I go through the work to realize this plan, can any @symfony/deciders provide some general feedback as to what I'm describing? Assuming it can be implemented without BC breaks, is it something @symfony/deciders would want to merge? Do you guys have any alternative ideas you'd like to offer? Basically, I just want to make sure I'm not going to waste a ton of time on something that may not be merged. :-) I'd appreciate any input! All the best! |
@robfrawley I like this proposal a lot. Thanks for proposing it! I've been looking into other implementation to see what they do: JavaScript: https://github.com/chalk/chalk
Rust: https://github.com/ogham/rust-ansi-term You can access the extended range of 256 colours by using the Fixed colour variant: use ansi_term::Colour::Fixed;
Fixed(134).paint("A sort of light purple"); You can also access full 24-bit color by using the RGB colour variant: use ansi_term::Colour::RGB;
RGB(70, 130, 180).paint("Steel blue"); Go: https://godoc.org/github.com/gizak/termui const (
ColorDefault Attribute = iota
ColorBlack
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
// ...
func ColorRGB(r, g, b int) Attribute Some questions and comments:
|
@javiereguiluz Here are my overly verbose answers to your comments/questions ;-)
|
About 2) I wouldn't add support for hex(), hsv(), hsl(), etc. I'd only support keywords, ANSI colors and RGB colors. That's more than enough to do anything. About the "bright" colors, I don't know what to say. I don't like |
Regarding the supported 24-bit color input types, I agree that As for the "bright" syntax question, yes, "bright" variants are really just colors and not modifications; I think you are correct then, to say using the "method" syntax for bright colors doesn't make sense. Your two examples are both good; I would add Here are the currently proposed syntaxes, all of them (except the "bright" variant) define "magenta":
|
I love this proposal! 👍 for supporting hexadecimal syntax as well. As @robfrawley said, it is quite common, especially with a web design background. |
@robfrawley Any news on this one? |
About the color functions: I really like But you can always preprocess the values and make the calculation for it before passing it to the component. Can't come up for any use case of The rest of the proposal is ace, though 👌 |
@robfrawley I tried to rebase this PR on master but I'm not allowed to push on your fork. Could you please have a look at https://github.com/nicolas-grekas/symfony/tree/feature-enhance-console-colors? |
See #36802 |
This PR was merged into the 5.2-dev branch. Discussion ---------- [Console] Add support for true colors | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #26576, Fix #19844 <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | - This PR adds support for true colors in the Console component. Instead of adding many ways to add more colors than the current "default" ones, I've opted to only add true color support via Hex CSS colors. If you have something else (RGB, HSV, ...), you need to first convert it to a CSS color. I've also decided to not support the ANSI 256 colors as most terminals support true colors nowadays. If true colors are not supported by the terminal, we fall back to the "nearest" default color. `<fg=green;bg=blue>` is now equivalent to `<fg=#00ff00;bg=#00f>`. The `Color` class is usable outside of the Console framework as well: ```php $color = new Color('black', 'white'); echo $color->apply("foo"); echo "\n"; $color = new Color('red', 'yellow'); echo $color->apply("foo"); echo "\n"; $color = new Color('#000000', '#ffffff'); echo $color->apply("foo"); $color = new Color('#000', '#fff', ['underscore', 'reverse']); echo $color->apply("bar"); echo "\n"; ``` Rainbow time! ```php function rainbowColor($i) { $h = (int) ($i / 43); $f = (int) ($i - 43 * $h); $t = (int) ($f * 255 / 43); $q = 255 - $t; if ($h == 0) { return new Color('', sprintf('#FF%02x00', $t)); } elseif ($h == 1) { return new Color('', sprintf('#%02xFF00', $q)); } elseif ($h == 2) { return new Color('', sprintf('#00FF%02x', $t)); } elseif ($h == 3) { return new Color('', sprintf('#00%02xFF', $q)); } elseif ($h == 4) { return new Color('', sprintf('#%02x00FF', $t)); } elseif ($h == 5) { return new Color('', sprintf('#FF00%02x', $q)); } } for ($i = 0; $i < 128; $i++) { echo rainbowColor($i)->apply(' '); } echo "\n"; for ($i = 255; $i >= 128; $i--) { echo rainbowColor($i)->apply(' '); } echo "\n"; ```  Commits ------- d066514 [Console] Add support for true colors
This pull request adds additional color support to the
OutputFormatterStyle
class (including support for the new color syntaxes inOutputFormatter
). Basically, this adds 16,581,375 additional colors to the Symfony console component. That means it will be 16581375 times better! </s>Disclaimer
This pull request is still very much a work in progress. Frequent, breaking changes will likely be pushed. But, since this idea is still fluid, I'm open to any and all ideas, comments, and thoughts. Feel free to let me know what you think (good or bad) and help shape this important change to color handling within the Symfony console component.
Current PR Description
The implementation is being rewritten to allow for a more extensible implementation. A new description will follow shortly. For now, you can get a general (but not specifically accurate) idea of the purpose of this PR below by reading the prior PR description.
Prior PR Description
Supported Colors
<color-name>
Provides existing normal colors per 3/4 bit ANSI escape codes.
default-<color-name>
Provides a "normal" namespace alias for existing colors per 4 bit ANSI escape codes.
bright-<color-name>
Provides a "bright" namespace for new bright colors per 4 bit codes.
mode-<color-int>
Provides a "mode" namespace for new colors per 8-bit color codes.
mode-<r>,<g>,<b>
Provides a "mode" namespace for new colors per 24-bit RBG codes.
Examples
Example color names:
Example format inputs and their respective console outputs: