Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.