-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.sh
executable file
·167 lines (144 loc) · 2.29 KB
/
color.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
set -Cefu
usage() {
printf '%s\n' 'color.sh [bg | fg | table] [char]'
exit 1
} >&2
# Uses:
# `printf` - bash, dash, ksh93, posh, yash, zsh
# `print` - mksh, oksh, pdksh
# `{n1..n2}` - bash, zsh, ksh93
set_sh() {
brace_e=false
# shellcheck disable=SC2059
sprint() {
printf "$@"
}
sprintln() {
printf '\n'
}
if [ -n "${BASH_VERSION+x}" ] || [ -n "${ZSH_VERSION+x}" ]
then
brace_e=true
elif [ -n "${KSH_VERSION+x}" ]
then
case "$KSH_VERSION" in
*'AJM'*) # ksh93
brace_e=true
;;
*'MIRBSD'*|*'PD'*) # mksh, pdksh, oksh
sprint() {
print -nr -- "$@"
}
sprintln() {
print -r --
}
;;
esac
fi
}
color_bg() {
case $brace_e in
true)
# shellcheck disable=SC3009
for y in 4 10
do
sprint "${esc}[$y%sm ${esc}[0m" {1..7}
sprintln
done
;;
false)
for y in 4 10
do
for z in 0 1 2 3 4 5 6 7
do
sprint "${esc}[${y}${z}m ${esc}[0m"
done
sprintln
done
;;
esac
}
color_fg() {
case $brace_e in
true)
# shellcheck disable=SC3009
for y in 0 1
do
sprint "${esc}[$y;3%sm $1 ${esc}[0m" {1..7}
sprintln
done
;;
false)
for y in 0 1
do
for z in 0 1 2 3 4 5 6 7
do
sprint "${esc}[${y};3${z}m$1 "
done
sprint "${esc}[0m"
sprintln
done
;;
esac
}
color_table() {
arg=$1
printf '%12s' 40m
printf '%6s' 41m 42m 43m 44m 45m 46m 47m 48m
sprintln
set -- ' 0' ' 1' \
' 30' '1;30' ' 31' '1;31' \
' 32' '1;32' ' 33' '1;33' \
' 34' '1;34' ' 35' '1;35' \
' 36' '1;36' ' 37' '1;37'
case $brace_e in
true)
# shellcheck disable=SC3009
for fgs
do
fg=
for w in $fgs
do
fg="$fg$w"
done
printf '%b' " ${fgs}m ${esc}[${fg}m $arg "
printf " ${esc}[${fg}m${esc}[4%sm $arg ${esc}[0m" {0..7}
printf '\n'
done
;;
false)
for fgs
do
fg=
for w in $fgs
do
fg="$fg$w"
done
sprint " ${fgs}m ${esc}[${fg}m $arg "
for bg in 0 1 2 3 4 5 6 7
do
sprint "${esc}[${fg}m ${esc}[4${bg}m $arg ${esc}[0m"
done
sprintln
done
;;
esac
}
main() {
[ $# -ge 3 ] && usage
# shellcheck disable=SC3003
esc=$'\033' && [ ${#esc} = 1 ] || esc=$(printf '\033')
set_sh
case "${1:-table}" in
bg)
color_bg "${2:-}" ;;
fg)
color_fg "${2:-λ}" ;;
table)
color_table "${2:-λ}" ;;
*)
table ;;
esac
}
main ${1+"$@"}