-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions
281 lines (251 loc) · 7.92 KB
/
functions
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
#!/bin/bash
#####################################################################
# Global functions in here. Will always be loaded
# Be careful about possible name collisions here!
#####################################################################
# Print warning message.
function int_ninjab_warning() {
echo "$*" >&2
}
# Print error message and exit.
function int_ninjab_error() {
echo "$*" >&2
#exit 1
}
# Ask yesno question. (taken from http://www.linuxjournal.com/content/asking-yesno-question-bash-script)
# Minor changes by Lars Solberg
#
# Usage: int_ninjab_yesno OPTIONS QUESTION
#
# Options:
# --timeout N Timeout if no input seen in N seconds.
# --default ANS Use ANS as the default answer on timeout or
# if an empty answer is provided.
#
# Exit status is the answer.
function int_ninjab_yesno() {
local ans
local ok=0
local timeout=0
local charcount
local default
local t
while [[ "$1" ]]; do
case "$1" in
--default)
shift
default=$1
if [[ ! "$default" ]]; then int_ninjab_error "Missing default value"; fi
t=$(tr '[:upper:]' '[:lower:]' <<<$default)
if [[ "$t" != 'y' && "$t" != 'yes' && "$t" != 'n' && "$t" != 'no' ]]; then
int_ninjab_error "Illegal default answer: $default"
fi
default=$t
shift
;;
--timeout)
shift
timeout=$1
if [[ ! "$timeout" ]]; then int_ninjab_error "Missing timeout value"; fi
if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then int_ninjab_error "Illegal timeout value: $timeout"; fi
shift
;;
--charcount)
shift
charcount=$1
shift
;;
--nonewline)
nonewline=1
shift
;;
-*)
int_ninjab_error "Unrecognized option: $1"
break
;;
*)
break
;;
esac
done
if [[ $timeout -ne 0 && ! "$default" ]]; then
int_ninjab_error "Non-zero timeout requires a default answer"
fi
while [[ $ok -eq 0 ]]; do
if [ -z ${charcount} ]; then
charcount_cmd=""
else
charcount_cmd=" -n ${charcount}"
fi
if [[ $timeout -ne 0 ]]; then
if ! eval read -t $timeout $charcount -p "$*" ans; then
ans=$default
else
# Turn off timeout if answer entered.
timeout=0
if [[ ! "$ans" ]]; then ans=$default; fi
fi
else
if [ -z ${nonewline} ]; then
echo "${1}"
else
echo -n "${1}"
fi
read_cmd="read ${charcount_cmd} ans"
eval ${read_cmd}
if [[ ! "$ans" ]]; then
ans=$default
else
ans=$(tr '[:upper:]' '[:lower:]' <<<$ans)
fi
fi
if [[ "$ans" == 'y' || "$ans" == 'yes' || "$ans" == 'n' || "$ans" == 'no' ]]; then
ok=1
fi
# We want to ask the user with our own text
#if [[ $ok -eq 0 ]]; then
# int_ninjab_warning "Valid answers are: yes y no n"
#fi
done
[[ "$ans" = "y" || "$ans" == "yes" ]]
}
function set_color() {
[ "${2}" == "tput" ] && colormode=tput
case $colormode in
tput)
# Thanks from https://github.com/wayneeseguin/rvm/blob/master/scripts/color
case "$1" in
# regular colors
black) tput setaf 0 ;;
red) tput setaf 1 ;;
green) tput setaf 2 ;;
yellow) tput setaf 3 ;;
blue) tput setaf 4 ;;
magenta) tput setaf 5 ;;
cyan) tput setaf 6 ;;
white) tput setaf 7 ;;
# emphasized (bolded) colors
eblack) tput bold ; tput setaf 0 ;;
ered) tput bold ; tput setaf 1 ;;
egreen) tput bold ; tput setaf 2 ;;
eyellow) tput bold ; tput setaf 3 ;;
eblue) tput bold ; tput setaf 4 ;;
emagenta) tput bold ; tput setaf 5 ;;
ecyan) tput bold ; tput setaf 6 ;;
ewhite) tput bold ; tput setaf 7 ;;
# underlined colors
ublack) set smul unset rmul ; tput setaf 0 ;;
ured) set smul unset rmul ; tput setaf 1 ;;
ugreen) set smul unset rmul ; tput setaf 2 ;;
uyellow) set smul unset rmul ; tput setaf 3 ;;
ublue) set smul unset rmul ; tput setaf 4 ;;
umagenta) set smul unset rmul ; tput setaf 5 ;;
ucyan) set smul unset rmul ; tput setaf 6 ;;
uwhite) set smul unset rmul ; tput setaf 7 ;;
# background colors
bblack) tput setab 0 ;;
bred) tput setab 1 ;;
bgreen) tput setab 2 ;;
byellow) tput setab 3 ;;
bblue) tput setab 4 ;;
bmagenta) tput setab 5 ;;
bcyan) tput setab 6 ;;
bwhite) tput setab 7 ;;
# Defaults
default) tput setaf 9 ;;
bdefault) tput setab 9 ;;
none) tput sgr0 ;;
*) tput sgr0 # Reset
esac
;;
bash)
case $1 in
# regular colors
black) echo -e '\e[0;30m';;
red) echo -e '\e[0;31m';;
green) echo -e '\e[0;32m';;
yellow) echo -e '\e[0;33m';;
blue) echo -e '\e[0;34m';;
magenta) echo -e '\e[0;35m';;
cyan) echo -e '\e[0;36m';;
white) echo -e '\e[0;37m';;
# emphasized (bolded) colors
eblack) echo -e '\e[1;30m';;
ered) echo -e '\e[1;31m';;
egreen) echo -e '\e[1;32m';;
eyellow) echo -e '\e[1;33m';;
eblue) echo -e '\e[1;34m';;
emagenta) echo -e '\e[1;35m';;
ecyan) echo -e '\e[1;36m';;
ewhite) echo -e '\e[1;37m';;
# underlined colors
ublack) echo -e '\e[4;30m';;
ured) echo -e '\e[4;31m';;
ugreen) echo -e '\e[4;32m';;
uyellow) echo -e '\e[4;33m';;
ublue) echo -e '\e[4;34m';;
umagenta) echo -e '\e[4;35m';;
ucyan) echo -e '\e[4;36m';;
uwhite) echo -e '\e[4;37m';;
# background colors
bblack) echo -e '\e[40m';;
bred) echo -e '\e[41m';;
bgreen) echo -e '\e[42m';;
byellow) echo -e '\e[43m';;
bblue) echo -e '\e[44m';;
bmagenta) echo -e '\e[45m';;
bcyan) echo -e '\e[46m';;
bwhite) echo -e '\e[47m';;
# Defaults
default) echo -e '\e[0;37m';;
bdefault) echo -e '\e[40m';;
none) echo -e '\e[0m';;
*) echo -e '\e[0m';;
esac
esac
}
# Global ninjab helper
function ninjab() {
case $1 in
help)
if [ ${2} ]; then
if [ -f "${mydir}/parts/${2}" ]; then
if declare -f int_ninjab_help_${2} &> /dev/null; then
echo -e "Help text about ${2}:\n"
int_ninjab_help_${2}
else
if int_ninjab_yesno "${2} didnt contain any help, do you want to manually inspect the source? [y/n]"; then
less "${mydir}/parts/${2}"
fi
fi
else
echo "Unable to find a module with that name"
fi
else
echo "Ninjab Is Not Just Another Bashrc.."
echo
echo "Comes in different parts, here is a list of the currently"
echo "loaded parts. Type eg 'ninjab help 4_prompt' to get more"
echo "info/help on that specific part."
echo
for p in ${int_ninjab_available_parts}; do
echo "Part $p"
done
echo
echo
echo "'ninjab' would reload ninjab"
echo "'ninjab unload' to unload and set a basic prompt temporary"
fi
;;
unload)
echo "Unloading most of ninjab, note that we cant set everything back. Like aliases and bash options"
echo "Type 'ninjab' to get the prompt back in this session"
unset PROMPT_COMMAND SUDO_PS1
export PS1="\u@\h { \w }\$ "
;;
*)
echo "Loading/Reloading ninjab, type 'ninjab help' for help"
. "${mydir}/loader"
;;
esac
}