-
-
Notifications
You must be signed in to change notification settings - Fork 674
/
utils.sh
265 lines (232 loc) · 6.72 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
#!/usr/bin/env bash
############################---Description---###################################
# #
# Summary : A collection of handy utilities and functions for bash #
# Support : [email protected] #
# 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
################################################################################
_omb_version=10000
_omb_bash_version=$((BASH_VERSINFO[0] * 10000 + BASH_VERSINFO[1] * 100 + BASH_VERSINFO[2]))
function _omb_util_defun_print {
builtin eval -- "function $1 { local $3; $2 \"\$@\" && printf '%s\n' \"\${$3}\"; }"
}
function __omb_util_defun_deprecate__message {
local old=$1 new=$2
local v=__omb_util_DeprecateFunction_$old; v=${v//[!a-zA-Z0-9_]/'_'}
[[ ${!v+set} ]] && return 0
printf 'warning (oh-my-bash): %s\n' "\`$old' is deprecated. Use \`$new'." >&2
printf -v "$v" done
}
function _omb_util_defun_deprecate {
local warning=
((_omb_version>=$1)) &&
warning='__omb_util_defun_deprecate__message "$2" "$3"; '
builtin eval -- "function $2 { $warning$3 \"\$@\"; }"
}
#
# Set Colors
#
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [[ -t 1 && $ncolors && ncolors -ge 8 ]]; then
bold=$(tput bold 2>/dev/null || tput md 2>/dev/null)
underline=$(tput smul 2>/dev/null || tput ul 2>/dev/null)
reset=$(tput sgr0 2>/dev/null || tput me 2>/dev/null)
red=$(tput setaf 1 2>/dev/null || tput AF 1 2>/dev/null)
green=$(tput setaf 2 2>/dev/null || tput AF 2 2>/dev/null)
yellow=$(tput setaf 3 2>/dev/null || tput AF 3 2>/dev/null)
blue=$(tput setaf 4 2>/dev/null || tput AF 4 2>/dev/null)
purple=$(tput setaf 171 2>/dev/null || tput AF 171 2>/dev/null)
tan=$(tput setaf 3 2>/dev/null || tput AF 3 2>/dev/null)
else
bold=""
underline=""
reset=""
red=""
green=""
yellow=""
blue=""
purple=""
tan=""
fi
#
# Headers and Logging
#
e_header() { printf "\n${bold}${purple}========== %s ==========${reset}\n" "$@"
}
e_arrow() { printf "➜ %s\n" "$@"
}
e_success() { printf "${green}✔ %s${reset}\n" "$@"
}
e_error() { printf "${red}✖ %s${reset}\n" "$@"
}
e_warning() { printf "${tan}➜ %s${reset}\n" "$@"
}
e_underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
e_bold() { printf "${bold}%s${reset}\n" "$@"
}
e_note() { printf "${underline}${bold}${blue}Note:${reset} ${yellow}%s${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
#
seek_confirmation() {
printf "\\n${bold}%s${reset}" "$@"
read -p " (y/n) " -n 1
printf "\\n"
}
# Test whether the result of an 'ask' is a confirmation
is_confirmed() {
[[ $REPLY =~ ^[Yy]$ ]]
}
#
# 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
_omb_util_command_exists() {
type -t -- "$@" &>/dev/null # bash-4.0
}
_omb_util_binary_exists() {
type -P -- "$@" &>/dev/null # bash-4.0
}
else
_omb_util_command_exists() {
while (($#)); do
type -t -- "$1" &>/dev/null || return 1
shift
done
}
_omb_util_binary_exists() {
while (($#)); do
type -P -- "$1" &>/dev/null || return 1
shift
done
}
fi
_omb_util_function_exists() {
declare -F "$@" &>/dev/null # bash-3.2
}
_omb_util_defun_deprecate 20000 type_exists _omb_util_binary_exists
#
# Test which OS the user runs
# $1 = OS to test
# Usage: if is_os 'darwin'; then
#
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
#
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
}
_omb_util_add_prompt_command() {
# Set OS dependent exact match regular expression
local prompt_re
if [[ $OSTYPE == darwin* ]]; then
# macOS
prompt_re='[[:<:]]'$1'[[:>:]]'
else
# Linux, FreeBSD, etc.
prompt_re='\<'$1'\>'
fi
# See if we need to use the overriden version
if _omb_util_function_exists append_prompt_command_override; then
append_prompt_command_override "$1"
return
fi
if [[ $PROMPT_COMMAND =~ $prompt_re ]]; then
return
else
PROMPT_COMMAND="$1${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
}