Files

55 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
EosColor() {
local color="$1" # one of the foreground color strings below, or a reserved word below
local std="$2" # optional; stdout (=default) or stderr or 2
local progname=${0##*/}
case "$color" in
-h | --help) Help 0 ;;
esac
source "/etc/$progname.conf"
case "$color" in
"" | reset) color="$RESET" ;;
error | fail) color="$RED" ;;
warning) color="$YELLOW" ;;
info) color="$GREEN" ;;
tip | ok) color="$CYAN" ;;
esac
case "$std" in
stderr | 2) echo -n "$color" >&2 ;;
*) echo -n "$color" ;;
esac
}
Help() {
local -r progname=${0##*/}
cat <<EOF
Usage: $progname [Options] Color-def [Target-def]
Options: -h, --help This help. Optional.
Color-def: Color variables defined in /etc/$progname.conf. Required.
Special values:
error, fail For errors messages (red).
warning For warning messages (yellow).
info For info messages (green).
tip, ok For tips and OK messages (cyan).
"", reset Back to "normal" colors.
Target-def: Sets the file descriptor (fd) for the output. Optional.
Values:
2, stderr Output to fd 2.
Any other value Output to fd 1.
Examples:
eos-color error stderr Sets the color of future outputs to "error" (red).
Output goes to fd 2.
eos-color \$RED 2 Same as above, using a variable from /etc/$progname.conf.
eos-color tip Sets the color for future tips (for fd 1).
eos-color reset 2 Sets future outputs to "normal" colors (for fd 2).
eos-color Sets future outputs to "normal" colors (for fd 1).
EOF
[ "$1" ] && exit $1
}
EosColor "$@"