-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle
300 lines (259 loc) · 11.6 KB
/
style
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
# This is implemented as a function not a script because it runs many times per
# prompt, and we don't want any extra overhead (timed at 0m0.016s per call).
# For easy use, there is also a script in .bin/style that uses this function.
style() {
# Disable trailing new line?
local newline=true
if [[ ${1:-} = '-n' ]]; then
newline=false
shift
fi
# Save the original parameters
local styles=${1:-}
shift
# Help Bash calculate the correct prompt size (Note: "\[" doesn't work here)
# This doesn't work in Windows Terminal
#echo -en "\001"
# Reset to prevent any previous styles interfering
echo -en "\e[0m"
# Output ANSI codes and text
local style
for style in ${styles//,/ }; do
# Automatically calculate the inverse colour, if required
# Warning: This is relative slow (10ms) so don't use it unless necessary
# It is mainly here for use in the 'style-test' script
if [[ $style = 'fg=auto' ]]; then
style="$(_color-inverse-fg "$styles")"
elif [[ $style = 'bg=auto' ]]; then
style="$(_color-inverse-bg "$styles")"
fi
case "$style" in
reset) echo -en "\e[0m" ;;
bold) echo -en "\e[1m" ;; # May do the same as the "l" colours, or may actually be bold
dim) echo -en "\e[2m" ;;
italic) echo -en "\e[3m" ;;
underline) echo -en "\e[4m" ;;
blink) echo -en "\e[5m" ;;
rapidblink) echo -en "\e[6m" ;; # Not widely supported
reverse) echo -en "\e[7m" ;;
hide) echo -en "\e[8m" ;;
strike) echo -en "\e[9m" ;;
fg=black | black) echo -en "\e[30m" ;;
fg=red | red) echo -en "\e[31m" ;;
fg=green | green) echo -en "\e[32m" ;;
fg=yellow | yellow) echo -en "\e[33m" ;;
fg=blue | blue) echo -en "\e[34m" ;;
fg=magenta | magenta) echo -en "\e[35m" ;;
fg=cyan | cyan) echo -en "\e[36m" ;;
fg=white | white) echo -en "\e[37m" ;;
fg=lblack | lblack) echo -en "\e[90m" ;;
fg=grey | grey) echo -en "\e[90m" ;;
fg=lred | lred) echo -en "\e[91m" ;;
fg=lgreen | lgreen) echo -en "\e[92m" ;;
fg=lyellow | lyellow) echo -en "\e[93m" ;;
fg=lblue | lblue) echo -en "\e[94m" ;;
fg=lmagenta | lmagenta) echo -en "\e[95m" ;;
fg=lcyan | lcyan) echo -en "\e[96m" ;;
fg=lwhite | lwhite) echo -en "\e[97m" ;;
fg=? | fg=?? | fg=???) echo -en "\e[38;5;${style:3}m" ;;
fg=\#???) echo -en "\e[38;2;$(_color-hex-to-ansi "${style:4}")m" ;;
\#???) echo -en "\e[38;2;$(_color-hex-to-ansi "${style:1}")m" ;;
fg=\#??????) echo -en "\e[38;2;$(_color-hex-to-ansi "${style:4}")m" ;;
\#??????) echo -en "\e[38;2;$(_color-hex-to-ansi "${style:1}")m" ;;
bg=black) echo -en "\e[40m" ;;
bg=red) echo -en "\e[41m" ;;
bg=green) echo -en "\e[42m" ;;
bg=yellow) echo -en "\e[43m" ;;
bg=blue) echo -en "\e[44m" ;;
bg=magenta) echo -en "\e[45m" ;;
bg=cyan) echo -en "\e[46m" ;;
bg=white) echo -en "\e[47m" ;;
bg=lblack) echo -en "\e[100m" ;;
bg=grey) echo -en "\e[100m" ;;
bg=lred) echo -en "\e[101m" ;;
bg=lgreen) echo -en "\e[102m" ;;
bg=lyellow) echo -en "\e[103m" ;;
bg=lblue) echo -en "\e[104m" ;;
bg=lmagenta) echo -en "\e[105m" ;;
bg=lcyan) echo -en "\e[106m" ;;
bg=lwhite) echo -en "\e[107m" ;;
bg=? | bg=?? | bg=???) echo -en "\e[48;5;${style:3}m" ;;
bg=\#???) echo -en "\e[48;2;$(_color-hex-to-ansi "${style:4}")m" ;;
bg=\#??????) echo -en "\e[48;2;$(_color-hex-to-ansi "${style:4}")m" ;;
*)
echo -e "\e[0m" # Reset
echo "style: Invalid style '$style'" >&2
return 1
;;
esac
done
#echo -en "\002"
echo -n "$@"
#echo -en "\001\e[0m\002"
echo -en "\e[0m"
if $newline; then
echo
fi
}
_color-hex-to-ansi() {
local hex=$1
local r g b
if [[ ${#hex} = 3 ]]; then
r="${hex:0:1}${hex:0:1}"
g="${hex:1:1}${hex:1:1}"
b="${hex:2:1}${hex:2:1}"
else
r=${hex:0:2}
g=${hex:2:2}
b=${hex:4:2}
fi
echo "$((16#$r));$((16#$g));$((16#$b))"
}
_color-inverse-bg() {
local styles=$1
local result='black'
local style
for style in ${styles//,/ }; do
case "$style" in
fg=black | black) result=$(_color-inverse-ansi bg 0) ;;
fg=red | red) result=$(_color-inverse-ansi bg 1) ;;
fg=green | green) result=$(_color-inverse-ansi bg 2) ;;
fg=yellow | yellow) result=$(_color-inverse-ansi bg 3) ;;
fg=blue | blue) result=$(_color-inverse-ansi bg 4) ;;
fg=magenta | magenta) result=$(_color-inverse-ansi bg 5) ;;
fg=cyan | cyan) result=$(_color-inverse-ansi bg 6) ;;
fg=white | white) result=$(_color-inverse-ansi bg 7) ;;
fg=lblack | lblack) result=$(_color-inverse-ansi bg 8) ;;
fg=grey | grey) result=$(_color-inverse-ansi bg 8) ;;
fg=lred | lred) result=$(_color-inverse-ansi bg 9) ;;
fg=lgreen | lgreen) result=$(_color-inverse-ansi bg 10) ;;
fg=lyellow | lyellow) result=$(_color-inverse-ansi bg 11) ;;
fg=lblue | lblue) result=$(_color-inverse-ansi bg 12) ;;
fg=lmagenta | lmagenta) result=$(_color-inverse-ansi bg 13) ;;
fg=lcyan | lcyan) result=$(_color-inverse-ansi bg 14) ;;
fg=lwhite | lwhite) result=$(_color-inverse-ansi bg 15) ;;
fg=? | fg=?? | fg=???) result=$(_color-inverse-ansi bg "${style:3}") ;;
fg=#???) result=$(_color-inverse-hex bg "${style:4}") ;;
'#???') result=$(_color-inverse-hex bg "${style:1}") ;;
fg=#??????) result=$(_color-inverse-hex bg "${style:4}") ;;
'#??????') result=$(_color-inverse-hex bg "${style:1}") ;;
esac
done
echo "bg=$result"
}
_color-inverse-fg() {
local styles=$1
local result='lwhite'
local style
for style in ${styles//,/ }; do
case "$style" in
bg=black) result=$(_color-inverse-ansi fg 0) ;;
bg=red) result=$(_color-inverse-ansi fg 1) ;;
bg=green) result=$(_color-inverse-ansi fg 2) ;;
bg=yellow) result=$(_color-inverse-ansi fg 3) ;;
bg=blue) result=$(_color-inverse-ansi fg 4) ;;
bg=magenta) result=$(_color-inverse-ansi fg 5) ;;
bg=cyan) result=$(_color-inverse-ansi fg 6) ;;
bg=white) result=$(_color-inverse-ansi fg 7) ;;
bg=lblack) result=$(_color-inverse-ansi fg 8) ;;
bg=grey) result=$(_color-inverse-ansi fg 8) ;;
bg=lred) result=$(_color-inverse-ansi fg 9) ;;
bg=lgreen) result=$(_color-inverse-ansi fg 10) ;;
bg=lyellow) result=$(_color-inverse-ansi fg 11) ;;
bg=lblue) result=$(_color-inverse-ansi fg 12) ;;
bg=lmagenta) result=$(_color-inverse-ansi fg 13) ;;
bg=lcyan) result=$(_color-inverse-ansi fg 14) ;;
bg=lwhite) result=$(_color-inverse-ansi fg 15) ;;
bg=? | bg=?? | bg=???) result=$(_color-inverse-ansi fg "${style:3}") ;;
bg=#???) result=$(_color-inverse-hex fg "${style:4}") ;;
bg=#??????) result=$(_color-inverse-hex fg "${style:4}") ;;
esac
done
echo "fg=$result"
}
# Based on https://gist.github.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263
# (c) Tom Hale, 2016. MIT Licence
_color-inverse-ansi() {
local type=$1
local style=$2
# Initial 16 ANSI colours
if (( style < 16 )); then
case "$style" in
0) _color-inverse-rgb "$type" 0 0 0 ;; # black
1) _color-inverse-rgb "$type" 191 0 0 ;; # red
2) _color-inverse-rgb "$type" 0 191 0 ;; # green
3) _color-inverse-rgb "$type" 191 191 0 ;; # yellow
4) _color-inverse-rgb "$type" 59 72 227 ;; # blue (Note: I adjust this to make it more readable)
5) _color-inverse-rgb "$type" 191 0 191 ;; # magenta
6) _color-inverse-rgb "$type" 0 191 191 ;; # cyan
7) _color-inverse-rgb "$type" 191 191 191 ;; # white
8) _color-inverse-rgb "$type" 64 64 64 ;; # lblack (grey)
9) _color-inverse-rgb "$type" 255 64 64 ;; # lred
10) _color-inverse-rgb "$type" 64 255 64 ;; # lgreen
11) _color-inverse-rgb "$type" 255 255 64 ;; # lyellow
12) _color-inverse-rgb "$type" 125 135 236 ;; # lblue (Note: I adjust this to make it more readable)
13) _color-inverse-rgb "$type" 255 64 255 ;; # lmagenta
14) _color-inverse-rgb "$type" 64 255 255 ;; # lcyan
15) _color-inverse-rgb "$type" 255 255 255 ;; # lwhite
*) echo 'lwhite' ;;
esac
return
fi
# Greyscale
local r g b
if (( style > 231 )); then
r=$(( ((style-232) * 11) + 1 ))
g=$r
b=$r
else
# All other colours:
# 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
# See http://stackoverflow.com/a/27165165/5353461
# That makes each be in the range 0..5, and we need to convert to 0..255
r=$(( 51 * (style-16) / 36 ))
g=$(( 51 * ((style-16) % 36) / 6 ))
b=$(( 51 * (style-16) % 6 ))
fi
_color-inverse-rgb "$type" $r $g $b
}
_color-inverse-hex() {
local type=$1
local hex=$2
local r g b
if [[ ${#hex} = 3 ]]; then
r="${hex:0:1}${hex:0:1}"
g="${hex:1:1}${hex:1:1}"
b="${hex:2:1}${hex:2:1}"
else
r=${hex:0:2}
g=${hex:2:2}
b=${hex:4:2}
fi
_color-inverse-rgb "$type" $((16#$r)) $((16#$g)) $((16#$b))
}
_color-inverse-rgb() {
local type=$1
local r=$2
local g=$3
local b=$4
local luminance=$(( ($r * 299) + ($g * 587) + ($b * 114) ))
# Calculate percieved brightness
# See https://www.w3.org/TR/AERT#style-contrast and http://www.itu.int/rec/R-REC-BT.601
# Luminance range is 0..255000
local cutoff=127500
if [[ $type = 'bg' ]]; then
# Adjusted for the background because we want a dark background as much as possible
cutoff=50000
fi
local dark='black'
local light='lwhite'
if [[ $type = 'bg' ]]; then
light='white'
fi
if (( luminance > cutoff )); then
echo "$dark"
else
echo "$light"
fi
}