sharkdp / pastel

A command-line tool to generate, analyze, convert and manipulate colors
Apache License 2.0
5k stars 97 forks source link

`pastel format` does not print ansi-24bit colors #113

Closed bbkane closed 4 years ago

bbkane commented 4 years ago

Version

$ pastel --version
pastel 0.6.1

Working on hsl

$ pastel format hsl red
hsl(0, 100.0%, 50.0%)

Not working in ansi-24bit

$ pastel format ansi-24bit red

This returns 0 and doesn't print anything. I would expect it to print an integer between 0 and 255.

Not sure if this is intentional - see https://github.com/sharkdp/pastel/issues/111 . That seems to indicate that this is outputting a control character I can't see. If so, would it be possible to specify a ansi-24bit-int argument that prints out the integer value?

bbkane commented 4 years ago

Use-case - I want to put these colors in my prompt, but I don't want a pastel dependency

bbkane commented 4 years ago

oooh zsh accepts colors in hex format... I'll just use that. I still think this might be a usability issue

sharkdp commented 4 years ago

Not sure if this is intentional - see #111 .

Yes, it is.

That seems to indicate that this is outputting a control character I can't see.

Correct. You can check this by running something like

pastel format ansi-24bit red | hexdump -C

or

pastel format ansi-24bit red | cat -A

If so, would it be possible to specify a ansi-24bit-int argument that prints out the integer value?

For ansi-8bit, that's probably a good idea! For ansi-24bit, the escape code has the format

\x1b[38;2;R;G;Bm

So it's not really a single number. I guess we could still have a special format that omits the \x1b[ escape prefix and the m suffix. What do you think?

oooh zsh accepts colors in hex format... I'll just use that. I still think this might be a usability issue

I guess you mean the \x1b[38;2;R;G;Bm format I referenced above? Note that it's not zsh but rather your terminal emulator that needs to support this. The "hex format" code is exactly what you get by calling pastel format ansi-24bit. You could, for example, use sed or tail to strip the leading characters:

> pastel format ansi-24bit salmon | tail -c +3
38;2;250;128;114m
bbkane commented 4 years ago

I got what I want (to easily use pastel generated colors in my zsh prompt - see https://stackoverflow.com/q/58615054/2958070 ).

I would still like (but to be honest don't know if I would actually use) the ability to format colors into something I can paste into my own printf call. Maybe an ansi-8bit-printf and an ansi-24bit-printf (or -pastable or -terminal or something) that would print the color prefixes needed to paste into a terminal. Something like:

$ pastel format ansi-24bit-printf salmon
\x1b[38;2;250;128;114m

Then I could paste that into a shell script. and put my text after it. I would have to remember the "reset" sequence, but life isn't perfect (or, as you suggested, it might could omit the \x1b[ escape prefix and the m suffix and the user would just have to remember both.

Now that I'm thinking more on it, it's possible to see almost everything needed with a command like:

pastel -m 24bit paint cornflowerblue TEST | less
# in less window
ESC[38;2;100;149;237mTESTESC[0m

So maybe the best thing to do is just update pastel format --help to say that the ansi- methods output control codes and if I want to read them I need to hexdump them or tail -c +3 them.

Regardless, thanks for the tool and responding on the issue!

sharkdp commented 4 years ago

I would still like (but to be honest don't know if I would actually use) the ability to format colors into something I can paste into my own printf call.

Yeah, I agree. The fact that this came up the second time (here and in #111) is probably a good sign that the current behavior is confusing/unexpected.

I opened #115 to address this.

Thank you for your feedback!

bbkane commented 4 years ago

Thank you!

bbkane commented 4 years ago

BTW, this doesn't use this change (this'll be most useful if I port the prompt to bash), but pastel has really made my prompt a lot more fun! Thank you!

zp_prompt

https://github.com/bbkane/dotfiles/tree/master/zsh

sharkdp commented 4 years ago

:heart: