colorama
colorama copied to clipboard
24 bits ANSI codes are wrongly converted instead of being stripped
Hi.
Some terminals support 24-bit ANSI colors (aka "true colors") in the form ESC[ 38;2;<r>;<g>;<b>
.
This code:
import colorama
colorama.init()
print("\x1b[38;2;166;226;46m{}\x1b[0m".format("Foo"))
Result on Windows:
Result on a true colors terminal:
I guess there may be an issue within the colorama
regex in charge of parsing color codes. On Windows, this should be stripped, right?
https://github.com/tartley/colorama/blob/d7a538212c3b8d00dc0051a45f99dc9b201dd3b2/colorama/ansitowin32.py#L224-L225
Need limit this params tuple lenght to 2, just like below:
if command == 'm':
- for param in params:
+ for param in params[:2]:
Or ignore whole params which the first param not included in AnsiCodes.
@Delgan you are correct, it should be stripped.
Due to the nature of the CSI 'm' (color) command, multiple codes can be specified in the same command. This is why we have a for
loop going over all the parameters given and treating each parameter as its own code. It just so happens that the RGB you specified has number 46
which is the Back.CYAN
code so we read it as such. You can swap out the RGB command (38) with any unrecognized number and the output will be the same, and if you swap the "RGB" number 46
with any other code it will print out that code.
The only idea I have for how to fix this is to explicitly check for parameter "38" and then skip the following 3 parameters.
Thanks for the explanation @wiggin15, this is indeed what I understood while reading the code.
About the fix, as you and @SeaHOH suggested, I guess it's more explicit to skip the win32 calls if param == 38
, rather than slicing the params.
Encounter this issue today when using tqdm (https://github.com/tqdm/tqdm/issues/678). Any progress to the fix? I see that @Delgan makes a Pull Request and awaits merge. Could anyone with the merge right kindly do the favour please? It would be highly appreciated. :see_no_evil:
how can i use this with tqdm?