sharkdp / pastel

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

How to print ansi reset code (`\e[m`)? #156

Closed thorstenkampe closed 2 years ago

thorstenkampe commented 2 years ago

The title says it all: how can I print the ANSI reset code (\e[m) with pastel format ansi-24bit-escapecode?

sharkdp commented 2 years ago

you can't. But you can use pastel paint to print colored text with a proper reset sequence.

thorstenkampe commented 2 years ago

pastel paint is fine for generating single lines of colored text. That becomes quite unreadable for longer text like help messages where you would have to split each line into the colored part with pastel paint --no-newline and the rest.

Generating the escape codes via pastel format works fine: I get all the colors I need except the "reset color". Would you consider adding it?

sharkdp commented 2 years ago

Generating the escape codes via pastel format works fine: I get all the colors I need except the "reset color". Would you consider adding it?

How do you think the command-line interface for this should look like?

thorstenkampe commented 2 years ago

Using "reset", "normal" or "nocolor" as special identifier (similar to "pick") would do the job in my opinion.

Use case:

declare -A color
for color in yellow reset; do
    color[$color]=$(pastel format ansi-24bit-escapecode $color)
done

echo -e "${color[yellow]}[WARN]${color[reset]}: a warning event occurred"

image

thorstenkampe commented 2 years ago

As a side note: ANSI defines a lot of "colors" which are rather text effects like blink, bold or underline. "Reset" in that regard is special in my opinion as you need it constantly to "turn off color" and switch back to "normal".

sharkdp commented 2 years ago

I'm not 100% convinced. It's inconsistent that pastel format ansi-24bit-escapecode reset works, but pastel format rgb reset does not. pastel color yellow works, but pastel color reset makes no sense.

What is the problem with simply setting

reset=$(printf "\x1b[0m")

or

reset=$(tput sgr0)
thorstenkampe commented 2 years ago

Of course there is no problem simply setting reset to the respective escape sequence. On the other hand, why should I use pastel to set the color yellow when I simply can do yellow='\e[33m'? What's the use case of format ansi-24bit-escapecode if not so you don't have to know all those weird escape codes?

Anyway, please feel free to close this issue.

sharkdp commented 2 years ago

I mean... pastel format ansi-24bit-escapecode yellow is not the most interesting use case.

The general idea of pastel is that you generate/compute colors in a pastel … | pastel … | … "pipeline" and then in the end convert it to a desired output format (here: an ANSI escape sequence).

I think I'm inclined to accept this feature request. But I would like us to think about different ways to achieve that goal.

By the way, I think pastel format ansi-24bit-escapecode should have a way to handle multiple colors. Right now, if you do something like

pastel gradient -n 10 red yellow | pastel format ansi-24bit-escapecode

it just outputs those 10 escape sequences one after each other without any way to split them. Because we do not print a newline for ansi-*-escapesequence. Which is useful if there is just a single color. But not if you want to work with multiple escape codes somehow (e.g. via xargs).

bend-n commented 2 years ago

ive looked at this issue, but for me, format ansi-24bit-escapecode doesnt seem to work very well: it prints absolutely nothing?

sharkdp commented 2 years ago

ive looked at this issue, but for me, format ansi-24bit-escapecode doesnt seem to work very well: it prints absolutely nothing?

Yes it does. It prints actual ansi escape sequences (which are not visible in interactive mode). Either print add some text after them. Or pipe into a hexeditor for "debugging" purposes.

sharkdp commented 2 years ago

I'm closing this due to inactivity. Please feel free to comment in case it should be re-opened.