Live data from Hacker News

How to choose colors for your CLI applications (2023)

blog.xoria.org

101–103 of 103 posts

Re: How to choose colors for your CLI applications (2023)

#101
post #53

If you want a quick easy way to add some colors to your own shell scripts: export STDOUT_COLOR_START='' export STDOUT_COLOR_STOP='' export STDERR_COLOR_START='' export STDERR_COLOR_STOP='' In your shell script: print_stdout() { printf %s%s%s\\n "${STDOUT_COLOR_START:-}" "$*" "${STDOUT_COLOR_STOP:-}" } print_stderr() { >&2 printf %s%s%s\\n "${STDERR_COLOR_START:-}" "$*" "${STDERR_COLOR_STOP:-}" } Sou…

That seems needlessly cumbersome, why not declare STDOUT_COLOR='\e[34m' declare STDERR_COLOR='\e[31m' declare COLOR_STOP='\e[0m' print_stdout() { echo -e "${STDOUT_COLOR}${*}${COLOR_STOP}" &> /dev/stdout } print_stderrr() { echo -e "${STDERR_COLOR}${*}${COLOR_STOP}" &> /dev/stderr } Like why are you exporting? Do you really need those in your environment? And those print statements aren't going to work by default.

Good questions.

For shell syntax, I aim for POSIX because for anything more advanced I switch from shell to a more powerful programming language.

Currently POSIX doesn't have the 'declare' statement, nor the '\e' syntax consistently, nor the 'echo -e' syntax consistently.

As for exporting, I do it because I have many quick scripts that I run often, and I prefer to switch the colors in one place such as in the evening from light mode to dark mode.

When you say the print statements aren't going to work by default, can you say more about this? Anything you can explain or point me to about this will be a big help. Thanks!

Re: How to choose colors for your CLI applications (2023)

#102
post #6

Earlier quoted context omitted.

More importantly, dont use color as sole source of information. Strikethrough, emoji or ok / bad can also be used.

Emojis aren't 7-bit clean. They're hard to type. They don't mean things the same way words do. `foo | grep -i error` communicates intent better than `foo | grep :-/` or whatever goofy hieroglyph someone chose instead of, like, a word with clearly defined meaning.

ok in that context use error or ok, just dont use color as ~10% of ppl have an issue with seeing colors perfectly (that includes people with epaper displays)

Re: How to choose colors for your CLI applications (2023)

#103
post #55

Earlier quoted context omitted.

Emojis aren't 7-bit clean. They're hard to type. They don't mean things the same way words do. `foo | grep -i error` communicates intent better than `foo | grep :-/` or whatever goofy hieroglyph someone chose instead of, like, a word with clearly defined meaning.

Yes that's why I also mentioned text labels. (strikethrough ansi codes aren't also fun to type). Besides, where are you needing 7but clean data ? Isn't that a narrow use case ?

> narrow use case?

It's the robustness principle. "Be conservative in what you do, be liberal in what you accept from others." A CLI author shouldn't assume support for UTF-8.

Post reply on HN