Files
bashlib/bashcols
2021-02-10 20:39:52 +01:00

73 lines
1.3 KiB
Bash
Executable File

# vim: syntax=bash:
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 <color id> <command>"
return
fi
local color="$1"
shift
tput setaf "$color"
"$@"
tput sgr0
}
# defines a new color function
# and a variable named <colorname>_colid that contains the actual value for the color function
function defcol() {
if [[ $# -ne 2 ]]; then
echo "usage: defcol <color name> <color id>"
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 \"%s\" \"\${${name}_esc}$@$reset_esc\""
}
# default colors
defcol red 1
defcol green 2
defcol brown 3
defcol blue 4
defcol magenta 5
defcol cyan 6
defcol white 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