Repository navigation
Expand file tree
/
Copy pathtint
More file actions
395 lines (371 loc) · 16 KB
/
Copy pathtint
File metadata and controls
395 lines (371 loc) · 16 KB
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
395
#!/usr/bin/env bash
#=========================================================================================
# HEADER
#=========================================================================================
# IMPLEMENTATION
#
# title tint
# description The ANSI/VT100 terminals and terminal emulators can display colors
# and formatted text using escape sequences. Instead of using the raw
# ANSI escape sequences, we use the tput command which is ensures
# portability between various terminals and terminal emulators.
# version 0.3
# author Djordje Ungar <https://www.djordjeungar.com/>
# collaborator Heitor Moreira da Silva <heitormoreira@yahoo.com.br>
# copyright Copyright (c) 2018 ArtBIT
# license MIT License
#
#=========================================================================================
# HISTORY
# 2018-04-19 : ArtBIT : Script creation
# 2020-03-01 : heitormsilva : Add -c and -l options and improvements
#
#=========================================================================================
# DEBUG OPTION
# set -n # Uncomment to check your syntax, without execution.
# set -x # Uncomment to debug this shell script
#
#=========================================================================================
# END_OF_HEADER
#=========================================================================================
# Returns the position of the matching closing bracket in the string
# Note: $2 specifies the position of a open bracket to find the match for
_bash_tint__get_closing_bracket_position() {
local text="$1"
local position=${2:-0}
local counter=0
local ch prev_ch
local closing_bracket_position=
for (( j=position; j<${#text}; j++ )); do
ch="${text:$j:1}"
if [[ "$ch" == '(' ]]; then
((counter++))
elif [[ "$ch" == ')' ]] && [[ "$prev_ch" != '\' ]]; then
((counter--))
fi
if [[ $counter -eq 0 ]]; then
closing_bracket_position=$j
return $j
fi
prev_ch="$ch"
done
return 0
}
_debug() {
[[ -z "$DEBUG" ]] && return
echo >&2 "$@"
}
# Echoes the resulting transform
_bash_tint__parse() {
local text="$1"
local context
eval "declare -A context="${2#*=}
local i char word cmd result content
word_char_regex="[a-zA-Z0-9_]"
word=""
result=""
for (( i=0; i<"${#text}"; i++ )); do
char="${text:$i:1}"
if [[ "$char" =~ $word_char_regex ]]; then
word="${word}${char}"
else
if [[ ! -z "$word" ]]; then
if [[ "$char" == '(' ]]; then
_bash_tint__get_closing_bracket_position "${text:$i}"
local closing_bracket_position=$?
if [[ $closing_bracket_position -gt 0 ]]; then
# copy context
context_string=$(declare -p context)
eval "declare -A child_context="${context_string#*=}
content="${text:$(( i + 1)):$(( closing_bracket_position - 1 ))}"
ctx=
cmd=
# re-match brackets for commands that return functions, i.e. color(123)(text)
rematch=
case "$word" in
# Text effects
bold) cmd="bold" ctx="$word" ;;
dim) cmd="dim" ctx="$word" ;;
underline) cmd="smul" ctx="$word" ;;
standout) cmd="smso" ctx="$word" ;;
blink) cmd="blink" ctx="$word" ;;
invert) cmd="rev" ctx="$word" ;;
hidden) cmd="invis" ctx="$word" ;;
# Foreground colors
light_red) cmd="setaf 9" ctx="fg" ;;
light_green) cmd="setaf 10" ctx="fg" ;;
light_yellow) cmd="setaf 228" ctx="fg" ;;
light_blue) cmd="setaf 12" ctx="fg" ;;
light_magenta) cmd="setaf 13" ctx="fg" ;;
light_cyan) cmd="setaf 14" ctx="fg" ;;
light_gray | light_grey)
cmd="setaf 15" ctx="fg" ;;
light_orange) cmd="setaf 11" ctx="fg" ;;
black) cmd="setaf 0" ctx="fg" ;;
red) cmd="setaf 1" ctx="fg" ;;
green) cmd="setaf 2" ctx="fg" ;;
yellow) cmd="setaf 226" ctx="fg" ;;
blue) cmd="setaf 4" ctx="fg" ;;
magenta) cmd="setaf 5" ctx="fg" ;;
cyan) cmd="setaf 6" ctx="fg" ;;
white) cmd="setaf 7" ctx="fg" ;;
gray | grey) cmd="setaf 8" ctx="fg" ;;
orange) cmd="setaf 3" ctx="fg" ;;
# Background colors
Light_red) cmd="setab 9" ctx="bg" ;;
Light_green) cmd="setab 10" ctx="bg" ;;
Light_yellow) cmd="setab 228" ctx="bg" ;;
Light_blue) cmd="setab 12" ctx="bg" ;;
Light_magenta) cmd="setab 13" ctx="bg" ;;
Light_cyan) cmd="setab 14" ctx="bg" ;;
Light_gray | Light_grey)
cmd="setab 15" ctx="bg" ;;
Light_orange) cmd="setab 11" ctx="bg" ;;
Black) cmd="setab 0" ctx="bg" ;;
Red) cmd="setab 1" ctx="bg" ;;
Green) cmd="setab 2" ctx="bg" ;;
Yellow) cmd="setab 226" ctx="bg" ;;
Blue) cmd="setab 4" ctx="bg" ;;
Magenta) cmd="setab 5" ctx="bg" ;;
Cyan) cmd="setab 6" ctx="bg" ;;
White) cmd="setab 7" ctx="bg" ;;
Gray | Grey) cmd="setab 8" ctx="bg" ;;
Orange) cmd="setab 3" ctx="bg" ;;
# Commands
reset) cmd="sgr0" ;;
clear) cmd="clear" ;;
save_cursor) cmd="sc" ;;
restore_cursor) cmd="rc" ;;
move_cursor)
IFS=$',' read -r X Y <<< "$content"
cmd="cup $Y $X"
;;
color)
if [[ "$content" =~ ^[0-9]+$ ]]; then
cmd="setaf $content"
ctx="fg"
rematch=1
fi
;;
Color)
if [[ "$content" =~ ^[0-9]+$ ]]; then
cmd="setab $content"
ctx="bg"
rematch=1
fi
;;
esac
if [ ! -z "$cmd" ]; then
(( i+=closing_bracket_position ))
if [[ ! -z "$ctx" ]]; then
if [[ ! -z "$rematch" ]]; then
content="${text:$(( i + 1))}"
_bash_tint__get_closing_bracket_position "$content"
closing_bracket_position=$?
content="${content:1:$(( closing_bracket_position - 1 ))}"
(( i+=closing_bracket_position + 1))
fi
# reset all contexts then set all the active contexts
local tputargs="sgr0"
for context_name in fg bg bold dim underline standout blink invert hidden; do
if [ ! -z "${context[$context_name]}" ]; then
tputargs="${tputargs}\n${context[$context_name]}"
fi
done
child_context["$ctx"]="$cmd"
content="$(_bash_tint__parse "$content" "$(declare -p child_context)")"
# if the sub content ends with a context reset, we truncate it, since the same thing is
# going to happen in this step as well
# sed 's,\x1B(B\x1B\[m\(\x1B\[[^m]\+m\)\+$,,g'
re="(.*)\(B\[m(\[[^m]+m)*$";
while [[ "$content" =~ $re ]]; do
content="${BASH_REMATCH[1]}"
done
if [ "$clearText" != "true" ]; then
result="${result}$(tput $cmd)${content}$(printf -- "$tputargs" | tput -S)"
else
result="${result}${content}"
fi
else
# No need to reset context
if [ "$clearText" != "true" ]; then
result="${result}$(tput $cmd)"
fi
fi
fi
else
result="${result}${word}${char}"
fi
word=
char=
fi
result="${result}${word}${char}"
word=""
else
result="${result}${char}"
word=""
fi
fi
done
result="${result}${word}"
printf -- "$result"
}
_bash_tint__exec_command() {
local cmd=
local content="$2"
}
_bash_tint__logo() {
colors=(Red Green Yellow Magenta Blue Cyan Orange)
regex="#"
while IFS= read -r line; do
while true; do
if ! [[ "$line" =~ $regex ]]; then
break;
fi
color="${colors[$RANDOM % ${#colors[@]}]}"
line=${line/\#\#/$color( )}
done
tint "$line"
done < <(cat <<-'EOF'
########## ###### #### ## ##########
## ## ## ## ## ##
## ## ## #### ##
## ###### ## ## ##
EOF
)
}
tint_usage() {
usage=$(cat <<"HELP_USAGE"
Usage: tint [-c -n] <string>
Options:
-c Remove all tint tag marks from the text and
returns it cleared.
-l List available colours codes to be used in
color\(123)(text) format.
-n Do not put a new line character at the end of text.
String format: String can contain one or more tint functions.
Functions: Result:
Effects:
bold\(text) bold(text)
underline\(text) underline(text)
invert\(text) invert(text)
blink\(text) blink(text)
hidden\(text) hidden(text)
Foreground colors:
black\(text) black(text)
red\(text) red(text)
green\(text) green(text)
yellow\(text) yellow(text)
orange\(text) orange(text)
blue\(text) blue(text)
magenta\(text) magenta(text)
cyan\(text) cyan(text)
white\(text) white(text)
gray\(text) gray(text)
light_red\(text) light_red(text)
light_green\(text) light_green(text)
light_yellow\(text) light_yellow(text)
light_orange\(text) light_orange(text)
light_blue\(text) light_blue(text)
light_magenta\(text) light_magenta(text)
light_cyan\(text) light_cyan(text)
light_gray\(text) light_gray(text)
Background colors:
Black\(text) Black(text)
Red\(text) Red(text)
Green\(text) Green(text)
Yellow\(text) Yellow(text)
Orange\(text) Orange(text)
Blue\(text) Blue(text)
Magenta\(text) Magenta(text)
Cyan\(text) Cyan(text)
White\(text) White(text)
Gray\(text) Gray(text)
Light_red\(text) Light_red(text)
Light_green\(text) Light_green(text)
Light_yellow\(text) Light_yellow(text)
Light_orange\(text) Light_orange(text)
Light_blue\(text) Light_blue(text)
Light_magenta\(text) Light_magenta(text)
Light_cyan\(text) Light_cyan(text)
Light_gray\(text) Light_gray(text)
Example:
tint "white\(Cyan\(T)Magenta\(I)Yellow\(N)Black\(T)) is fun!"
Outputs:
bold(Cyan(T)Magenta(I)Yellow(N)Black(T)) is fun!
HELP_USAGE
)
_bash_tint__logo
tint "$usage"
}
listColours() {
tput init
local end=$(( $(tput colors)-1 ))
local w=8
for c in $(seq 0 $end); do
eval "$(printf "tput setaf %3s " "$c")"; echo -n "$_"
[[ $c -ge $(( w*2 )) ]] && offset=2 || offset=0
[[ $(((c+offset) % (w-offset))) -eq $(((w-offset)-1)) ]] && echo
done
tput init
}
tint() {
if [ "$1" == "--help" ]; then
tint -h
return
fi
local clearText="false"
local newLine="true"
OPTIND=1 #reset getops index
while getopts ":chln" opt $@; do
case $opt in
c)
clearText=true
;;
h)
tintf "$@"
return
;;
l)
listColours
return
;;
n)
newLine="false"
;;
\?)
echo "Invalid option: '-${OPTARG}'" >&2
#exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
#exit 1
;;
esac
shift $((--OPTIND))
done
if [ "$newLine" = "true" ]; then
tintf "$@"
printf "\n"
else
tintf "$@"
fi
}
tintf() {
if [ "$1" == "-h" -o "$1" == "--help" ]; then
tint_usage
return
fi
# trailing newlines are removed with command substitution, to circumvent this, we add a trailing dot to the input string
# and then remove the last character from the result string
input="$1."
shift
# This is the initial context
declare -A context
context=( ["fg"]= ["bg"]= ["bold"]= ["dim"]= ["underline"]= ["standout"]= ["blink"]= ["invert"]= ["hidden"]= )
result="$(_bash_tint__parse "$(printf -- "$input" "${@}")" "$(declare -p context)")"
result="${result//\\(/(}"
result="${result//\\)/)}"
printf -- "${result::-1}"
}
tint "${@}"