-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate
executable file
·394 lines (334 loc) · 13.8 KB
/
generate
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit
# https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
# ╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
# ║ Code ║ Effect ║ Note ║
# ╠══════════╬════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
# ║ 0 ║ Reset / Normal ║ all attributes off ║
# ║ 1 ║ Bold or increased intensity ║ ║
# ║ 2 ║ Faint (decreased intensity) ║ Not widely supported. ║
# ║ 3 ║ Italic ║ Not widely supported. Sometimes treated as inverse. ║
# ║ 4 ║ Underline ║ ║
# ║ 5 ║ Slow Blink ║ less than 150 per minute ║
# ║ 6 ║ Rapid Blink ║ MS-DOS ANSI.SYS; 150+ per minute; not widely supported ║
# ║ 7 ║ [[reverse video]] ║ swap foreground and background colors ║
# ║ 8 ║ Conceal ║ Not widely supported. ║
# ║ 9 ║ Crossed-out ║ Characters legible, but marked for deletion. Not widely supported. ║
# ║ 10 ║ Primary(default) font ║ ║
# ║ 11–19 ║ Alternate font ║ Select alternate font `n-10` ║
# ║ 20 ║ Fraktur ║ hardly ever supported ║
# ║ 21 ║ Bold off or Double Underline ║ Bold off not widely supported; double underline hardly ever supported. ║
# ║ 22 ║ Normal color or intensity ║ Neither bold nor faint ║
# ║ 23 ║ Not italic, not Fraktur ║ ║
# ║ 24 ║ Underline off ║ Not singly or doubly underlined ║
# ║ 25 ║ Blink off ║ ║
# ║ 27 ║ Inverse off ║ ║
# ║ 28 ║ Reveal ║ conceal off ║
# ║ 29 ║ Not crossed out ║ ║
# ║ 30–37 ║ Set foreground color ║ See color table below ║
# ║ 38 ║ Set foreground color ║ Next arguments are `5;n` or `2;r;g;b`, see below ║
# ║ 39 ║ Default foreground color ║ implementation defined (according to standard) ║
# ║ 40–47 ║ Set background color ║ See color table below ║
# ║ 48 ║ Set background color ║ Next arguments are `5;n` or `2;r;g;b`, see below ║
# ║ 49 ║ Default background color ║ implementation defined (according to standard) ║
# ║ 51 ║ Framed ║ ║
# ║ 52 ║ Encircled ║ ║
# ║ 53 ║ Overlined ║ ║
# ║ 54 ║ Not framed or encircled ║ ║
# ║ 55 ║ Not overlined ║ ║
# ║ 60 ║ ideogram underline ║ hardly ever supported ║
# ║ 61 ║ ideogram double underline ║ hardly ever supported ║
# ║ 62 ║ ideogram overline ║ hardly ever supported ║
# ║ 63 ║ ideogram double overline ║ hardly ever supported ║
# ║ 64 ║ ideogram stress marking ║ hardly ever supported ║
# ║ 65 ║ ideogram attributes off ║ reset the effects of all of 60-64 ║
# ║ 90–97 ║ Set bright foreground color ║ aixterm (not in standard) ║
# ║ 100–107 ║ Set bright background color ║ aixterm (not in standard) ║
# ╚══════════╩════════════════════════════════╩═════════════════════════════════════════════════════════════════════════╝
readlinkf() { # Modified from https://github.com/ko1nksm/readlinkf
[ "${1:-}" ] || return 1
CDPATH='' # to avoid changing to an unexpected directory
local max_symlinks=40
local link
local target=$1
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
[ -d "${target:-/}" ] && target="$target/"
cd -P . 2>/dev/null || return 1
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
if [ ! "$target" = "${target%/*}" ]; then
case $target in
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
esac
target=${target##*/}
fi
if [ ! -L "$target" ]; then
target="${PWD%/}${target:+/}${target}"
printf '%s\n' "${target:-/}"
return 0
fi
link=$(ls -dl -- "$target" 2>/dev/null) || break
target=${link#*" $target -> "}
done
return 1
}
SCRIPT_PATH="$(readlinkf "$0")"
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
# shellcheck source=deps/lobash.bash
source "$SCRIPT_DIR"/deps/lobash.bash
declare -A opts=()
declare -a args=()
usage() {
cat <<EOF
Usage: $0 [-h|--help] [-p=<prefix>] [-e=<escaped_suffix>] [-a] [<output>=./colors.bash]
Options:
-h, --help Show usage.
-y If set, force write the colors file
-p=<prefix> If set, Add prefix '<prefix>' to each name of exported variables.
-e, -e=<escaped_suffix> If set, export escaped variables instead of general variables. And add suffix '<escaped_suffix>' to each name of escaped variables.
-a If set, export both general variables and escaped variables.
--map If set, export a colors map instead of color lists
EOF
}
init() {
l.parse_params opts args "$@"
if [[ ${opts[h]:-} == true ]] || [[ ${opts[help]:-} == true ]]; then
usage
exit 0
fi
declare -g TARGET EXPORT_GENERAL=false EXPORT_ESCAPED=false
TARGET=${args[0]:-colors.bash}
if [[ -d $TARGET ]]; then
TARGET=${TARGET}/colors.bash
fi
# User Specified Environment Variables
PREFIX=${opts[p]:-}
ESCAPED_SUFFIX=${opts[e]:-}
if [[ -n ${opts[a]:-} ]]; then
EXPORT_GENERAL=true
EXPORT_ESCAPED=true
if [[ -z $ESCAPED_SUFFIX ]] || [[ $ESCAPED_SUFFIX == true ]]; then ESCAPED_SUFFIX='_ESC'; fi
else
if [[ -n $ESCAPED_SUFFIX ]]; then
if [[ $ESCAPED_SUFFIX == true ]]; then ESCAPED_SUFFIX=''; fi
EXPORT_ESCAPED=true
else
EXPORT_GENERAL=true
fi
fi
declare -g BASE_COLORS=(
BLACK
RED
GREEN
YELLOW
BLUE
PURPLE
CYAN
WHITE
)
declare -ga BG_COLORS
local COLOR
for COLOR in "${BG_COLORS[@]}"; do
BG_COLORS+=("$COLOR")
done
local -a attrs=(
RESET 0
BOLD 1
# FAINT 2
# ITALIC 3
UNDERLINE 4
BLINK 5
# RAPID_BLINK 6
REVERSE 7
# CONCEAL 8
# CROSSED_OUT 9
BLACK 30
RED 31
GREEN 32
YELLOW 33
BLUE 34
PURPLE 35
CYAN 36
WHITE 37
RESET_FG 39
BLACK_BG 40
RED_BG 41
GREEN_BG 42
YELLOW_BG 43
BLUE_BG 44
PURPLE_BG 45
CYAN_BG 46
WHITE_BG 47
RESET_BG 49
GREY 90
)
local len=${#attrs[@]}
local i
for (( i = 0; i < "$len"; i+=2 )); do
declare -g "${attrs[$i]}"
read -r "${attrs[$i]}" <<< "${attrs[$((i+1))]}"
done
}
_BG() {
printf '%s' $(($1 + 10))
}
_BRIGHT() {
printf '%s' $(($1 + 60))
}
# Valid Formats:
# "${ESC}[{FG_COLOR}m"
# "${ESC}[{FG_COLOR};{ATTR}m"
# "${ESC}[{FG_COLOR};{BG_COLOR};{ATTR}m"
ansi() {
local ESC='\e'
# local ESC=$'\033'
# local ESC=$'\x1B'
# local ESC=$'\u001b'
# Control Sequence Introducer
local CSI="${ESC}["
# Operating System Command
local OSC="${ESC}]"
# String Terminator
local ST="${ESC}\\"
local c name
local str=''
for c in "$@"; do
if [[ $c =~ ^[0-9]+$ ]]; then
str="${str};${c}"
else
name=${c}
str="${str};${!name}"
fi
done
printf '%s%sm' "$CSI" "${str:1}"
}
safe_rm() {
local path=$1
if [[ $path == / ]]; then
echo "Dangerous! Do not rm $path"
return 3
fi
if [[ $(dirname "$path") == / ]]; then
echo "Dangerous! Do not rm $path"
return 3
fi
rm -rf "$path"
}
clean() {
if [[ ${opts[y]:-} != 'true' ]]; then
if [[ -f $TARGET ]]; then
local answer
answer=$(l.ask "Existed file: ${TARGET}. Overwrite it?" N)
echo "$answer"
case $answer in
YES )
;;
* )
echo "Not overwrite it. No color file will be generated."
exit 0
;;
esac
fi
fi
safe_rm "$TARGET"
}
write() {
printf -- '%s\n' "$*" >> "$TARGET"
}
write_color() {
if [[ $EXPORT_GENERAL == true ]]; then
printf -- "%s='%s'\n" "${PREFIX}$1" "$2" >> "$TARGET"
fi
if [[ $EXPORT_ESCAPED == true ]]; then
printf -- "%s=$'%s'\n" "${PREFIX}$1${ESCAPED_SUFFIX}" "$2" >> "$TARGET"
fi
}
write_comment_title() {
printf -- '\n# %s\n' "$*" >> "$TARGET"
}
general_colors() {
local color_name_prefix=${1:+$1_}
(( $# > 0 )) && shift
local colorNum op
for color in "${BASE_COLORS[@]}"; do
colorNum=${!color}
local ops=()
for op in "$@"; do
if [[ $op =~ ^_ ]]; then
if [[ $op == _BRIGHT ]]; then
colorNum="$($op "${colorNum}")"
else
ops+=( "$($op "${colorNum}")" )
fi
else
ops+=( "$op" )
fi
done
write_color "${color_name_prefix}$color" "$(ansi "$colorNum" "${ops[@]}")"
done
}
general_colors_list() {
write_comment_title 'General Foreground Colors'
general_colors
write_color GREY "$(ansi GREY)"
write_comment_title 'General Background Colors'
general_colors BG _BG
write_color BG_GREY "$(ansi "$(_BG "$GREY")")"
write_comment_title 'BOLD'
general_colors BOLD BOLD
write_color BOLD_GREY "$(ansi BOLD GREY)"
write_comment_title 'UNDERLINE'
general_colors UL UNDERLINE
write_color UL_GREY "$(ansi UNDERLINE GREY)"
write_comment_title 'BLINK'
general_colors BLK BLINK
write_color BLK_GREY "$(ansi BLINK GREY)"
write_comment_title 'REVERSE'
general_colors REV REVERSE
write_color REV_GREY "$(ansi REVERSE GREY)"
write_comment_title 'BRIGHT'
general_colors BRI _BRIGHT
write_comment_title 'BRIGHT & BOLD'
general_colors BRI_BOLD _BRIGHT BOLD
write_comment_title 'RESET'
write_color "RESET_FG" "$(ansi RESET_FG)"
write_color "RESET_BG" "$(ansi RESET_BG)"
write_color "RESET_ALL" "$(ansi RESET)"
}
# shellcheck disable=2120
general_colors_map() {
write_comment_title() {
printf -- '\n # %s\n' "$*" >> "$TARGET"
}
write_color() {
if [[ $EXPORT_GENERAL == true ]]; then
printf -- " [%s]='%s'\n" "$1" "$2" >> "$TARGET"
fi
if [[ $EXPORT_ESCAPED == true ]]; then
printf -- " [%s]=$'%s'\n" "$1${ESCAPED_SUFFIX}" "$2" >> "$TARGET"
fi
}
write ""
write "declare -g -A ${PREFIX}colors=("
general_colors_list
write ')'
}
generate() {
write '# This file is generated by https://github.com/adoyle-h/shell-general-colors'
write "# Command: shell-general-colors${*:+ $*}"
write '# Author: ADoyle <[email protected]>'
write '# License: BSD 3-clause License'
write '# Attentions: GREY may not work in some shells'
if [[ ${opts[map]:-} == true ]]; then
general_colors_map
else
general_colors_list
fi
echo "Generated file: $TARGET"
}
init "$@"
clean
generate "$@"