Skip to content

Instantly share code, notes, and snippets.

@lebensterben
Created June 18, 2019 00:59
Show Gist options
  • Save lebensterben/3b422c85d53757b3fdb8b697db23667f to your computer and use it in GitHub Desktop.
Save lebensterben/3b422c85d53757b3fdb8b697db23667f to your computer and use it in GitHub Desktop.
#-*- mode: shell-script -*-
# lscolor () {
emulate -RL zsh
setopt extendedglob
local myname usage opt lscolor_short
local -a lscolor_long
local b f s i
myname=${(%):-%N}
read -r -d '' usage <<EOT
Usage:
$myname [options]
Print out colors available in this terminal emulator. The default output is a short list of colors, unless -v or -a flag is turned on.
Options:
-v,--verbose display a table with all combination of background and foreground colors
-a,--all display both the list of colors and the table of all combinations of foreground and backgroud colors.
-h,--help display this help
EOT
option=$(getopt -q -o vha --long verbose,help,all -- "$@")
if [[ ! "$?" -eq 0 ]]; then
echo -e "$0: Error(1): Unrecognized option $1." >&2 | logger
print $usage
fi
read -r -d '' lscolor_short <<EOT
\033[0mNC (No color)
\033[0;30mBLACK \t\033[1;30mGRAY
\033[0;31mRED \t\033[1;31mLIGHT_RED
\033[0;32mGREEN \t\033[1;32mLIGHT_GREEN
\033[0;33mYELLOW \t\033[1;33mLIGHT_YELLOW
\033[0;34mBLUE \t\033[1;34mLIGHT_BLUE
\033[0;35mPURPLE \t\033[1;35mLIGHT_PURPLE
\033[0;36mCYAN \t\033[1;36mLIGHT_CYAN
\033[0;37mLIGHT_GRAY\t\033[1;37mWHITE\033[0m
EOT
lscolor_long=("printf ' '")
for b in 0 1 2 3 4 5 6 7; do
lscolor_long+=("printf \" 4${b}m \"")
done
lscolor_long+=("printf \"\n\"")
for f in "" 30 31 32 33 34 35 36 37; do
for s in "" "1;"; do
lscolor_long+=("printf \"%4sm\" \"${s}${f}\"")
lscolor_long+=("printf \" \033[%sm%s\033[0m\" \"$s$f\" \"gYw \"")
for b in 0 1 2 3 4 5 6 7; do
lscolor_long+=("printf \" \033[4%s;%sm%s\033[0m\" \"$b\" \"$s$f\" \" gYw \"")
done
lscolor_long+=("printf \"\n\"")
done
done
if (( $# > 1 )); then
print $usage
return 1
fi
if (( $# == 0 )); then
echo $lscolor_short
return 0
fi
while getopts ":hva" opt; do
case "${opt}" in
h ) echo $usage
return 0
;;
v ) for i in $lscolor_long; do
eval $i
done
return 0
;;
a ) print $lscolor_short
for i in $lscolor_long; do
eval $i
done
return 0
;;
esac
done
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment