]> git.wh0rd.org - home.git/blame - .bin/bash-colors
cros-board: update
[home.git] / .bin / bash-colors
CommitLineData
8069c098 1#!/bin/bash
412ff3c0 2# Author: Mike Frysinger <vapier@gmail.com>; released into the public domain; suck it.
ea2a7d52 3
3b6c49d6
MF
4# This is for bash.
5BASH_PREFIX="\e"
6# And this is the portable one.
7POSIX_PREFIX="\033"
8# Default to bash.
9PREFIX=${BASH_PREFIX}
10
412ff3c0
MF
11show_env() {
12 local c colors=(
3b6c49d6
MF
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'"
ea2a7d52
MF
19 )
20 for c in "${colors[@]}" ; do
b73f07c7 21 [[ -t 0 ]] || c="${c%=*}="
ea2a7d52
MF
22 echo "${c}"
23 done
412ff3c0 24}
ea2a7d52 25
412ff3c0
MF
26check256() {
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}
ea2a7d52 34
ee33e474 35showit() {
3b6c49d6
MF
36 local col="${PREFIX}[${1};${2}m"
37 printf '%bthis color is: %s %b\n' "${col}" "${col}" "${PREFIX}[0m"
ee33e474 38}
412ff3c0
MF
39
40main() {
3b6c49d6
MF
41 if [[ $1 == "--posix" ]] ; then
42 PREFIX=${POSIX_PREFIX}
43 shift
44 fi
412ff3c0
MF
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
ee33e474 54 done
412ff3c0
MF
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 ;;
1f3bf8d2
MF
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 ;;
412ff3c0
MF
99 *)
100 cat <<-EOF
3b6c49d6 101 usage: $0 [--posix] [colors]
412ff3c0
MF
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
1f3bf8d2 109 - curses - show ncurses color names (used by mutt, nano, etc...)
412ff3c0
MF
110 EOF
111 exit 1
112 ;;
113 esac
114}
115main "$@"