-
-
Notifications
You must be signed in to change notification settings - Fork 674
/
utils.sh
454 lines (400 loc) · 12.6 KB
/
utils.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#! bash oh-my-bash.module
############################---Description---###################################
# #
# Summary : A collection of handy utilities and functions for bash #
# Support : destro.nnt@gmail.com #
# Created date : Mar 18,2017 #
# Latest Modified date : Mar 18,2017 #
# #
################################################################################
############################---Usage---#########################################
# source ~/path/to/directory/utils.sh
########################## Styled text output ##################################
# e_header "I am a sample script"
# e_success "I am a success message"
# e_error "I am an error message"
# e_warning "I am a warning message"
# e_underline "I am underlined text"
# e_bold "I am bold text"
# e_note "I am a note"
################# Performing simple Yes/No confirmations #######################
# seek_confirmation "Do you want to print a success message?"
# if is_confirmed; then
# e_success "Here is a success message"
# else
# e_error "You did not ask for a success message"
# fi
############ Testing if packages, apps, gems, etc. are installed ###############
# if _omb_util_command_exists 'git'; then
# e_success "Git good to go"
# else
# e_error "Git should be installed. It isn't. Aborting."
# exit 1
# fi
# if is_os "darwin"; then
# e_success "You are on a mac"
# else
# e_error "You are not on a mac"
# exit 1
# fi
##################### Sending notifications to Pushover ########################
# pushover "We just finished performing a lengthy task."
############################### Comparing A List ###############################
# recipes=(
# A-random-package
# bash
# Another-random-package
# git
# )
# list="$(to_install "${recipes[*]}" "$(brew list)")"
# if [[ "$list" ]]; then
# for item in ${list[@]}
# do
# echo "$item is not on the list"
# done
# else
# e_arrow "Nothing to install. You've already got them all."
# fi
################################################################################
function _omb_util_setexit {
return "$1"
}
function _omb_util_defun_print {
builtin eval -- "function $1 { local $3; $2 \"\$@\" && printf '%s\n' \"\${$3}\"; }"
}
#
# Test whether a command---either an alias, a keyword, a function, a builtin,
# or a file---is defined.
#
# $1 = cmd to test
#
# Usage:
#
# if _omb_util_command_exists 'git'; then
# some action
# else
# some other action
# fi
#
if ((_omb_bash_version >= 40000)); then
function _omb_util_command_exists {
type -t -- "$@" &>/dev/null # bash-4.0
}
function _omb_util_binary_exists {
type -P -- "$@" &>/dev/null # bash-4.0
}
else
function _omb_util_command_exists {
while (($#)); do
type -t -- "$1" &>/dev/null || return 1
shift
done
}
function _omb_util_binary_exists {
while (($#)); do
type -P -- "$1" &>/dev/null || return 1
shift
done
}
fi
function _omb_util_function_exists {
declare -F "$@" &>/dev/null # bash-3.2
}
#
# Set Colors
#
# Use colors, but only if connected to a terminal, and that terminal
# supports them. These colors are intended to be used with `echo`
#
function _omb_term_color_initialize {
local name
local -a normal_colors=(black brown green olive navy purple teal silver)
local -a bright_colors=(gray red lime yellow blue magenta cyan white)
if [[ ! -t 1 ]]; then
_omb_term_colors=
_omb_term_bold=
_omb_term_underline=
_omb_term_reset=
_omb_term_normal=
_omb_term_reset_color=
for name in "${normal_colors[@]}" "${bright_colors[@]}" violet; do
printf -v "_omb_term_$name" ''
printf -v "_omb_term_background_$name" ''
printf -v "_omb_term_bold_$name" ''
printf -v "_omb_term_underline_$name" ''
done
return 0
fi
if _omb_util_binary_exists tput; then
_omb_term_colors=$(tput colors 2>/dev/null || tput Co 2>/dev/null)
_omb_term_bold=$(tput bold 2>/dev/null || tput md 2>/dev/null)
_omb_term_underline=$(tput smul 2>/dev/null || tput ul 2>/dev/null)
_omb_term_reset=$(tput sgr0 2>/dev/null || tput me 2>/dev/null)
else
_omb_term_colors=
_omb_term_bold=$'\e[1m'
_omb_term_underline=$'\e[4m'
_omb_term_reset=$'\e[0m'
fi
_omb_term_normal=$'\e[0m'
_omb_term_reset_color=$'\e[39m'
# normal colors
if ((_omb_term_colors >= 8)); then
local index
for ((index = 0; index < 8; index++)); do
local fg=$(tput setaf "$index" 2>/dev/null || tput AF "$index" 2>/dev/null)
[[ $fg ]] || fg=$'\e[3'$index'm'
printf -v "_omb_term_${normal_colors[index]}" %s "$fg"
printf -v "_omb_term_background_${normal_colors[index]}" '\e[4%sm' "$index"
done
else
local index
for ((index = 0; index < 8; index++)); do
printf -v "_omb_term_${normal_colors[index]}" '\e[3%sm' "$index"
printf -v "_omb_term_background_${normal_colors[index]}" '\e[4%sm' "$index"
done
fi
# bright colors
if ((_omb_term_colors >= 16)); then
local index
for ((index = 0; index < 8; index++)); do
local fg=$(tput setaf $((index+8)) 2>/dev/null || tput AF $((index+8)) 2>/dev/null)
[[ $fg ]] || fg=$'\e[9'$index'm'
local refbg=_omb_term_background_${normal_colors[index]}
local bg=${!refbg}$'\e[10'$index'm'
printf -v "_omb_term_${bright_colors[index]}" %s "$fg"
printf -v "_omb_term_background_${bright_colors[index]}" %s "$bg"
done
else
# copy normal colors to bright colors (with bold)
local index
for ((index = 0; index < 8; index++)); do
local reffg=_omb_term_${normal_colors[index]}
local refbg=_omb_term_background_${normal_colors[index]}
printf -v "_omb_term_${bright_colors[index]}" %s "$_omb_term_bold${!reffg}"
printf -v "_omb_term_background_${bright_colors[index]}" %s "$_omb_term_bold${!refbg}"
done
fi
# index colors
if ((_omb_term_colors == 256)); then
_omb_term_violet=$'\e[38;5;171m'
_omb_term_background_violet=$'\e[48;5;171m'
else
_omb_term_violet=$_omb_term_purple
_omb_term_background_violet=$_omb_term_background_purple
fi
# bold / underline versions
for name in "${normal_colors[@]}" "${bright_colors[@]}" violet; do
local ref=_omb_term_$name
printf -v "_omb_term_bold_$name" %s "$_omb_term_bold${!ref}"
printf -v "_omb_term_underline_$name" %s "$_omb_term_underline${!ref}"
done
}
_omb_term_color_initialize
#
# Headers and Logging
#
function _omb_log_header { printf "\n${_omb_term_bold}${_omb_term_violet}========== %s ==========${_omb_term_reset}\n" "$@"; }
function _omb_log_arrow { printf "➜ %s\n" "$@"; }
function _omb_log_success { printf "${_omb_term_green}✔ %s${_omb_term_reset}\n" "$@"; }
function _omb_log_error { printf "${_omb_term_brown}✖ %s${_omb_term_reset}\n" "$@"; }
function _omb_log_warning { printf "${_omb_term_olive}➜ %s${_omb_term_reset}\n" "$@"; }
function _omb_log_underline { printf "${_omb_term_underline}${_omb_term_bold}%s${_omb_term_reset}\n" "$@"; }
function _omb_log_bold { printf "${_omb_term_bold}%s${_omb_term_reset}\n" "$@"; }
function _omb_log_note { printf "${_omb_term_underline}${_omb_term_bold}${_omb_term_navy}Note:${_omb_term_reset} ${_omb_term_olive}%s${_omb_term_reset}\n" "$@"; }
#
# USAGE FOR SEEKING CONFIRMATION
# seek_confirmation "Ask a question"
# Credit: https://github.com/kevva/dotfiles
#
# if is_confirmed; then
# some action
# else
# some other action
# fi
#
function seek_confirmation {
printf "\\n${_omb_term_bold}%s${_omb_term_reset}" "$@"
read -rp " (y/n) " -n 1
printf "\\n"
}
# Test whether the result of an 'ask' is a confirmation
function is_confirmed {
[[ $REPLY =~ ^[Yy]$ ]]
}
#
# Test which OS the user runs
# $1 = OS to test
# Usage: if is_os 'darwin'; then
#
function is_os {
[[ $OSTYPE == $1* ]]
}
#
# Pushover Notifications
# Usage: pushover "Title Goes Here" "Message Goes Here"
# Credit: http://ryonsherman.blogspot.com/2012/10/shell-script-to-send-pushover.html
#
function pushover {
PUSHOVERURL="https://api.pushover.net/1/messages.json"
API_KEY=$PUSHOVER_API_KEY
USER_KEY=$PUSHOVER_USER_KEY
DEVICE=$PUSHOVER_DEVICE
TITLE="${1}"
MESSAGE="${2}"
curl \
-F "token=${API_KEY}" \
-F "user=${USER_KEY}" \
-F "device=${DEVICE}" \
-F "title=${TITLE}" \
-F "message=${MESSAGE}" \
"${PUSHOVERURL}" > /dev/null 2>&1
}
## @fn _omb_util_get_shopt optnames...
## @var[out] __shopt
if ((_omb_bash_version >= 40100)); then
function _omb_util_get_shopt { __shopt=$BASHOPTS; }
else
function _omb_util_get_shopt {
__shopt=
local opt
for opt; do
if shopt -q "$opt" &>/dev/null; then
__shopt=${__shopt:+$__shopt:}$opt
fi
done
}
fi
_omb_util_unload_hook=()
function _omb_util_unload {
local hook
for hook in "${_omb_util_unload_hook[@]}"; do
eval -- "$hook"
done
}
_omb_util_original_PS1=$PS1
# shellcheck disable=SC2016
_omb_util_unload_hook+=('PS1=$_omb_util_original_PS1')
_omb_util_prompt_command=()
function _omb_util_prompt_command_hook {
local status=$? lastarg=$_ hook
for hook in "${_omb_util_prompt_command[@]}"; do
_omb_util_setexit "$status" "$lastarg"
eval -- "$hook"
done
_omb_util_setexit "$status"
}
_omb_util_unload_hook+=('_omb_util_prompt_command=()')
: "${_omb_util_prompt_command_setup=}"
function _omb_util_add_prompt_command {
local other
for other in "${_omb_util_prompt_command[@]}"; do
[[ $1 == "$other" ]] && return 0
done
_omb_util_prompt_command+=("$1")
if [[ ! $_omb_util_prompt_command_setup ]]; then
_omb_util_prompt_command_setup=1
local hook=_omb_util_prompt_command_hook
# See if we need to use the overriden version
if _omb_util_function_exists append_prompt_command_override; then
append_prompt_command_override "$hook"
return
fi
# Set OS dependent exact match regular expression
local prompt_re
if [[ $OSTYPE == darwin* ]]; then
# macOS
prompt_re='[[:<:]]'$hook'[[:>:]]'
else
# Linux, FreeBSD, etc.
prompt_re='\<'$hook'\>'
fi
[[ $PROMPT_COMMAND =~ $prompt_re ]] && return 0
if ((_omb_bash_version >= 50100)); then
local other
for other in "${PROMPT_COMMAND[@]}"; do
[[ $hook == "$other" ]] && return 0
done
PROMPT_COMMAND+=("$hook")
else
PROMPT_COMMAND="$hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
fi
}
## @fn _omb_util_split array str [sep]
function _omb_util_split {
local __set=$- IFS=${3:-$' \t\n'}
set -f
eval -- "$1=(\$2)"
[[ $__set == *f* ]] || set +f
return 0
}
function _omb_util_glob_expand {
local __set=$- __shopt __gignore=$GLOBIGNORE
_omb_util_get_shopt failglob nullglob extglob
shopt -u failglob
shopt -s nullglob
shopt -s extglob
set +f
GLOBIGNORE=
eval -- "$1=($2)"
GLOBIGNORE=$__gignore
# Note: dotglob is changed by GLOBIGNORE
if [[ :$__shopt: == *:dotglob:* ]]; then
shopt -s dotglob
else
shopt -u dotglob
fi
[[ $__set == *f* ]] && set -f
[[ :$__shopt: != *:extglob:* ]] && shopt -u extglob
[[ :$__shopt: != *:nullglob:* ]] && shopt -u nullglob
[[ :$__shopt: == *:failglob:* ]] && shopt -s failglob
return 0
}
function _omb_util_split {
local __set=$- IFS=${3:-$' \t\n'}
set -f
eval -- "$1=(\$2)"
[[ $__set == *f* ]] || set +f
return 0
}
function _omb_util_alias {
case ${OMB_DEFAULT_ALIASES:-enable} in
(disable) return 0 ;;
(check) alias -- "${1%%=*}" &>/dev/null && return 0 ;;
(enable) ;;
(*)
_omb_log_error "invalid value: OMB_DEFAULT_ALIASES='${OMB_DEFAULT_ALIASES-}' (expect: enable|disable|check)" >&2
return 2
esac
alias -- "$1"
}
function _omb_util_alias_delayed__init {
local _omb_name=$1 _omb_init=${FUNCNAME[1]}
local _omb_command=$_omb_name
"_omb_util_alias_select_$_omb_name"
if [[ ! $_omb_command || $_omb_command == "$_omb_name" ]]; then
unalias "$_omb_name"
else
alias "$_omb_name=$_omb_command"
fi || return 1
eval -- "function $_omb_init { command ${_omb_command:-$_omb_name} \"\$@\"; }" && "$_omb_init" "${@:2}"
}
function _omb_util_alias_delayed {
local name=$1 opts=${2-}
local func=_omb_util_alias_init_$name
eval -- "function $func { _omb_util_alias_delayed__init $name \"\$@\"; }"
if [[ :$opts: == *:force:* ]]; then
alias "$name=$func"
else
_omb_util_alias "$name=$func"
fi
}
function _omb_util_mktemp {
local template=tmp.oh-my-bash.XXXXXXXXXX
if _omb_util_command_exists mktemp; then
mktemp -t "$template"
else
m4 -D template="${TMPDIR:-/tmp}/$template" <<< 'mkstemp(template)'
fi
}