Skip to content

Instantly share code, notes, and snippets.

@codemedic
Created April 3, 2019 09:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codemedic/af013d32b4b7476d3108fc8309057970 to your computer and use it in GitHub Desktop.
Save codemedic/af013d32b4b7476d3108fc8309057970 to your computer and use it in GitHub Desktop.
Utility function to print bash colour codes
# based on https://github.com/mathieu-aubin/c7repos/blob/master/functions/bash-colors
__print_color_row() {
local color
color=$(printf '%03d' "$1")
echo -ne "\\033[1;48;5;${color}m \\\\033[48;5;${color}m \\033[0m";
echo -ne "\\033[1;7;38;5;${color}m\\\\033[7;38;5;${color}m \\033[0m";
echo -ne " \\033[1;38;5;${color}m\\\\033[1;38;5;${color}m\\033[0m";
echo -ne " \\033[38;5;${color}m\\\\033[38;5;${color}m\\033[0m";
echo -ne " \\033[2;38;5;${color}m\\\\033[2;38;5;${color}m\\033[0m";
echo -ne " \\033[3;38;5;${color}m\\\\033[3;38;5;${color}m\\033[0m";
echo -ne " \\033[4;38;5;${color}m\\\\033[4;38;5;${color}m\\033[0m";
echo -ne " \\033[9;38;5;${color}m\\\\033[9;38;5;${color}m\\033[0m";
echo -ne " \\033[4;9;38;5;${color}m\\\\033[4;9;38;5;${color}m\\033[0m";
echo -e " \\033[1;3;4;9;38;5;${color}m\\\\033[1;3;4;9;38;5;${color}m\\033[0m";
}
bash-colors() {
local incr=${1:-4}
if [ "$incr" -lt 1 ]; then
incr=4
fi
tput rmam;
for ((i=0; i<=256;)); do
__print_color_row $i
i=$((i+incr))
done
tput smam;
}
alias bash-colors-full='bash-colors 1'
alias bash-colors-minimal='bash-colors 8'
alias bash-colors-less='bash-colors 2'
alias bash-colors-random='bash-colors $(shuf -n1 -i 1-64)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment