Skip to content

Instantly share code, notes, and snippets.

@lebensterben
Last active June 18, 2019 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lebensterben/2548f75ea24becc7ac3dd1478397f1a7 to your computer and use it in GitHub Desktop.
Save lebensterben/2548f75ea24becc7ac3dd1478397f1a7 to your computer and use it in GitHub Desktop.
[DEPRECATED ]A simple zsh function to output term colors
## color-test
colortest () {
colortest_usage () {
echo -e "Usage: colortest [[-v|--verbose] | [-a|--all] | [-h|--help]]\n\nPrint out colors available in this terminal emulator. The default output is a short list of colors. If -v or --verbose is specified, a table containing all combination of background and foreground colors is displayed. Alternatively, if -a or -all is specified, then both short list and the detailed table would be displayed.\n\nNote that there should be no argument to this programme.\n -v, --verbose\t displays a table with detailed table of combinations of colors.\n -a, --all \t displays both brief and detailed results.\n -h, --help \t displays this help."
}
if [[ "$#" > 1 ]]; then
echo -e "$0: Error(1): At most one flag is allowed." >&2 | logger
colortest_usage
unset colortest_usage
return 1
fi
colortest_short () {
### gives the color table (text)
echo -e "\033[0mNC (No color)"
echo -e "\033[0;30mBLACK \t\033[1;30mGRAY"
echo -e "\033[0;31mRED \t\033[1;31mLIGHT_RED"
echo -e "\033[0;32mGREEN \t\033[1;32mLIGHT_GREEN"
echo -e "\033[0;33mYELLOW \t\033[1;33mLIGHT_YELLOW"
echo -e "\033[0;34mBLUE \t\033[1;34mLIGHT_BLUE"
echo -e "\033[0;35mPURPLE \t\033[1;35mLIGHT_PURPLE"
echo -e "\033[0;36mCYAN \t\033[1;36mLIGHT_CYAN"
echo -e "\033[0;37mLIGHT_GRAY\t\033[1;37mWHITE\033[0m"
}
if [[ "$#" -eq 0 ]]; then
colortest_short
unset -f colortest_usage colortest_short
return 1
fi
option=$(getopt -q -o vha --long verbose,help,all -- "$@")
if [[ ! "$?" -eq 0 ]]; then
echo -e "$0: Error(1): Unrecognized option $1." >&2 | logger
colortest_usage
unset -f colortest_usage colortest_short
unset option
return 1
fi
colortest_long () {
### gives the color table
printf " "
for b in 0 1 2 3 4 5 6 7; do
printf " 4${b}m "
done
echo
for f in "" 30 31 32 33 34 35 36 37; do
for s in "" "1;"; do
printf "%4sm" "${s}${f}"
printf " \033[%sm%s\033[0m" "$s$f" "gYw "
for b in 0 1 2 3 4 5 6 7; do
printf " \033[4%s;%sm%s\033[0m" "$b" "$s$f" " gYw "
done
echo
done
done
}
while [[ "$#" > 0 ]]; do
case "$1" in
-h|--help) colortest_usage;;
-v|--verbose) colortest_long;;
-a|--all) colortest_short; colortest_long;;
--) colortest_short;;
*) echo "$0: Error(1): This function doesn't accept any argument." >&2 | logger; colortest_usage;unset -f colortest_usage colortest_short colortest_long; option; return 1;;
esac
shift
done
unset -f colortest_usage colortest_short colortest_long
unset option
}
@lebensterben
Copy link
Author

lebensterben commented Jun 17, 2019

This implementation only works for zsh. It shall be put under $fpath and be autoloaded by zsh.

#-*- 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
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
  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