-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext
executable file
·390 lines (303 loc) · 10.1 KB
/
text
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
#!/usr/bin/env bash
# shellcheck disable=SC2059
# Functions: Text
[[ "${_}" != "${0}" && "${SOURCE_VERBOSE}" == "true" ]] && output keyval "Loaded" "${BASH_SOURCE[@]/$HOME\/\.kdm\//}"
# Flash full terminal screen (reverse video) until a key is pressed
#
# https://en.wikipedia.org/wiki/ANSI_escape_code#Example_of_use_in_shell_scripting
# https://stackoverflow.com/a/25845393
_flash_term() {
# Declare vars as local
local FLASH_COUNT="0" FLASH_COUNT_LIMIT="${2-5}" FLASH_INTERVAL="${1-0.5}"
# Some sanity checking
[[ "$(awk "BEGIN { print (${FLASH_INTERVAL} < .05) ? \"Y\" : \"N\" }")" == "Y" ]] && FLASH_INTERVAL=".05"
[[ "$(awk "BEGIN { print (${FLASH_INTERVAL} > 1) ? \"Y\" : \"N\" }")" == "Y" ]] && FLASH_INTERVAL="1"
# There are two waits during the loop, so we need to halve the provided flash interval time
FLASH_INTERVAL="$(echo "scale=4; ${FLASH_INTERVAL}/2" | bc)"
while true; do
if [[ "${FLASH_COUNT}" -ge "${FLASH_COUNT_LIMIT}" ]]; then
# Reset video
printf '\e[?5l'
return
fi
# Reverse video
printf '\e[?5h'
# Wait for ${FLASH_INTERVAL} time, and break if a key is pressed
read -n 1 -r -s -t "${FLASH_INTERVAL}" && break
# Reset video
printf '\e[?5l'
# Wait for ${FLASH_INTERVAL} time, and break if a key is pressed
read -n 1 -r -s -t "${FLASH_INTERVAL}" && break
((FLASH_COUNT++))
done
# Reset video
printf '\e[?5l'
return
}
# Output formatted text
# I would have used an associative array (and in one version, I did),
# but it's only in bash 4+ which is ... more rarely seen than I'd like
output() {
# Declare vars as local
local COLOR_SELECTED OUTPUT_FORMAT OUTPUT_STDERR OUTPUT_STRING
# Case statement for output color/format
case "${1,,}" in
# Basic colors
a|alert) COLOR_SELECTED="${C_ALT}";;
blk|black) COLOR_SELECTED="${C_BLK}";;
blu|blue) COLOR_SELECTED="${C_BLU}";;
c|cyn|cyan) COLOR_SELECTED="${C_CYN}";;
gry|gray) COLOR_SELECTED="${C_GRY}";;
grn|green) COLOR_SELECTED="${C_GRN}";;
o|orn|orange) COLOR_SELECTED="${C_ORN}";;
pk|pnk|pink) COLOR_SELECTED="${C_PNK}";;
pr|prp|purple) COLOR_SELECTED="${C_PRP}";;
r|red) COLOR_SELECTED="${C_RED}";;
w|wht|white) COLOR_SELECTED="${C_WHT}";;
y|ylw|yellow) COLOR_SELECTED="${C_YLW}";;
b|backtick)
# Output the input string with backticks (for Slack, mainly)
OUTPUT_FORMAT="${C_CODE}${C_BLD}\`%s\`${C_RST}\\n"
# Copy string to clipboard if not connected via SSH and binary is present
[[ -z "${SSH_CONNECTION}" ]] && hash pbcopy && printf "\`%s\`" "${2}" | pbcopy
output stderr "Copied string to clipboard"
;;
e|error)
# Special function to output formatted usage string, with stderr output
OUTPUT_FORMAT="${C_RED_BRT_BLD}${C_ITA}Error${C_RST} ${C_WHT_BRT}:${C_RST} ${C_RED}%s${C_RST}\\n"
OUTPUT_STDERR="1>&2"
;;
ef|error_fail|error-fail)
# Special function to output formatted usage string, with stderr output
OUTPUT_FORMAT="${C_RED_BRT_BLD}${C_ITA}Error${C_RST} ${C_WHT_BRT}:${C_RST} ${C_RED}Failed %s, cannot continue${C_RST}\\n"
OUTPUT_STDERR="1>&2"
;;
f|failure)
# Special function for failure message, no string input
COLOR_SELECTED="${C_RED_BRT_BLD}"
OUTPUT_STRING="failure"
;;
ky|kvy|keyval-y|k|kv|keyval)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_YLW}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
ka|kva|keyval-a)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_ALT}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kb|kvb|keyval-b)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_BLU}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kc|kvc|keyval-c)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_CYN}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kf|kvf|keyval-f|kr|kvr|keyval-r)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_RED}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
ko|kvo|keyval-o)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_ORN}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kp|kvp|keyval-p)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_PRP}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kpn|kvpn|keyval-pn)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_PNK}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
kg|kvg|keyval-g|kt|kvt|keyval-t)
# Check for 2nd and 3rd args
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
OUTPUT_FORMAT="${C_BLU}%s${C_WHT_BRT} : ${C_GRY}'${C_GRN}%s${C_GRY}'${C_RST}\\n"
# Output special format and immediately return
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
l|lu|leadup) # Special function for leadup to status message, no newline
# Create dot string and output format
printf -v LOADING_DOT_STRING '%*s' $((70-${#2}-${#3})) ''
OUTPUT_FORMAT="${C_RST}Performing ${C_GRY}'${C_ORN_BRT}%s${C_GRY}'${C_RST} ${C_WHT_BRT}$(printf '%s' "${LOADING_DOT_STRING// /.}") ${C_RST}"
;;
lc|luc|leadup-custom) # Special function for leadup to status message, no newline, with custom command name
# Check for 2nd and 3rd args
[[ -z "${2}" || -z "${3}" ]] && output usage "${USAGE_STRING}" && return
if [[ -z "${2}" || -z "${3}" ]]; then
output usage "${USAGE_STRING}"
return
fi
# Create dot string and output format
printf -v LOADING_DOT_STRING '%*s' $((70-${#2}-${#3})) ''
OUTPUT_FORMAT="${C_RST}%s ${C_GRY}'${C_ORN_BRT}%s${C_GRY}'${C_RST} ${C_WHT_BRT}$(printf '%s' "${LOADING_DOT_STRING// /.}") ${C_RST}"
printf "${OUTPUT_FORMAT}" "${2}" "${3}"
return
;;
2|se|stderr)
# Special function to output to stderr
COLOR_SELECTED="${C_BLU}"
OUTPUT_STDERR="1>&2"
;;
s|success)
# Special function for success message, no string
COLOR_SELECTED="${C_GRN_BLD}"
OUTPUT_STRING="success"
;;
u|usg|usage)
# Special function to output formatted usage string, with stderr output
OUTPUT_FORMAT="${C_YLW_BLD}${C_ITA}Usage${C_RST} ${C_WHT_BRT}:${C_RST} %s\\n"
OUTPUT_STDERR="1>&2"
;;
*) COLOR_SELECTED="${C_RST}";; # If nothing matches, reset color
esac
# Check for variables and throw usage message if issue
if [[ -z "${OUTPUT_STRING-${2}}" ]]; then
_output_usage
return
fi
# Finalize vars
OUTPUT_FORMAT="${OUTPUT_FORMAT-${COLOR_SELECTED}%s${C_RST}\n}"
OUTPUT_STRING="${OUTPUT_STRING-${2}}"
# Output to stdout or stderr based on switch
[[ "${OUTPUT_STDERR}" ]] && printf "${OUTPUT_FORMAT}" "${OUTPUT_STRING}" 1>&2 && return
printf "${OUTPUT_FORMAT}" "${OUTPUT_STRING}"
}
# Function "alias" for use in scripts
function o() {
output "${@}"
}
_output_usage() {
# Declare vars as local
local ARRAY_USAGE
# Array of arrays options
declare -A ARRAY_USAGE='(
[0]="alert error error-fail usage"
[1]="black blue orange cyan gray green orange pink purple red white yellow"
[2]="leadup leadup-custom"
[3]="failure success"
[4]="keyval keyval-a keyval-b keyval-c keyval-f keyval-o keyval-p keyval-pn keyval-r keyval-t"
[5]="backtick stderr"
)'
output usage "output <format> <string 1> [<string 2, for keyval options>]"; echo
output text "Format : < ${ARRAY_USAGE[0]} >"
output text " < ${ARRAY_USAGE[1]} >"
output text " < ${ARRAY_USAGE[2]} >"
output text " < ${ARRAY_USAGE[3]} >"
output text " < ${ARRAY_USAGE[4]} >"
output text " < ${ARRAY_USAGE[5]} >"
}
# bash tab-completion for output function
_output_completion() {
# Declare vars as local
local ARRAY_USAGE_OPTIONS CUR_WORD
# Array of options
ARRAY_USAGE_OPTIONS=(
alert
error error-fail
usage
black blue cyan gray green orange pink purple red white yellow
leadup leadup-custom
failure success
keyval
keyval-a
keyval-b
keyval-c
keyval-f
keyval-g
keyval-o
keyval-p
keyval-pn
keyval-r
keyval-t
keyval-y
backtick stderr
)
CUR_WORD=${COMP_WORDS[COMP_CWORD]}
mapfile -t COMPREPLY < <(compgen -W "${ARRAY_USAGE_OPTIONS[*]}" -- "${CUR_WORD}")
}
# bash tab-completion for shortened o function
_o_completion() {
# Declare vars as local
local ARRAY_USAGE_OPTIONS CUR_WORD
# Array of options
ARRAY_USAGE_OPTIONS=(
a e ef u
blk blu c gry grn o pk pr r w y
l lc
f s
k ka kb kc kf ko kp kpn kr kt
b 2
)
CUR_WORD=${COMP_WORDS[COMP_CWORD]}
mapfile -t COMPREPLY < <(compgen -W "${ARRAY_USAGE_OPTIONS[*]}" -- "${CUR_WORD}")
}
# Configure/enable completion
complete -F _output_completion output
complete -F _o_completion o
# Function aliases
alias flash-term=' _flash_term'
alias flash-terminal=' _flash_term'
alias term-flash=' _flash_term'
alias terminal-flash=' _flash_term'
# vim: set filetype=sh ts=2 sw=2 tw=0 noet :