-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
521 lines (411 loc) · 11.9 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env bash
# Description ->
#
# |- Summary: Functions for bash environment
# - Includes many functions that work better inside of bash
#
# |- Dependencies:
# - fzf
# - fdfind
# - eza
# - others depending on OS
# - RilCritch's ansi_escape_sequences file
# Document Info ->
#
# |- Author: RilCritch
#
# |- Last Update: 02/11/2024
# Imports - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
. ${BASH_HOME}/ansi_escape_sequences
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Utilities {{{
## directory tests {{{{
# Check if argument for dir is empty or a valid dir
# This function defaults to the $HOME dir
# |- Arg 1 is empty: dir -> $HOME
# |- Arg 1 is Dir: dir -> $1
# |- Arg 1 is ! Dir: dir -> "" (empty string denotes failure)
get-dir-h() {
if [[ -z "$1" ]]; then
echo "${HOME}"
else
if [[ -d "$1" ]]; then
echo "${1}"
else
echo ""
fi
fi
}
# Check if argument for dir is empty or a valid dir
# This function defaults to the working directory
# |- Same as get-dir-c but default is current directory
get-dir-c() {
if [[ -z "$1" ]]; then
\pwd
else
if [[ -d "$1" ]]; then
echo "${1}"
else
echo ""
fi
fi
}
# }}}}
## Tests {{{{
# Simple number test
# 0 -> is a number
# 1 -> not a number
is_number() {
if [[ "$1" =~ ^-?[0-9]+$ ]]; then
echo "0"
else
echo "1"
fi
}
#}}}}
## Size Calculations {{{{
# Returns the recommended size of a tree
# |- If a non valid dir is specified pwd is used
tree-size() {
# Arguments
if [[ ! -d "${1}" ]]; then
dir_name=$(\pwd)
else
dir_name="${1}"
fi
if [[ -z $2 ]]; then
extra_lines="0"
else
if [[ "$2" =~ ^-?[0-9]+$ ]]; then
extra_lines="$2"
else
extra_lines="0"
fi
fi
term_height=$(tput lines)
height=$((term_height - extra_lines))
if [[ $(\eza -I=".git" -Ta -L=2 "$dir_name" | wc -l) -lt "$height" ]]; then
echo "2"
elif [[ $(\eza -I=".git" -Ta -L=1 "$dir_name" | wc -l) -lt "$height" ]]; then
echo "1"
else
echo "0"
fi
}
# }}}}
# }}}
# Directory Listing {{{
## Recursive {{{{
lsr() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
eza -R --icons -L="$size" --group-directories-first
}
lsra() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
eza -R -a --icons -L="$size" --group-directories-first
}
clsr() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
clear
eza -R --icons -L="$size" --group-directories-first
}
clsra() {
local size="$1"
local ignore="$2" # format "ignore1|ignore2|ignoreN"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
# files to ignore
if [ -z "$ignore" ]; then
ignore=""
else
ignore="--ignore-glob='${2}'"
fi
clear
eval eza -Ra --icons "$ignore" -L="$size" --group-directories-first
}
# }}}}
## Tree {{{{
lst() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
eza -T --icons -L="$size" --group-directories-first
}
clst() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
clear
eza -T --icons -L="$size" --group-directories-first
}
clsta() {
local size="$1"
# Default size of 2
if [ -z "$size" ] || [ "$size" -le 0 ]; then
size=2
fi
clear
eza -Ta --icons -L="$size" --group-directories-first
}
# }}}}
## In depth list outputs {{{{
# Directory overview
# |- Arg 1: Directory to list (must be valid dir, default: pwd)
# |- Arg 2: Integer specifying extra lines for eza height calculation
dir-overview() {
# Arguments
dir_name=$(get-dir-c "$1")
if [[ -z "$dir_name" ]]; then
echo -e "${red}ERROR: ${reset}Specified Directory ${blue}${1} ${reset}does not exist."
return 2
fi
if [[ -n "${2}" && $(is_number "${2}") -eq 0 ]]; then
input="${2}"
extra_lines=$((input + 4))
else
extra_lines="4"
fi
# Data
dir_path=$(realpath "${dir_name}")
num_files=$(fdfind . "${dir_path}" -H --type f --max-depth 1 | wc -l)
num_dirs=$(fdfind . "${dir_path}" -H --type d --max-depth 1 | wc -l)
num_links=$(fdfind . "${dir_path}" -H --type l --max-depth 1 | wc -l)
tree_height=$(tree-size "${dir_path}" "${extra_lines}")
# output
if [[ "$tree_height" -eq 0 ]]; then
echo -en "${cyan} "
echo -en "${bold}${dir_path}${reset} ${l_black}|"
echo -en "${reset} "
echo -en "${dim} ${num_files}${reset} ${l_black}|"
echo -en "${blue} "
echo -en "${dim} ${num_dirs}${reset} ${l_black}|"
echo -en "${cyan} "
echo -e "${dim} ${num_links}"
across-line | clr blackL
eza -a --group-directories-first -I=".git" "$dir_path"
else
echo -en "${cyan} "
echo -en "${bold}${dir_path}${reset} ${l_black}|"
echo -en "${reset} "
echo -en "${dim} ${num_files}${reset} ${l_black}|"
echo -en "${blue} "
echo -en "${dim} ${num_dirs}${reset} ${l_black}|"
echo -en "${cyan} "
echo -e "${dim} ${num_links}"
dir_length=${#dir_path}
files_length=${#num_files}
dirs_length=${#num_dirs}
links_length=${#num_links}
linewidth=$((dir_length + files_length + dirs_length + links_length + 21))
echo -e -n "${l_black}┍"
for (( i=1 ; i<=linewidth; i++ )); do
echo -n "━"
done
echo
eza -Ta -L="$tree_height" --group-directories-first -I=".git" "$dir_path" | awk 'NR > 1'
fi
}
# }}}}
# }}}
# FZF cd {{{
#
dir-nav-fzf() {
# Test for specified dir
dir_name=$(get-dir-h "$1")
if [[ -z "$dir_name" ]]; then
echo -e "Specified Directory ${blue}${1}${reset} does not exist."
fi
dir_path=$(realpath "${dir_name}")
choice=$(fdfind . "$dir_path" --type d | sed "s|${dir_path}/||" | fzf --border-label=" ${dir_path}/ " --preview "eza --tree -la --icons -F -L=2 --group-directories-first --no-filesize --no-user --no-time --no-permissions {}")
if [[ -n "$choice" ]]; then
cd "${dir_path}/${choice}" || return 2
clear
fi
}
dir-nav-fzf-hid() {
# Test for specified dir
dir_name=$(get-dir-h "$1")
if [[ -z "$dir_name" ]]; then
echo -e "Specified Directory ${blue}${1}${reset} does not exist."
fi
dir_path=$(realpath "${dir_name}")
choice=$(fdfind . "$dir_path" -H --type d | sed "s|${dir_path}/||" | fzf --border-label=" ${dir_path}/ " --preview "eza --tree -la --icons -F -L=2 --group-directories-first --no-filesize --no-user --no-time --no-permissions {}")
if [[ -n "$choice" ]]; then
cd "${dir_path}/${choice}" || return 2
clear
fi
}
d () {
# Test for directory path -> default to home
dir_name=$(get-dir-h "$1")
if [[ -z "${dir_name}" ]]; then
echo -e "Specified Directory ${blue}${1}${reset} does not exist."
echo "false"
return 2
fi
dir_path=$(realpath "${dir_name}")
choice="notempty"
dir_traversal="false"
# Run fzf to traverse directories
while [[ -n "${choice}" ]]; do
choice=$(fdfind . "$dir_path" -H --type d | sed "s|${dir_path}/||" | fzf --border-label=" ${dir_path}/ " --preview "eza --tree -l --icons -F -L=2 --group-directories-first --no-filesize --no-user --no-time --no-permissions ${dir_path}/{}")
if [[ -n "${choice}" ]]; then
dir_traversal="true"
cd "${dir_path}/${choice}" || return 2
dir_path=$(realpath "${dir_path}/${choice}")
dir_number=$(fdfind . "${dir_path}" --type d --max-depth 1 | wc -l)
# stop loop if ther are no more directories to traverse
if [[ "${dir_number}" -le 0 ]]; then
choice=""
fi
fi
done
# Print data if neccessary
if [[ "${dir_traversal}" == "true" ]]; then
clear
# Git repo?
repo_path=$(git rev-parse --show-toplevel 2> /dev/null)
# account for extra height of script output
if [[ -n "${repo_path}" ]]; then
repo-header
header_size=$(repo-header | wc -l)
extra_lines=$((header_size + 1))
else
extra_lines=1
fi
dir-overview . "${extra_lines}"
echo
fi
}
# Fast cd to my repos
# r() {
# choice=$(\ls /home/rc/Repos/ | fzf --height=100% --header-first --border-label=" Git Repositories " --reverse --preview "eza --tree -l --icons -F -L=2 --group-directories-first --no-filesize --no-user --no-time --no-permissions ${HOME}/Repos/{}")
#
# if [ -n "$choice" ]; then
# base_dir="/home/rc/Repos/${choice}"
# cd "${base_dir}" || return 2
# clear
#
# dir_number=$(fdfind . "${base_dir}" --type d --max-depth 1 | wc -l)
# if [[ "$dir_number" -gt 0 ]]; then
# dir-nav-fzf "${base_dir}"
# fi
#
# repo-header
#
# header_size=$(repo-header | wc -l)
# extra_lines=$((header_size + 1))
#
# dir-overview . "${extra_lines}"
# echo
# fi
# }
# Fast cd through notes
n() {
choice=$( fdfind . "$HOME/Repos/notes-ril" --type d | sed 's|/home/rc/Repos/notes-ril/||' | fzf --border-label=" Search Notes Directory " --header-first --reverse --preview "eza --tree -la --icons -F -L=2 --group-directories-first --no-filesize --no-user --no-permissions --no-time ${HOME}/Repos/notes-ril/{}")
if [ -n "$choice" ]; then
cd "${HOME}/Repos/notes-ril/${choice}" || return 2
clear
dir-overview
fi
}
# }}}
# FZF packages {{{
# debian packages
ddf() {
choice=$(apt-cache -n search '.*' | awk -F ' - ' '{print $1}' | fzf --height=100% --header-first --reverse --prompt='▶ ' --pointer='' --multi --preview-window="down" --preview 'apt show {}')
if [ -n "$choice" ]; then
echo "Installing ${choice}" | clr cyan
sudo nala install "${choice}"
fi
}
# pacman searching
pacf() {
choice=$(pacman -Slq | fzf --header-first --reverse --prompt='▶ ' --pointer='' --preview 'pacman -Si {}')
if [ -n "$choice" ]; then
echo "Installing $choice" | clr green
sudo pacman -S "${choice}"
fi
}
# AUR searching
aurf() {
choice=$(paru -a -Slq | fzf --header-first --reverse --prompt='▶ ' --pointer='' --preview 'paru -Si {}')
if [ -n "$choice" ]; then
echo "Installing $choice" | clr green
paru "${choice}"
fi
}
flatf() {
choice=$(flatpak remote-ls flathub | awk '{ printf "%s %s\n", $1, $2, $3 }' | fzf --header-first --reverse --prompt='▶ ' --pointer='' --preview $'flatpak remote-info flathub "$(echo {} | awk \'{printf "%s", $2}\')"')
if [ -n "$choice" ]; then
echo "Installing $choice" | clr green
# flatpak install flathub "${choice}"
fi
}
# }}}
# Navigation {{{
# Moving up directories
up() {
local d=""
local limit="$1"
# Default to limit of 1
if [ -z "$limit" ] || [ "$limit" -le 0 ]; then
limit=1
fi
for ((i = 1; i <= limit; i++)); do
d="../$d"
done
# perform cd. Show error if cd fails
if ! cd "$d"; then
echo "Couldn't go up $limit dirs."
fi
}
# }}}
# File Manipulation {{{
# EXtractor for all kinds of archives
# usage: ex <file>
ex() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.deb) ar x $1 ;;
*.tar.xz) tar xf $1 ;;
*.tar.zst) tar xf $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# }}}
# Vim options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# vim: ts=4 sts=4 sw=4 et
# vim:fileencoding=utf-8:foldmethod=marker