dye is a portable and respectful library for adding color and emphasis to the output of shell scripts.
It's portable because
-
it works on many Unix systems, including macOS, Linux, and OpenBSD;
-
it is written to the POSIX shell standard, so it works in many shells that are POSIX-compatible, such as ash and Dash, Bash, ksh, and Zsh;
-
it uses tput(1) instead of hard-wired ANSI sequences, so it will work wherever the appropriate terminal capabilities are available, and gracefully degrade where they are not; and
-
it additionally degrades gracefully if tput(1) is not available (e.g. in a Docker alpine image where ncurses is not installed).
It's respectful because:
-
it will disable if "NO_COLOR" is set;
-
it will enable if "CLICOLOR" is set, but only if stdout is a tty;
-
it will unconditionally enable (e.g. if stdout is not a tty) if "CLICOLOR_FORCE" is set;
-
it allows script developers to choose to default to color off unless the user has opted in with environment variables; and
-
it is written to only put functions and environment variables prefixed with "dye" or "DYE" into the shell's global namespace, and carefully avoids clobbering any existing shell variables during operation.
dye does call tput for every terminal sequence it needs to output, so it's not screamingly fast. However, in practice, it's more than fast enough for the job I need it for: making the output of shell scripts colorful to make them easier to read and scan. If you're working with lots of color (for example, creating ANSI art), it's probably best to stick to a solution that caches ANSI sequences and forgo the portability.
I wrote dye to replace my previous project, portable-color. portable-color was fine, but would load lots of functions into the shell's global namespace. dye has a better API, with more capabilities and conveniences.
You can use dye in your script by copying the contents of dye.sh into your script so that the functions are defined before you use them.
This the method I recommend. Shell scripts that I write are generally made to be self-contained, and embedding makes them very easy to download and use.
A couple notes on this strategy:
-
If you don't want dye's over 100 lines of code at the top of your script, wrap your main code in a function, then insert all of dye's code, and finally call your main function at the end.
-
If you end up changing dye's code, consider changing the names of the dye functions and variables that end up in the shell's global namespace, to avoid conflicts with the standard dye code that may be depended on elsewhere—particularly if you're removing functionality you do not use.
-
If you distribute your script with dye's code inline, you must include a copy of the license in some way with your script. This specifically so others understand their rights in regard to the use of this software.
If you have dye.sh available on your PATH (executable bit not necessary), you can load it very simply:
. dye.sh || exit 1If you include a copy alongside your script, you can also load it from a specific directory:
. ./dye.sh || exit 1dye must be initialized once before use, or no text changes will happen when you use the color or emphasis routines:
dye setup"setup" is where dye will check things like whether stdout is a tty, whether environment variables like "NO_COLOR" and "CLICOLOR" are set, and make a decision whether or not to set the variable "DYE_COLORS" to the number of available colors, which the other routines will use.
An alternate mode for "setup" will not enable color by default, but will enable it if the user has "CLICOLOR" set:
dye setup default-offThe best way to use dye is with its built-in templates.
Templates embed other dye commands inside a single string.
dye print "{{green}}It's not easy being... well, you know.{{reset}}"Like "echo", "dye print" ends its output with a newline. ("dye p" is a shorthand for "dye print" and highly recommended.)
Note that unlike "echo" on many systems, control characters are not automatically parsed. Whatever the shell passes to "dye print" is only processed according to two simple syntax rules.
dye write "So {{bold}}bold{{reset}}, it's not recommended "
dye write "for human consumption!""dye write" is the same, except it does not end its output with a newline.
Templates have two basic syntax rules:
-
Text between double curly braces ("{{" and "}}") is interpreted as a dye command. The curly braces are not printed.
-
A backslash ("") escapes the next character if it is either a backlash or left curly brace. The backslash itself is not printed. Useful if you want to print two left curly braces.
dye commands between double curly braces, and as such can have trailing spaces added to enable multiline strings:
dye print "We have an {{bold
}}awful{{reset}} lot to say across {{bold
}}many{{reset}} lines."There is one caveat to the syntax rules. If you're using double quotes, the backslash behavior can be a bit unintuitive in one case.
dye print "a\\{{red}}/b"This actually prints "a{{red}}/b", because the double backlash is interpreted as a single backslash by the shell before dye even sees it. dye then interprets the resulting "\{" as escaping "{".
Escaping both backslashes passes through "\\" to dye's engine, which in turn interprets it as an escaped single-backslash. This prints a\/b with a red color change in the middle:
dye print "a\\\\{{red}}/b"Using single-quoted strings works around this issue, but of course sacrifices the variable interpolation you get with double-quoted strings.
Note
Wrapping text is deprecated due to the awkwardness of dealing with resets and the extra weirdness of unquoted wrapping, and is likely to be removed in a future major version.
For simple color and emphasis, using dye to wrap quoted text is the most convenient method.
echo "$(dye green "It's not easy being... well, you know.")"echo "So $(dye bold "bold"), it's not recommended for human consumption\!"Quoting text is also not strictly necessary, but can result in the need to use many more escapes (just like it would if using "echo" straight up). It also means whitespace gets collapsed, so beware!
echo Quotes\? Quotes\? $(dye italic We don\'t need no stinking quotes\!)When wrapping text, one key caveat applies: all colors and several emphases do not have a matching ending terminal sequence—they can only be turned off by sending an "sgr0" terminal capability to reset all color and emphasis.
dye will send this reset sequence at the end of wrapped text for colors and select emphases, so it's best not to stack wrappers. It gets unreadable really fast, anyway, so it's better to use manual control.
You can generally use command substitution to capture the output from manual control commands so it can all be strung together:
echo "$(dye cyan)$(dye bold)Cyan$(dye reset), $(dye magenta)$(dye bold
)magenta$(dye reset), and $(dye bold)white$(dye reset
) ought to be enough for anybody."Using lots of manual control can make lines pretty long, but as you can see, you can also leverage the fact that line breaks are valid inside command substitution to break them up.
It's better (and shorter!) to use templates, though. Capturing the output of dye works in most cases, but could break in some obscure situations.
Many colors are available for use, subject to terminal support.
There's the basic ANSI color set:
black(or0)red(or1)green(or2)yellow(or3)blue(or4)magenta(or5)cyan(or6)white(or7, orbrightgray)gray(or8)brightred(or9)brightgreen(or10)brightyellow(or11)brightblue(or12)brightmagenta(or13)brightcyan(or14)brightwhite(or15)
"dye yellow" will set the foreground color to yellow, for example. There are also "fg" and "bg" commands that will explicitly set the foreground or background color, respectively:
dye print '{{bg blue}}{{fg yellow}}In Ann Arbor, everything is this color.{{reset}}'Turning colors off requires replacing them with a different color, or a reset.
Some terminal definitions, like "ansi" and "xterm", don't recognize colors higher than 7. If only colors 0-7 are supported, indicated by "DYE_COLORS" being set to 8 (via setup), dye will synthesize "bright" colors by turning on "bold" and setting the non-bright equivalent color. (Some terminals will, in turn, render this with a bright color!)
Note that this also means that "bold" may be turned on unexpectedly if you're using "bright" colors—so keep this situation in mind:
-
If you're using templates or manual control, be sure to reset at the appropriate time if the possiblity that "bold" might be on.
-
If you're nesting wrapped text, make sure that nested text deals with the fact that "bold" might be on if you're using a "bright" color.
You can test your code in this situation by setting "TERM" to "ansi" or "xterm" on many systems.
Several emphases are available as well.
The first group, like colors, must be reset to turn them off:
dim(makes things darker)boldreverse(see also "standout" below)
They can be used just like colors, and wrapping text with them will automatically send a reset at the end.
The second group have "end" terminal sequences that can turn them off explicitly, with all other settings remaining in play:
italic(ori)standout(orso, often displayed as reversed foreground and background)underline(orul, oru)
When using one of these, you don't have to re-enable other modes—just "end" them:
dye print "{{magenta}}Mary {{italic}}had{{end italic}} a little lamb.{{reset}}"
dye print "{{magenta}}Mary had a {{italic}}little{{end italic}} lamb.{{reset}}"They also support old-school wrapping:
echo "Visit $(dye ul "https://mattiebee.dev/dye") to get the code."To match "end", "begin" is also available (and works with all emphases). It behaves the same way as just using the emphasis, e.g. "dye begin italic" is equivalent to "dye italic".
Unit tests are exhaustively written in shellspec. The specs script will look for shells on your system that are expected to be compatible, and run the suite for each, stopping on the first failures.
coverage pairs shellspec with kcov in Docker to gather coverage while running against Bash. (I'm using Docker here because I can't get shellspec with kcov to work on macOS.) 100% is impossible to reach due to kcov thinking some syntax isn't covered. But all meaningful lines of dye.sh are covered.
Code should all be written to the POSIX shell spec. Deviations are probably bugs. 🐜
This is particularly important because dye should run everywhere it can, including in very limited systems. If it starts getting loaded up with Bashisms, it won't work in some places.
During the development of dye, I did explore things like caching the output of "tput" so it didn't have to be invoked quite so much.
The added complexity was really not worth it, since "tput" is still fast enough (i.e. not at all noticeably slow) for most purposes where a shell script is doing work for at least a small amount of time. The cache would also need to be filled, and most scripts just don't switch colors enough to make it worthwhile.
The sequences dye generally uses are simple and unconcerned with this, but there are also interesting details with certain terminal control sequences on certain systems that "tput" can handle if invoked directly, such as embedded delays. So, the practice also encourages maximum compatibility—especially if using the new template engine, which passes control back and forth between "tput" and "printf".
