-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
471 lines (396 loc) · 14.4 KB
/
functions
File metadata and controls
471 lines (396 loc) · 14.4 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env bash
#
# Utilities for monotonous tasks.
# ===================================
#
# This file includes all the utilities used to execute monotonous and
# repetitive tasks such as compilation, or shell execution. Some of these
# functions are used to have an easy management of shell dotfiles
# (see README.md for more information)
#
# Feel free to add new functions or edit the default ones as much as you
# want, but as it is obvious, any modification is unsafe and not considered.
# So, it is recommended to only include more functions if you want.
#
# SYNTAX (IMPORTANT!!) <<<<<<<<
#
# Related to code style, some of the default functions are parser which will
# read this file and extract some information based on a specific syntax. The
# following list includes all the restrictions that should be taken into account:
#
# - Function declaration must be "function <name> () {"
# - Private function needs to be after private heading ("# PRIVATE FUNCTIONS")
# - Names for private functions needs to be declared with an "_" at the beggining
# - This initial comments should finish with a line with "END_DESCRIPTION"
# - Functions should have a docummentation, but it is not necessary
# - After private heading it is only possible to define private functions
#
# END_DESCRIPTION
script_path="${BASH_SOURCE:-$0}"
abs_script_path="$(realpath "${script_path}")"
_shell_abs_script_directory="$(dirname "${abs_script_path}")"
unset script_path abs_script_path
source $_shell_abs_script_directory/aliases
# Display and error and usage message related to a function.
# This function would not print anything if parameters are
# not all defined, but does not check its values.
# usage: showerr <FUNC_NAME> <ERR_MESSAGE> <USAGE_MESSAGE>
function showerr () {
if (( $# == 3 )); then
echo "error: $1: $2" 1>&2
echo "use: $1: $3" 1>&2
fi
return 1
}
# Show information about the passed utility whether
# it truly exists. If no arguments were passed, this
# command would show help information for any single
# function defined
# usage: shellhelp <NAME> [NAMES ...]
function shellhelp () {
local usage_mssg="shellhelp <NAME>"
local error1_mssg="invalid number of arguments"
local error2_mssg="cannot be an empty value"
(( $# < 0 )) && showerr "shellhelp" $error1_mssg $usage_mssg && return 1
if [[ ! -f $_shell_abs_script_directory/.shellhelp-cache ]]; then
rm $_shell_abs_script_directory/.date-shellhelp-cache &>/dev/null
touch $_shell_abs_script_directory/.shellhelp-cache
touch -r $_shell_abs_script_directory/functions $_shell_abs_script_directory/.date-shellhelp-cache
_parse_shellhelp > $_shell_abs_script_directory/.shellhelp-cache
else
local date_curr=$(stat -c %y "$_shell_abs_script_directory/functions")
local date_last=$(stat -c %y "$_shell_abs_script_directory/.date-shellhelp-cache")
if [ ! $date_curr = $date_last ]; then
rm $_shell_abs_script_directory/.date-shellhelp-cache &>/dev/null
touch -r $_shell_abs_script_directory/functions $_shell_abs_script_directory/.date-shellhelp-cache
_parse_shellhelp > $_shell_abs_script_directory/.shellhelp-cache
fi
fi
if (( $# == 0 )); then
cat "$_shell_abs_script_directory/.shellhelp-cache" | egrep -v "^#"
else
while (( $# > 0 )); do
[[ -z $1 ]] && showerr "shellhelp" "NAME $error2_mssg" $usage_mssg && return 1
local temp_dir=$(mktemp)
cat $_shell_abs_script_directory/.shellhelp-cache | egrep -n "#$1#" | cut -d ":" -f 1 > $temp_dir
local min_value=$(sed -n 1p $temp_dir)
min_value=$(($min_value + 1))
local max_value=$(sed -n 2p $temp_dir)
max_value=$(($max_value - 1))
sed -n $min_value,${max_value}p $_shell_abs_script_directory/.shellhelp-cache
rm $temp_dir &>/dev/null; shift
done
fi
}
# Reload all dotfiles and bash settings.
# It is important to notice that ZSH shell
# is not optimized, so it will take a lot of
# time to fully load into shell. Please, use
# reload-bash instead whether you only want to reload
# functions, aliases, or exports.
# usage: reload-bash
function reload-all-bash () {
echo "This function will create a new shell session from current one, so"
echo "you have to take into account that if this function is called multiple"
echo "times, multiple sessions would be created."
sleep 2
case $SHELL_NAME in
bash) bash ;;
zsh) zsh ;;
*) showerr "reload-bash" "invalid shell" "reload-bash"; return 1 ;;
esac
}
# Reload all dotfiles to apply any changes
# that was done at internal files
# usage: reload-bash
function reload-bash () {
source ~/.config/shell/exports
source ~/.config/shell/aliases
source ~/.config/shell/functions
}
# Update value for passed global variable or add a new one
# which passed value. Changes would be applied before
# executing reload-bash.
# usage: setexport <NAME> <VALUE>
function setexport () {
local usage_mssg="setexport <NAME> <VALUE>"
local error1_mssg="invalid number of arguments"
local error2_mssg="cannot be an empty value"
(( $# != 2 )) && showerr "setexport" $error1_mssg $usage_mssg && return 1
[[ -z $1 ]] && showerr "setexport" "$1 $error2_mssg" $usage_mssg && return 1
[[ -z $2 ]] && showerr "setexport" "$2 $error2_mssg" $usage_mssg && return 1
local n_line=$(egrep -n "^export $1=.*" ~/.config/shell/exports | cut -d ":" -f 1)
local curr_n_line=1
if [[ -z $n_line ]]; then
echo "export $1=$2" >> ~/.config/shell/exports
return 0
fi
local temp_file=$(mktemp)
while IFS= read -r line; do
if (( curr_n_line == n_line )); then
echo "export $1=$2" >>$temp_file
else
echo "$line" >>$temp_file
fi
curr_n_line=$(($curr_n_line + 1))
done < ~/.config/shell/exports
rm ~/.config/shell/exports
mv $temp_file ~/.config/shell/exports
}
# Remove passed global variable whether it exists at current
# exports file. Changes would be applied before executing reload-bash.
# usage: rmexport <NAME>
function rmexport () {
local usage_mssg="rmexport <NAME>"
local error1_mssg="invalid number of arguments"
local error2_mssg="cannot be an empty value"
local error3_mssg="does not exist as a global variable"
(( $# != 1 )) && showerr "rmexport" $error1_mssg $usage_mssg && return 1
[[ -z $1 ]] && showerr "rmexport" "$1 $error2_mssg" $usage_mssg && return 1
local n_line=$(egrep -n "^export $1=.*" ~/.config/shell/exports | cut -d ":" -f 1)
local curr_n_line=1
[[ -z $n_line ]] && showerr "rmexport" "$1 $error3_mssg" $usage_mssg && return 1
local temp_file=$(mktemp)
while IFS=$'\t' read -r line; do
if (( curr_n_line != n_line )); then
echo "$line" >>$temp_file
fi
curr_n_line=$(($curr_n_line + 1))
done < ~/.config/shell/exports
rm ~/.config/shell/exports
mv $temp_file ~/.config/shell/exports
}
# Change current shell whether passed value it is an available
# and valid shell name. Changes would be applied before
# executing alias reload-bash found at aliases
# usage: chshell NAME
function chshell () {
local usage_mssg="chshell <NAME>"
local error1_mssg="invalid number of arguments"
local error2_mssg="cannot be an empty value"
local error3_mssg="is not a valid shell"
(( $# != 1 )) && showerr "chshell" $error1_mssg $usage_mssg && return 1
[[ -z $1 ]] && showerr "chshell" "$1 $error2_mssg" $usage_mssg && return 1
local curr_shell=$(ps --no-headers -p $$ -o cmd)
case $1 in
bash)
setexport SHELL /bin/bash
setexport SHELL_NAME bash
setexport HISTFILE ~/.config/shell/.bash_history
reload-bash
sudo chsh -s $(which bash)
[ $curr_shell = "bash" ] || reload-all-bash
;;
zsh)
setexport SHELL /bin/zsh
setexport SHELL_NAME zsh
setexport HISTFILE ~/.config/shell/.zhistory
reload-bash
sudo chsh -s $(which zsh)
[ $curr_shell = "zsh" ] || reload-all-bash
;;
*)
showerr "chshell" "$1 $error3_mssg" $usage_mssg
return 1
;;
esac
}
# If this command is executed, next time shell runs it would
# only load necessary files, since performance is most
# important than appearance.
# usage: set-shell-simple
function set-shell-simple () {
setexport SHELL_STARTUP_MODE simple
}
# If this command is executed, next time shell runs
# it would load all files, since appearance is most
# important than performance.
# # usage: set-shell-pretty
function set-shell-pretty () {
setexport SHELL_STARTUP_MODE pretty
}
# Return current memory porcentage that is not occupied
# usage: porcmem
function porcmem () {
local column=$(free -w | egrep "Mem:")
local porc=$(( $(echo $column | awk '{print $3}') * 100 ))
porc=$(( $porc / $(echo $column | awk '{print $2}') ))
return $porc
}
# Show information about RAM's status using an human
# readable output. Status includes number of data used,
# total capacity, and usage procentage.
# usage: raminfo
function raminfo () {
local porc=$(porcmem; echo $?)
local column=$(free -hw | grep "Mem:")
local used=$(echo $column | awk '{print $3}')
local total=$(echo $column | awk '{print $2}')
echo "${used} / ${total} (${porc}%)"
}
# Show information about a mounted filesystem using an
# human readable output. Data printed include usage,
# size, porcentage of use, filesystem type, and passed
# path for target specified
# usage: mountinfo <TARGET>
function mountinfo () {
[[ -z $1 ]] && showerr "mountinfo" "target not specified" "mountinfo <TARGET>" && return 1
local temp_file=$(mktemp)
df -h --output=used,size,pcent,fstype,target | grep "$1" > $temp_file
while IFS= read -r line; do
if [[ $(echo $line | awk '{print $5}') = $1 ]]; then
echo $line | awk '{print $1 " | " $2 " (" $3 ")" " (" $4 ")"}'
return 0
fi
done < $temp_file
showerr "mountinfo" "unkown target" "mountinfo <TARGET>"
return 1
}
# Create a script .sh file at current path adjusting its
# permissions in order to be executable.
# usage: mksh <FILE> [FILE, ...]
function mksh () {
local usage_mssg="mksh <FILE> [FILE, ...]"
(( $# < 1 )) && showerr "mksh" "invalid number of parameters" $usage_mssg && return 1
while (( $# != 0 )); do
[[ ! $1 =~ .+\.sh ]] && showerr "mksh" "file needs to be a .sh file" $usage_mssg && return 1
touch $1; chmod +x $1; shift
done
}
# Create a new directory and set current working
# directory at that new folder.
# usage: mkd <DIR_PATH>
function mkd () {
(( $# != 0 )) && showerr "mkd" "invalid number of parameters" "mkd <DIR_PATH>" && return 1
mkdir -p "$@" && cd "$_"
}
# Get size of a file/folder in human readable format.
# If file or folder is not specified, command would
# be applied to root path "/"
# usage: fs <FILE_PATH>
function fs () {
if [[ -n "$@" ]]; then
du -sh -- "$@"
else
du -sh ./*
fi
}
# Extracts current file according to its format
# usage: ex <FILE>
function ex () {
(( $# != 1 )) && showerr "ex" "invalid number of arguments" "ex <FILE>" && return 1
[[ -z $1 ]] && showerr "ex" "$1 cannot be empty" "ex <FILE>" && return 1
[[ ! -f $1 ]] && showerr "ex" "$1 is not a valid file" "ex <FILE>" && return 1
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.tar.xz) tar xf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*)
showerr "ex" "invalid format or not available" "ex <FILE>"
return 1
;;
esac
}
# Create an interactive menu using key arrows.
# You can include a header at the beggining of the menu
# passing a third parameter with a plain-text file that
# contains that title.
# usage: interactive_menu [HEADER_FILE] SELECTED_OPTION OPTIONS
function interactive_menu () {
local error1_mssg="invalid number of parameters"
local usage_mssg="interactive_menu [HEADER_FILE] SELECTED_OPTION OPTIONS"
(( $# < 2 )) && showerr "interactive_menu" $error1_mssg $usage_mssg
local header_title=""
[[ -f $1 ]] && header_title=$1 && shift
local function_arguments=($@)
local selected_item="$1"
local menu_items=(${function_arguments[@]:1})
local menu_size="${#menu_items[@]}"
local menu_limit=$((menu_size - 1))
clear; [[ ! -z $header_title ]] && echo -e $(cat $header_title)
_print_menu "$selected_item" "${menu_items[@]}"
while read -rsn1 input; do
case "$input" in
$'\x1B')
read -rsn1 -t 0.1 input
if [ "$input" = "[" ]; then
read -rsn1 -t 0.1 input
case "$input" in
A)
if [ "$selected_item" -ge 1 ]
then
selected_item=$((selected_item - 1))
clear; [[ ! -z $header_title ]] && echo -e $(cat $header_title)
_print_menu "$selected_item" "${menu_items[@]}"
fi
;;
B)
if [ "$selected_item" -lt "$menu_limit" ]
then
selected_item=$((selected_item + 1))
clear; [[ ! -z $header_title ]] && echo -e $(cat $header_title)
_print_menu "$selected_item" "${menu_items[@]}"
fi
;;
esac
fi
read -rsn5 -t 0.1
;;
"") return "$selected_item" ;;
esac
done
}
# PRIVATE FUNCTIONS
# ===========================
#
# It is not recommended to use any of these functions
# since they are used as auxiliar tools which are not
# safe and would only be used at some functions.
#
# Please do not modify private function heading since
# some functions could not work as expected
function _print_menu () {
local function_arguments=($@)
local selected_item="$1"
local menu_items=(${function_arguments[@]:1})
local menu_size="${#menu_items[@]}"
for (( i = 0; i < $menu_size; ++i )); do
if [ "$i" = "$selected_item" ]; then
echo -e " ${EXPAND_HI_BACKGROUND_WHITE}${BOLD_HI_BLACK}${menu_items[i]}${CLOSE_COLOR}"
else
echo -e " ${menu_items[i]}"
fi
done
}
function _parse_shellhelp () {
local func_name=""; local func_description=""; local flag_descr=0
while IFS= read -r line; do
if (( $flag_descr == 1 )); then
local cond_flag_private_func=$(echo "$line" | egrep -w "# PRIVATE FUNCTIONS")
[[ ! -z $cond_flag_private_func ]] && break
if [[ ! -z $(echo "$line" | egrep -w "^#.*|^function.*") ]]; then
if [[ ! -z $(echo "$line" | egrep -w "^function.*") ]]; then
func_name=$(echo "$line" | egrep -w "^function.*" | cut -d " " -f 2)
echo "#$func_name#\n * $func_name\n\n\t$func_description"
echo "#$func_name#"
unset func_name func_description
else
line=$(echo "$line" | tr -d "#" | tr -s " ")
func_description=${func_description}${line}$'\n\t'
fi
fi
else
local cond_flag_descr=$(echo "$line" | egrep -w "# END_DESCRIPTION")
[[ ! -z $cond_flag_descr ]] && flag_descr=1 && continue
fi
done < $_shell_abs_script_directory/functions
}