Files
bashlib/bashcols.inc
2021-07-28 00:27:40 +02:00

88 lines
1.6 KiB
Bash
Executable File

# vim: syntax=bash:
reset_esc='\e[0m'
# bgcol <color> <command>
# Use the specified color as background
function bgcol {
_bashcols_bgcol_conditionalsuffix=bg
"$@"
_bashcols_bgcol_conditionalsuffix=
}
# 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
if [[ -z $_bashcols_bgcol_conditionalsuffix ]]; then
tput setaf "$color"
else
tput setab "$color"
fi
"$@"
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
let "idx++" || true
if ! (( $idx % $width )); then
printf "\n"
fi
done
}
function colthis {
local name="$1"
shift
eval "printf \"%s\" \"\${${name}_esc${_bashcols_bgcol_conditionalsuffix}}$@$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
_bashlib_bashcols=1