-
Notifications
You must be signed in to change notification settings - Fork 3
/
bashbible.nix
368 lines (348 loc) · 9.52 KB
/
bashbible.nix
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
# This overlay contains the bashbible implemented entirely in nix attr sets.
final: prev:
let
inherit (final.lib) attrValues concatStrings;
in
{
# bash bible functions implemented as a set of attribute sets in nix
# https://github.com/dylanaraps/pure-bash-bible
bashbible = rec {
functions = {
strings = {
trim_string = ''
trim_string() {
# Usage: trim_string " example string "
: "''${1#"''${1%%[![:space:]]*}"}"
: "''${_%"''${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
'';
trim_all = ''
# shellcheck disable=SC2086,SC2048
trim_all() {
# Usage: trim_all " example string "
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
'';
regex = ''
regex() {
# Usage: regex "string" "regex"
[[ $1 =~ $2 ]] && printf '%s\n' "''${BASH_REMATCH[1]}"
}
'';
split = ''
split() {
# Usage: split "string" "delimiter"
IFS=$'\n' read -d "" -ra arr <<< "''${1//$2/$'\n'}"
printf '%s\n' "''${arr[@]}"
}
'';
lower = ''
lower() {
# Usage: lower "string"
printf '%s\n' "''${1,,}"
}
'';
upper = ''
upper() {
# Usage: upper "string"
printf '%s\n' "''${1^^}"
}
'';
reverse_case = ''
reverse_case() {
# Usage: reverse_case "string"
printf '%s\n' "''${1~~}"
}
'';
trim_quotes = ''
trim_quotes() {
# Usage: trim_quotes "string"
: "''${1//\'}"
printf '%s\n' "''${_//\"}"
}
'';
strip_all = ''
strip_all() {
# Usage: strip_all "string" "pattern"
printf '%s\n' "''${1//$2}"
}
'';
strip = ''
strip() {
# Usage: strip "string" "pattern"
printf '%s\n' "''${1/$2}"
}
'';
lstrip = ''
# shellcheck disable=SC2295
lstrip() {
# Usage: lstrip "string" "pattern"
printf '%s\n' "''${1##$2}"
}
'';
rstrip = ''
# shellcheck disable=SC2295
rstrip() {
# Usage: rstrip "string" "pattern"
printf '%s\n' "''${1%%$2}"
}
'';
urlencode = ''
urlencode() {
# Usage: urlencode "string"
local LC_ALL=C
for (( i = 0; i < ''${#1}; i++ )); do
: "''${1:i:1}"
case "$_" in
[a-zA-Z0-9.~_-])
printf '%s' "$_"
;;
*)
printf '%%%02X' "'$_"
;;
esac
done
printf '\n'
}
'';
urldecode = ''
urldecode() {
# Usage: urldecode "string"
: "''${1//+/ }"
printf '%b\n' "''${_//%/\\x}"
}
'';
};
arrays = {
reverse_array = ''
reverse_array() {
# Usage: reverse_array "array"
shopt -s extdebug
f()(printf '%s\n' "''${BASH_ARGV[@]}"); f "$@"
shopt -u extdebug
}
'';
remove_array_dups = ''
remove_array_dups() {
# Usage: remove_array_dups "array"
declare -A tmp_array
for i in "$@"; do
[[ $i ]] && IFS=" " tmp_array["''${i:- }"]=1
done
printf '%s\n' "''${!tmp_array[@]}"
}
'';
random_array_element = ''
random_array_element() {
# Usage: random_array_element "array"
local arr=("$@")
printf '%s\n' "''${arr[RANDOM % $#]}"
}
'';
cycle = ''
cycle() {
printf '%s ' "''${arr[''${i:=0}]}"
((i=i>=''${#arr[@]}-1?0:++i))
}
'';
};
files = {
head = ''
head() {
# Usage: head "n" "file"
mapfile -tn "$1" line < "$2"
printf '%s\n' "''${line[@]}"
}
'';
tail = ''
tail() {
# Usage: tail "n" "file"
mapfile -tn 0 line < "$2"
printf '%s\n' "''${line[@]: -$1}"
}
'';
lines = ''
lines() {
# Usage: lines "file"
mapfile -tn 0 lines < "$1"
printf '%s\n' "''${#lines[@]}"
}
'';
count = ''
count() {
# Usage: count /path/to/dir/*
# count /path/to/dir/*/
printf '%s\n' "$#"
}
'';
extract = ''
extract() {
# Usage: extract file "opening marker" "closing marker"
while IFS=$'\n' read -r line; do
[[ $extract && $line != "$3" ]] &&
printf '%s\n' "$line"
[[ $line == "$2" ]] && extract=1
[[ $line == "$3" ]] && extract=
done < "$1"
}
'';
};
paths = {
dirname = ''
dirname() {
# Usage: dirname "path"
local tmp=''${1:-.}
[[ $tmp != *[!/]* ]] && {
printf '/\n'
return
}
tmp=''${tmp%%"''${tmp##*[!/]}"}
[[ $tmp != */* ]] && {
printf '.\n'
return
}
tmp=''${tmp%/*}
tmp=''${tmp%%"''${tmp##*[!/]}"}
printf '%s\n' "''${tmp:-/}"
}
'';
basename = ''
basename() {
# Usage: basename "path" ["suffix"]
local tmp
tmp=''${1%"''${1##*[!/]}"}
tmp=''${tmp##*/}
tmp=''${tmp%"''${2/"$tmp"}"}
printf '%s\n' "''${tmp:-/}"
}
'';
};
terminal = {
get_term_size = ''
get_term_size() {
# Usage: get_term_size
# (:;:) is a micro sleep to ensure the variables are
# exported immediately.
shopt -s checkwinsize; (:;:)
printf '%s\n' "$LINES $COLUMNS"
}
'';
get_window_size = ''
# shellcheck disable=SC2141
get_window_size() {
# Usage: get_window_size
printf '%b' "''${TMUX:+\\ePtmux;\\e}\\e[14t''${TMUX:+\\e\\\\}"
IFS=';t' read -d t -t 0.05 -sra term_size
printf '%s\n' "''${term_size[1]}x''${term_size[2]}"
}
'';
get_cursor_pos = ''
get_cursor_pos() {
# Usage: get_cursor_pos
IFS='[;' read -p $'\e[6n' -d R -rs _ y x _
printf '%s\n' "$x $y"
}
'';
};
conversion = {
hex_to_rgb = ''
hex_to_rgb() {
# Usage: hex_to_rgb "#FFFFFF"
# hex_to_rgb "000000"
: "''${1/\#}"
((r=16#''${_:0:2},g=16#''${_:2:2},b=16#''${_:4:2}))
printf '%s\n' "$r $g $b"
}
'';
rgb_to_hex = ''
rgb_to_hex() {
# Usage: rgb_to_hex "r" "g" "b"
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
}
'';
};
other = {
read_sleep = ''
read_sleep() {
# Usage: read_sleep 1
# read_sleep 0.2
read -rt "$1" <> <(:) || :
}
'';
date = ''
date() {
# Usage: date "format"
# See: 'man strftime' for format.
printf "%($1)T\\n" "-1"
}
'';
uuid = ''
uuid() {
# Usage: uuid
C="89ab"
for ((N=0;N<16;++N)); do
B="$((RANDOM%256))"
case "$N" in
6) printf '4%x' "$((B%16))" ;;
8) printf '%c%x' "''${C:$RANDOM%''${#C}:1}" "$((B%16))" ;;
3|5|7|9)
printf '%02x-' "$B"
;;
*)
printf '%02x' "$B"
;;
esac
done
printf '\n'
}
'';
bar = ''
bar() {
# Usage: bar 1 10
# ^----- Elapsed Percentage (0-100).
# ^-- Total length in chars.
((elapsed=$1*$2/100))
# Create the bar with spaces.
printf -v prog "%''${elapsed}s"
printf -v total "%$(($2-elapsed))s"
printf '%s\r' "[''${prog// /-}''${total}]"
}
'';
get_functions = ''
get_functions() {
# Usage: get_functions
IFS=$'\n' read -d "" -ra functions < <(declare -F)
printf '%s\n' "''${functions[@]//declare -f }"
}
'';
bkr = ''
bkr() {
(nohup "$@" &>/dev/null &)
}
'';
};
};
bible = ''
## bash bible
### strings
${concatStrings (attrValues functions.strings)}
### arrays
${concatStrings (attrValues functions.arrays)}
### files
${concatStrings (attrValues functions.files)}
### paths
${concatStrings (attrValues functions.paths)}
### terminal
${concatStrings (attrValues functions.terminal)}
### conversion
${concatStrings (attrValues functions.conversion)}
### other
${concatStrings (attrValues functions.other)}
## end bash bible
'';
};
}