# vim: syntax=bash: eval "reset_esc=\"\\e[0m\"" # applies the given color id, executes the command, then resets the color function colout() { if [[ $# -eq 0 ]]; then echo "usage: colout " return fi local color="$1" shift tput setaf "$color" "$@" tput sgr0 } # defines a new color function # and a variable named _colid that contains the actual value for the color function function defcol() { if [[ $# -ne 2 ]]; then echo "usage: defcol " return fi local name="$1" local color="$2" eval "${name}_colid=$color" eval "${name}_esc=\"\\e[38;5;${color}m\"" eval "${name}_escbg=\"\\e[48;5;${color}m\"" eval "function $name { colout $color \"\$@\"; }" } # displays all 256 colors function dispcollist() { local width=${1:-16} local idx=0 for colid in {1..256}; do colout $colid printf "%3s " $colid idx=$(($idx+1)) if [[ $(($idx % $width)) -eq 0 ]]; then printf "\n" fi done } function colthis { local name="$1" shift eval "printf \"\${${name}_esc}$@$reset_esc\"" } # default colors defcol red 1 defcol green 2 defcol browni 3 defcol bluei 4 defcol magenta 5 defcol cyan 6 defcol whitei 7 defcol grey 8 defcol redl 9 defcol greenl 10 defcol yellow 11 defcol bluel 12 defcol magenta 13 defcol cyanl 14 defcol whitel 15 defcol black 16