]> git.wh0rd.org - home.git/blob - .bin/bash-colors
cros-board: update
[home.git] / .bin / bash-colors
1 #!/bin/bash
2 # Author: Mike Frysinger <vapier@gmail.com>; released into the public domain; suck it.
3
4 # This is for bash.
5 BASH_PREFIX="\e"
6 # And this is the portable one.
7 POSIX_PREFIX="\033"
8 # Default to bash.
9 PREFIX=${BASH_PREFIX}
10
11 show_env() {
12 local c colors=(
13 GOOD="\$'${PREFIX}[32;01m'"
14 WARN="\$'${PREFIX}[33;01m'"
15 BAD="\$'${PREFIX}[31;01m'"
16 HILITE="\$'${PREFIX}[36;01m'"
17 BRACKET="\$'${PREFIX}[34;01m'"
18 NORMAL="\$'${PREFIX}[0m'"
19 )
20 for c in "${colors[@]}" ; do
21 [[ -t 0 ]] || c="${c%=*}="
22 echo "${c}"
23 done
24 }
25
26 check256() {
27 if [[ ${TERM} != *-256color ]] ; then
28 (
29 echo "warning: TERM (${TERM}) does not end in '-256color';"
30 echo "warning: trying to use 256 colors might not work."
31 ) >&2
32 fi
33 }
34
35 showit() {
36 local col="${PREFIX}[${1};${2}m"
37 printf '%bthis color is: %s %b\n' "${col}" "${col}" "${PREFIX}[0m"
38 }
39
40 main() {
41 if [[ $1 == "--posix" ]] ; then
42 PREFIX=${POSIX_PREFIX}
43 shift
44 fi
45 case $1 in
46 --env)
47 show_env
48 ;;
49 ""|16|system)
50 for i in {0..1} ; do
51 for j in {30..37} ; do
52 showit ${i} ${j}
53 done
54 done
55 ;;
56 cube|6x6x6)
57 check256
58 for n in {16..231} ; do
59 showit "38;05" $(printf '%03i' ${n})
60 done
61 ;;
62 gr[ae]y)
63 check256
64 for n in {232..255} ; do
65 showit "38;05" ${n}
66 done
67 ;;
68 all|256)
69 for x in system cube gray ; do
70 echo "### SET: ${x}"
71 "$0" ${x}
72 done
73 ;;
74 ncurses|curses|mutt|nano)
75 colors=(
76 black
77 red
78 green
79 yellow
80 blue
81 magenta
82 cyan
83 white
84 )
85 norm=$(tput sgr0)
86 bold=$(tput bold)
87 blackfg=$(tput setaf 0)
88 for seta in "af" "ab" ; do
89 [[ ${seta} == "af" ]] && ground=fore || ground=back
90 for pfx in "" "bright" ; do
91 for (( i = 0; i < ${#colors[@]}; ++i )) ; do
92 [[ ${pfx} == "bright" ]] && printf "${bold}"
93 [[ ${seta} == "ab" ]] && printf "${blackfg}"
94 echo "$(tput set${seta} ${i})this is ${ground}ground color '${pfx}${colors[i]}'${norm}"
95 done
96 done
97 done
98 ;;
99 *)
100 cat <<-EOF
101 usage: $0 [--posix] [colors]
102
103 Colors:
104 - --env - show env you can eval in a shell
105 - 16, system - show basic colors (default)
106 - cube, 6x6x6 - show most extended colors
107 - gray - show gray scale
108 - all, 256 - show all colors
109 - curses - show ncurses color names (used by mutt, nano, etc...)
110 EOF
111 exit 1
112 ;;
113 esac
114 }
115 main "$@"