Live data from Hacker News

How to choose colors for your CLI applications (2023)

blog.xoria.org

71–80 of 103 posts

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

#72

I really wish you wouldn't. All the rinky dink colors and animations screw with the CLI output when you don't correctly detect whether the user's running the app interactively. Keep it plain text. Regular, old, boring output is good.

I agree. So many TUIs from webshit devs don't even bother to call isatty, let alone check terminfo to see if ANSI escape codes are even valid for this terminal.

But modern open source subscribes to Mao's Continuous Revolution Theory. Calls for some measure of stability and sanity are usually dismissed with some form of the argument "awwww, is poor diddums afwaid of a widdle change?" Or in this case, "still using vi on your ADM3A, old timer? Our software is not for you."

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

#73
post #64

Earlier quoted context omitted.

I dislike when devs only try to detect if it’s a tty, then enable all their gimmicks without even providing a flag. Not everything is xterm-256color.

And not everything that calls itself xterm-256color is actually xterm compatible *cough* GNOME *cough*.

Xterm is actually a terminal emulator, and has to pass a suite of conformance tests that actually check its emulation of DEC VT series terminals.

Most of its successors are more like "shitty xterm emulators" whose conformance tests are "do my favorite mOdErN CLI apps work".

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

#74
I use the built-in TokyoNight Day theme as my light theme in GhosTTY and I think it's almost perfect. Then I use TokyoNightMoon for dark. Works great. Hard to use anything else now.

If you're CLI application doesn't play nice with it (i haven't seen many) I don't use it.

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

#75
post #68
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…

If you're writing a zsh script and not worried about portability, you can also use the prompt expansion colors with "print". print_color () { print -P "%F{$1}$2%f" } And then to use it print_color "green" "This is printed in green"

Here's something also useful that's portable

  declare GRN='\e[1;32m'
  declare RED='\e[1;31m'
  declare YLW='\e[1;33m'
  declare CYN='\e[1;36m'

  write_log() {
      echo -e "[$( date +'%c' )] : ${1}\e[0m" | tee -a ${logfile}
  }

  write_log "${RED}I'm an ERROR"

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

#76
post #39

As long as CLI programs stick to the 8 or 16 standard colors and refrain from setting background colors (inverse mode is fine), as well as from explicitly setting white or black as text color, everyone can reasonably configure their terminal colors so that everything is readable. When going beyond that, the colors really need to be configurable on the application.

Configurable within the application... at runtime.

I want to be able to switch existing terminals with existing applications between themes.

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

#77
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.

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

#78
post #39

As long as CLI programs stick to the 8 or 16 standard colors and refrain from setting background colors (inverse mode is fine), as well as from explicitly setting white or black as text color, everyone can reasonably configure their terminal colors so that everything is readable. When going beyond that, the colors really need to be configurable on the application.

> refrain from setting background colors That's the thing though, setting bg color opens up a lot of options, and constraining to invert is not sufficient in my opinion.

That’s fine, but please make the colors configurable then.

For example like Mutt does: http://www.mutt.org/doc/manual/#color

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

#79
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.

> They're hard to type.

Globe key + E on Mac, Windows key + period on Windows, Ctrl + period on GNOME, Super key + period on KDE, yada yada.

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

#80
I made the "Aardvark Blue" a while back[1] to solve some of the problems that most color schemes have. The goal of this theme is that:

- colors are fairly natural

- background and black are distinct

- grays are naturally ordered avoiding full black

- light and dark colors are distinct from each other

- all colors look good on background, black, dark gray, gray, white

We use this for all the screenshots on https://ratatui.rs and https://github.com/ratatui/ratatui

It's available from the usual places https://iterm2colorschemes.com/, https://windowsterminalthemes.dev/?theme=Aardvark%20Blue, built in to ghostty, extension for vscode etc.

[1]: https://github.com/mbadolato/iTerm2-Color-Schemes/pull/417

Post reply on HN