-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathessential-functions
executable file
·332 lines (308 loc) · 9.79 KB
/
essential-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
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
#!/bin/bash
source ~/Repositories/dotfiles/zsh/variables
function echolor {
clrs="$usdd/echolors.csv"
color="$(grep "^$(echo "$1" | awk -F '-' '{print $1}' | tr -d '←→')," "$clrs" | ifne awk -F ',' '{print $2}')"
hlght="$(grep "^$(echo "$1" | awk -F '-' '{print $2}' | tr -d '←→')," "$clrs" | ifne awk -F ',' '{print $2}')"
[[ -z "$color" ]] && color=36
[[ -z "$hlght" ]] && {
[[ "$color" = "37" ]] && hlght=33 || hlght=37
}
echo "$1" | grep -q '←' && {
echo "$1" | awk -F '-' '{print $1}' | grep -q '←' && {
color="7;$color"
hlght="0;$hlght"
}
echo "$1" | awk -F '-' '{print $2}' | grep -q '←' && {
hlght="7;$hlght"
color="0;$color"
}
color="$(echo "$color" | ifne sed 's/7;0;/7;/g')"
hlght="$(echo "$hlght" | ifne sed 's/7;0;/7;/g')"
}
text="$(echo "$2" | sed "s/““/\\\\033[${hlght}m/g;s/””/\\\\033[${color}m/g")"
[[ "$3" -eq 1 ]] && nonewline="-n" || nonewline="" # add "1" as a 3rd argument to add -n to echo
echo $nonewline -e "\033["$color"m$text\033[0m"
}
function random-string {
head /dev/urandom | tr -dc a-z0-9 | head -c "${1:-13}"
}
function random-number {
head /dev/urandom | tr -dc 0-9 | head -c "${1:-8}"
}
function random-word {
function one-random-word {
sed -n "$(dice d7776)"p "$HOME/Data/eff/eff-large-wordlist.txt"
}
kebab "$(for j in {1..${1:-1}}; do one-random-word; done)"
}
function scramble-file-names {
echolor red ":: THIS FUNCTION IS UNSAFE. FIX BEFORE USING."
return 1
# TODO add logging
# TODO add file existence check
# TODO add reversal?
# TODO add more checks
# TODO fix unsafe eza calls
# TODO use random-word instead
[[ -z "$1" ]] \
&& echolor yellow ":: Available options: 'files', 'dirs', and 'all'." \
&& return
conf_str="$(basename $(realpath .)) $(random-string 4)"
echolor red-white ":: Are you sure you want to scramble all ““$(pwd)”” file names? Type ““$conf_str”” to confirm. "
read -r conf_str_response
[[ "$conf_str" != "$conf_str_response" ]] \
&& echolor yellow ":: Nothing done." \
&& return
IFS=$'\n'
case "$1" in
"files")
all_files=($(eza -1f))
;;
"dirs")
all_files=($(eza -1D))
;;
"all")
all_files=($(eza -1))
;;
*)
echolor yellow ":: Unknown option. Available options: 'files', 'dirs', and 'all'."
return
;;
esac
counter=0
[[ "${#all_files[@]}" -eq 0 ]] \
&& echolor yellow ":: Directory is empty of specified files."
for file_to_scramble in ${all_files[@]};
do
ext="${file_to_scramble##*.}"
mv -iv -- "$file_to_scramble" "$(random-string)"."$ext"
((counter++))
done
echolor yellow ":: Scrambled $counter files."
unset IFS
}
function kebab {
local kebab_input="${@:-$(cat /dev/stdin)}"
echo -e "$kebab_input" \
| iconv -c -f utf8 -t ascii//TRANSLIT \
| perl -pe 's|&.*?;||g' \
| tr -d '"’“”!?*=$:;#@^~()[]{}<>\t\\' \
| sed 's|/| |g;s/\&/-and-/g;s/\%/-percent-/g' \
| tr -d "'" \
| tr '_ .|/+,\n–—' '-' \
| tr '[A-Z]' '[a-z]' \
| sed 's/--*/-/g;s/-$//g;s/^-//g'
}
function basic-commit {
cd "$1" \
|| \
{
echolor red ":: Cannot stat into directory ““$1””."
return
}
git add .
git commit -m "$(date +"%Y%m%d%H%M")"
cd - > /dev/null
}
function list-cutter {
# what the fuck is this function and when did I write it
select_from=""
IFS=$'\n'
arr=($(echo $@))
i=0
echolor white -----------------------------------------
for j in ${arr[@]}
do
[[ "$((i % 2))" -eq 0 ]] \
&& color_alt="white" \
|| color_alt="yellow"
echolor "$color_alt" "$((i + 1))\t$j"
echo "$j" | grep -q ',' \
&& j="\"$j\""
select_from+="$(echo -ne "$j,")"
((i++))
done
echolor white -----------------------------------------
echo -n ':: Select range: '
read -r sel_range
echo "$select_from" | xsv select "${sel_range:-$(seq -s ',' 1 ${#arr[@]})}"
unset IFS
}
function move-to-trash {
trashdir="$HOME/.local/share/Trash"
fd -Fq "^$1$" "$trashdir"/files && {
postdelname="$1-$(date +"%Y_%m_%d_%H_%M_%S")"
command mv -n -- "$1" "$trashdir"/files/"$postdelname" \
|| {
echolor red ":: Error moving ““$1”” to trash as ““$postdelname””"
echolor red ":: Exiting immediately..."
return
}
} || {
postdelname="$1"
command mv -n -- "$1" "$trashdir"/files/ \
|| {
echolor red ":: Error moving ““$1”” to trash"
return
}
}
echo "$(date +"%Y-%m-%d %H:%M:%S") ¼⅓ $(basename -- "$1") ¼⅓ $(basename -- "$postdelname")" \
>> "$trashdir"/deletetimes
[ "$1" = "$postdelname" ] && {
echolor blue ":: File ““$1”” moved to trash."
} || {
echolor purple ":: File ““$1”” moved to trash as ““$postdelname””"
}
}
function move-to-trash-recursively {
IFS=$'\n'
files_to_trash=("$@")
i=0
for j in ${files_to_trash[@]}
do
move-to-trash "$j"
((i++))
done
export number_of_files_trashed="$i"
unset IFS
}
function restore-files-from-trash {
IFS=$'\n'
trashdir="$HOME/.local/share/Trash"
restfiles=($(cat "$trashdir"/deletetimes | sort -V | tail -n "${1:-1}" | awk -F ' ¼⅓ ' '{print $2, "¼⅓", $3}'))
[ -z "$restfiles" ] && {
echolor red ":: No files chosen."
return
}
echolor yellow ":: Restoring the following files from trash:"
for j in ${restfiles[@]}
do
echolor white "- $(echo "$j" | awk -F ' ¼⅓ ' '{print $2}')"
done
proceed_p=n
echo -ne "\033[33m:: Proceed? (y/N) "
read -r proceed_p
[ "$proceed_p" = "y" ] || {
echolor yellow ":: Doing nothing."
return
}
for j in ${restfiles[@]}
do
normal_name="$(echo "$j" | awk -F ' ¼⅓ ' '{print $1}')"
trash_name="$(echo "$j" | awk -F ' ¼⅓ ' '{print $2}')"
command mv -n -- "$trashdir"/files/"$trash_name" ./"$normal_name" && {
[ "$trash_name" = "$normal_name" ] \
&& echolor blue ":: File ““$normal_name”” restored from trash." \
|| echolor purple ":: File ““$trash_name”” restored from trash as ““$normal_name””"
} || {
echolor red ":: Error restoring ““$trash_name”” from trash as ““$normal_name””"
return
}
sed -i "/¼⅓ $normal_name ¼⅓ $trash_name/d;/^$/d" "$trashdir"/deletetimes
done
unset IFS
}
function extension-determiner {
grep "$(mimetype -Mb "$1")" ~/.local/share/user-scripts/mime-extensions.csv \
| ifne xsv select 2 2>/dev/null
}
function date-string {
date_string="$(date +"%Y%m%d%H%M%S")"
case "$1" in
"long") date_string="$(date +"%Y-%m-%d %H:%M:%S")" ;;
"") echo -n ;;
"y") date_string="$(echo $date_string | cut -c-4)" ;;
"ym") date_string="$(echo $date_string | cut -c-6)" ;;
"ymd") date_string="$(echo $date_string | cut -c-8)" ;;
"ymdh") date_string="$(echo $date_string | cut -c-10)" ;;
"ymdhm") date_string="$(echo $date_string | cut -c-12)" ;;
"ymdhms") date_string="$(echo $date_string | cut -c-14)" ;;
*) echo -n ;;
esac
echo $date_string
}
function exchange-names {
[[ -e "$1" ]] || {
echolor red ":: Please input valid filenames."
return
}
[[ -e "$2" ]] || {
echolor red ":: Please input second filename."
return
}
name1="$(echo $1 | sed 's|/||g')"
name2="$(echo $2 | sed 's|/||g')"
conf_str="$(random-string 2)"
echolor yellow ":: Are you sure to exchange ““$name1”” and ““$name2””?"
echolor yellow-green ":: Type ““$conf_str”” to confirm."
echo -ne "\033[37m>>> \033[0m"
read -r conf_str_response
[[ "$conf_str" != "$conf_str_response" ]] \
&& echolor red ":: Nothing done." \
&& return
mv -n -- "$name1" .temp-"$name2" || {
echolor red ":: Failure! (error code ““1””)"
return
}
mv -n -- "$name2" "$name1" || {
echolor red ":: Failure! (error code ““2””)"
return
}
mv -n -- .temp-"$name2" "$name2" || {
echolor red ":: Failure! (error code ““3””)"
return
}
echolor yellow ":: Files ““$name1”” and ““$name2”” have successfully exchanged names."
}
function battery-low-detect {
batterypercent="$(acpi -b | grep 'Battery 1' | awk -F ',' '{print $2}' | tr -d '% ')"
[[ "$batterypercent" -gt 25 ]] && batterylow="0" || batterylow="1"
}
function warning-bloops {
mpv --loop=10 "$HOME/.local/share/user-scripts/sounds/bloops.oga"
}
function sfx {
~/Repositories/scripts/sound-effects "$@"
}
function clear-line {
printf '\r\033[K'
}
function sane {
echolor purple ":: Sanity checkpoint $1"
}
function wifi-connected-p {
case "$(nmcli device status | grep "^wlan0" | awk '{print $3}')" in
"connected") return 0 ;;
*) return 1 ;;
esac
}
function esa {
eza -1fX --show-symlinks "$@"
}
function choose-font {
~/Repositories/scripts/font-chooser.sh "$@"
}
# function ¿ {
# : function ¿ interrogates functions and aliases in bash, enabling them to be self-documenting.
# :
# : in case $1 is an alias, ¿ will show the definition of the alias.
# :
# : in case $1 is a function, ¿ will show the location of the definition as well as the
# : docstring, which is defined by the builtin bash null function ":".
# type_of="$(type "$1" | awk '{print $4,$5}')"
# case "$type_of" in
# "alias for")
# echolor yellow-purple ":: ““$1”” is an alias for:"
# type "$1" | sed "s/$1 is an alias for //g" | bat -Ppl bash
# ;;
# "shell function")
# echolor yellow-purple ":: ““$1”” is a function defined in ““$(type "$1" | awk '{print $NF}')””"
# declare -f "$1" | sed '1d' | head -n -1 | grep "^\s*:" | bat -Ppl bash
# ;;
# *)
# echolor red ":: Unrecognized type."
# ;;
# esac
# # TODO find a better way for this
# }