News for hackers. Stuff that matters.
Re:Techniques I Use to Create a Great User Experience for Sh... by haileys on Friday September 13, 2024 @07:14PM (#41536036)
Don't output ANSI colour codes directly - your output could redirect to a file, or perhaps the user simply prefers no colour. Use tput instead, and add a little snippet like this to the top of your script:

    command -v tput &>/dev/null && [ -t 1 ] && [ -z "${NO_COLOR:-}" ] || tput() { true; }
This checks that the tput command exists (using the bash 'command' builtin rather than which(1) - surprisingly, which can't always be relied upon to be installed even on modern GNU/Linux systems), that stdout is a tty, and that the NO_COLOR env var is not set. If any of these conditions are false, a no-op tput function is defined.

This little snippet of setup lets you sprinkle tput invocations through your script knowing that it's going to do the right thing in any situation.

Re:Techniques I Use to Create a Great User Experience for Sh... by lilyball on Friday September 13, 2024 @10:13PM (#41536965)
If you use tput a lot it's also worth caching the output, because invoking it for every single color change and reset can really add up. If you know you're going to use a bunch of colors up front you can just stuff them into vars

  RED=$(tput setaf 1)
  GREEN=$(tput setaf 2)
  RESET=$(tput sgr0)
Re:Techniques I Use to Create a Great User Experience for Sh... by hinkley on Friday September 13, 2024 @11:25PM (#41537251)
There should just be a command for this. Like echo with a color flag that does something if you’re in a tty.
Re:Techniques I Use to Create a Great User Experience for Sh... by CGamesPlay on Saturday September 14, 2024 @05:11AM (#41538530)
But since there isn’t, even if you make one, people won’t want to rely on it as a dependency.
Re:Techniques I Use to Create a Great User Experience for Sh... by Aeolun on Saturday September 14, 2024 @08:03AM (#41539261)
Can add it to bash?
Re:Techniques I Use to Create a Great User Experience for Sh... by CGamesPlay on Saturday September 14, 2024 @12:26PM (#41540802)
It’s a bit more complicated. POSIX shell != bash, for example the default shell (/bin/sh) on macOS is now zsh, on Ubuntu it’s dash. Bash may still be installed, but may be years older than 2024. At a certain point, you’re better off embracing a dependency that just does everything better, like Python or Oil shell, for example.
Re:Techniques I Use to Create a Great User Experience for Sh... by f1shy on Saturday September 14, 2024 @09:29AM (#41539669)
Why? Benefit? 10% of people hace problemas with colors. Depending on the terminal/background, you will produce bad/invisible output. Any good typesetting book will tell you not to use colors.
Re:Techniques I Use to Create a Great User Experience for Sh... by irundebian on Saturday September 14, 2024 @10:18AM (#41539973)
I have problems with non-colored output because it makes it harder to distinguish important from less important stuff.
Re:Techniques I Use to Create a Great User Experience for Sh... by hinkley on Saturday September 14, 2024 @04:36PM (#41542653)
Why are you arguing against the entire thread as a reply to my comment?
Re:Techniques I Use to Create a Great User Experience for Sh... by jph on Saturday September 14, 2024 @12:46AM (#41537529)
Yes and even better for faster speed and greater shell compatibility for basic colors, you can use this POSIX code:

    if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
      COLOR_RESET=''
      COLOR_RED=''
      COLOR_GREEN=''
      COLOR_BLUE=''
    else
      COLOR_RESET=''
      COLOR_RED=''
      COLOR_GREEN=''
      COLOR_BLUE=''
    fi
For more about this see Unix Shell Script Tactics: https://github.com/SixArm/unix-shell-script-tactics/tree/mai...

Be aware there's an escape character at the start of each of color string, which is the POSIX equivalent of $'\e'; Hacker News seems to cut out that escape character.

Re:Techniques I Use to Create a Great User Experience for Sh... by account42 on Monday September 16, 2024 @11:22AM (#41556993)
You should also at least check for TERM=dumb which is an older convention than NO_COLOR.
Re:Techniques I Use to Create a Great User Experience for Sh... by jph on Monday September 16, 2024 @06:43PM (#41561935)
Good point, you're right. Added. Thank you.