-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
790 lines (706 loc) · 15.1 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#
# bashrc/functions
#
# all manner of goodies and nonsense
#
######
# scold ()
#
# cry foul
# (print to stderr)
#
# 2019-09-19
# 4strid (Astrid Ivy)
scold () {
echo "$*" 1>&2
}
# attempt ()
#
# don't cry over spilled milk
#
# 2019-09-19
# 4strid (Astrid Ivy)
attempt () {
$@ 2>/dev/null
}
# tryhard ()
#
# tryhard mode: activated
#
# tries common places for when I forget to type the start
# of a path. `tryhard cd etc' becomes `cd /etc' and
# `tryhard vim .bashrc' works from anywhere
#
# Usage: tryhard [options] CMD TARGET
#
# 4strid (Astrid Ivy)
# 2019-02-14
# TODO: add .tryhardignore config file (mostly for .git and node_modules lol)
tryhard () {
local FILES=1
local DIRS=1
local RECURSE=0
local VERBOSE=0
local GLOB=0
local MULTI=0
local -a _TRY_COMMON=("$HOME" "/")
local -a _TRY_CFG=("/etc")
local -a TRY
[[ -e $HOME/.tryhardrc ]] && source $HOME/.tryhardrc
OPTIND=1
while getopts "fdaD:ceuUrgGmvVh" OPT; do
case $OPT in
f) FILES=1; DIRS=0 # files
;;
d) FILES=0; DIRS=1 #directories
;;
a) FILES=1; DIRS=1 # all
;;
D)
TRY=("$OPTARG")
;;
c)
TRY+=("${_TRY_COMMON[@]}")
;;
e)
TRY+=("${_TRY_CFG[@]}")
;;
u)
TRY+=("..")
;;
U)
TRY+=(".." "../.." "../../..")
;;
r)
RECURSE=1
[[ ! $TRY ]] && TRY=(".")
;;
g)
GLOB=1
;;
G)
GLOB=2
;;
m)
MULTI=1
;;
v)
VERBOSE=1
;;
V)
VERBOSE=2
;;
h)
cat <<EOF
Usage: tryhard CMD TARGET
Options:
-f TARGET should be a file
-d TARGET should be a directory
[-a] TARGET may be a file or a directory
[-c] Search in common places (by default . ~ and /)
-e Search in configuration places (by default /etc)
-u Search up one parent directory
-U Search up to 3 levels of parent directories
-g Adds a glob star to the end of TARGET
-G Adds a glob star to both ends of TARGET
-D DIR Use DIR as base search directory
-r Recursively search. If search directory is not explicitly
provided, starts in current working directory
-m Perform CMD on all found TARGETs rather than stopping at the first one
-v Prints full path of resolved target(s) as it goes along
-V Prints full path of resolved target(s) and additional debug information
-h Print this help message
Files:
~/.tryhardrc Specify default directories to search by setting
_TRY_COMMON for -c (default) and _TRY_CFG for -e flags
EOF
;;
?)
# moving on
;;
esac
done
shift $(($OPTIND - 1))
[[ ! $TRY ]] && TRY=( "${_TRY_COMMON[@]}" )
local CMD=${@:1:$#-1}
local TARGET="${@:$#}"
local DONE=
[[ ! $TARGET ]] && {
$CMD
return $?
}
_try () {
for T in $@ ; do
TT="${TARGET} ${T}/${TARGET}"
# "//" just kind of bugs me lol
[[ $T == "/" ]] && TT="/$TARGET"
(( $GLOB == 1 )) && TT="${TT}*"
(( $GLOB == 2 )) && TT="${T}/*${TARGET}*"
(( $VERBOSE >= 2 )) && {
echo "DEBUG T $T" 1>&2
echo "DEBUG TT $TT" 1>&2
}
for TG in $TT ; do
(( $VERBOSE >= 2 )) && echo "DEBUG TG $TG" 1>&2
if (( $DIRS )); then
if [[ -d "$TG" ]]; then
(( $VERBOSE )) && [[ $TG != $TARGET ]] && echo "$TG" 1>&2
$CMD "$TG"
DONE=$?
(( $MULTI )) || return $DONE
fi
fi
if (( $FILES )); then
if [[ -f "$TG" ]]; then
(( $VERBOSE )) && [[ $TG != $TARGET ]] && echo "$TG" 1>&2
$CMD "$TG"
DONE=$?
(( $MULTI )) || return $DONE
fi
fi
done
# TODO: breath first instead of depth first search
if (( $RECURSE )); then
local -a SUBS
if [[ -d $T ]]; then
SUBS+=("$T/*/")
_try "${SUBS[@]}"
(( $MULTI )) || {
[[ $DONE ]] && return $DONE
}
fi
fi
done
}
_try "${TRY[@]}"
[[ $DONE ]] && return $DONE
# didn't find it; let CMD print an appropriate error message
$CMD "$TARGET";
}
# tryfind ()
#
# "FIND THEM, YOU IDIOTS!" - a supervillain, probably
#
# Usage: tryfind [OPTIONS] TARGET [...TARGETS]
#
# (shoutouts to GNU sort <3)
#
# dependencies
# tryhard
#
# 4strid (Astrid Ivy)
# 2019-08-20
tryfind () {
local TYPE=
local BASEDIR="-D ."
local MULTI="-m"
local RECURSE="-r"
local GLOB=
local UP=
local VERBOSE=
OPTIND=1
while getopts "fdaD:1RguUvVh" OPT; do
case $OPT in
f) TYPE="-f" ;;
d) TYPE="-d" ;;
a) TYPE="-a" ;;
D) BASEDIR="-D $OPTARG";;
1) MULTI="" ;;
g) GLOB="-g" ;;
G) GLOB="-G" ;;
R) RECURSE="" ;;
v) VERBOSE="-v" ;;
V) VERBOSE="-V" ;;
h) cat <<EOF
Usage: tryfind [options] TARGET [...TARGETS]
Options:
-f Search only for files
-d Search only for directories
-D Base directory to search in
-1 Only return first match
-R Do not recursively search subdirectories
-g Add globstar to the end of filename
-u Also search directory above
-U Also search up to 3 directories above
-v Print all search paths
-h Print this help message
See also: tryhard () on which this is based
EOF
return 0
;;
?)
;;
esac
done
shift $(($OPTIND -1))
local FLAGS="$TYPE $MULTI $RECURSE $GLOB $VERBOSE"
# lmao we use `sort' just to remove duplicates because I couldn't figure another way out
# it's *pretty damn silly* that it had the exact functionality i needed though
tryhard "$BASEDIR" $FLAGS ls -d -1 "$@" | sed 's/[ \t]*$//' | sort -u
}
lastcmd () {
history | tail -2 | head -1 | sed 's/^ *[0-9]*//'
}
# doless ()
#
# once more, with feeling
#
# convenience command for rerunning command
#
# Usage: doless
#
# dependencies
# lastcmd
#
# 2019-11-16
# 4strid (Astrid Ivy)
doless () {
local LASTCMD=`lastcmd`
local COLOR=
[[ $LASTCMD == "ls*" ]] && COLOR=
(attempt $LASTCMD --color=always || $LASTCMD) | less -R
}
# inception ()
#
# A shell within a shell within a...
#
# Returns 0 if not in a subshell, otherwise prints depth
# and returns that number as an error code
#
# 4strid (Astrid Ivy)
# 2019-01-21
inception () {
local DEPTH=$(($SHLVL - 1))
#[[ $TERM == xterm ]] &&
#[[ ! $SSH_CONNECTION ]] &&
#[[ ! $VIM_TERMINAL ]] &&
#DEPTH=$(($DEPTH - 2))
# this APPEARs to only be set in xterm
# and conveniently, not in an SSH session!
#[[ $XTERM_VERSION ]] && DEPTH=$(($DEPTH - 2))
# 2022-11 new canary
#[[ $XTERM_LOCALE ]] && DEPTH=$(($DEPTH - 2))
# nvm ... for some reason this is already 1 in xterm
# conveniently , alacritty also sets a useful
# canary we can use ><
[[ $ALACRITTY_LOG ]] && DEPTH=$(($DEPTH - 2))
(( $DEPTH == 0 )) || echo "$DEPTH>"
return $DEPTH
}
# ok? ()
#
# ok desuka?
#
# boo I used to be able to call this ok? but it
# gets mad now
#
# Forwards exit code of last executed command
# prints the code and an error message if not 0
#
# Usage: $ <TEST_CMD> ; ok [MESSAGE...]
#
# 4strid (Astrid Ivy)
# 2019-01-21
ok () {
local OK=$?
if (( $# )); then
local MSG="$*"
else
local MSG='not ok'
fi
if (( $OK != 0 )); then
echo $OK
echo $MSG 1>&2
fi
return $OK
}
# is_tty ()
#
# Hacky way to check if current shell is a tty.
# Really we should also check inception but w/e
#
# 2019-02-13
is_tty () {
local TTY_REGEX='^/dev/tty[0-9]+'
if [[ `tty` =~ $TTY_REGEX ]]; then
return 0
else
return 1
fi
}
# cd+ ()
#
# one of those rare, mysterious 800MB CDs you can never find anymore
#
# Usage: cd [directories...]
# If omitted, goes to home directory
# Otherwise treats each word as a segment running prefix before attempting each segment. Fails fast, any bad segment will cause it to return to starting PWD.
#
# Environment: Relies on $_CD_MOD_PRE and $_CD_MOD_POST variables
# for configuration. Prefixes cd+ command with PRE variable @-expanded
# and then runs commands in $_CD_MOD_POST. Commands are separated by --
#
# dependencies
# ...*technically* none, but it's sort of worthless without tryhard in $CD_MOD_PRE
#
# 4strid (Astrid Ivy)
# 2019-02-15
cd+ () {
local -a DEST
local -a PRE
local -a POST
local FAILED=0
local START="$PWD"
local PREV="$OLDPWD"
local PREV2="$OLDPWD2"
# woo extra step of history
if [[ $1 == "--" ]]; then
DEST=("$PREV2")
elif (( $# )); then
DEST=("$@")
else
DEST=("$HOME")
fi
[[ _CD_MOD_PRE ]] && PRE+=(${_CD_MOD_PRE[@]})
[[ _CD_MOD_POST ]] && POST+=(${_CD_MOD_POST[@]})
# run prefix on each segment of path
for SEG in ${DEST[@]} ; do
# fail fast
(( $FAILED )) || {
${PRE[@]} cd "$SEG"
FAILED=$?
}
done
# go back if something went wrong
if (( $FAILED )); then
cd "$START"
echo "You can't get there from here" 1>&2
export OLDPWD="$PREV"
return 127
# run post commands
else
local -a CMD
local ERROR=
for FRAG in ${POST[@]} ; do
if [[ "$FRAG" != '--' ]] ; then
CMD+=("$FRAG")
else
# run command
${CMD[@]} || ERROR=$?
[[ $ERROR ]] && return $ERROR
# make room for next command
CMD=()
fi
done
# final remaining command
[[ $CMD ]] && ${CMD[@]}
export OLDPWD2="$PREV"
export OLDPWD="$START"
fi
}
#
# cdmod ()
#
# Turn it up a notch!
#
# (lol this is honestly extremely overkill, but i guess i *really* wanted cd+ to be general purpose)
# (like seriously, why on earth do i not just set _CD_MOD_PRE and _CD_MOD_POST directly ???)
#
# 2019-02-14
# 4strid (Astrid Ivy)
cdmod () {
local -a PRE=()
local -a POST=()
PRE+=(${_CD_MOD_PRE[@]})
POST+=(${_CD_MOD_POST[@]})
local VAR=
for ARG ; do
case $ARG in
'-l')
echo '$_CD_MOD_PRE'
echo ${_CD_MOD_PRE[@]}
echo '$_CD_MOD_POST'
echo ${_CD_MOD_POST[@]}
return 0
;;
'-p')
VAR=pre
PRE=()
;;
'-P')
VAR=post
POST=()
;;
'-h')
cat <<EOF
usage: cdmod [-p|P] MODS... [ [-p|P]] MODS... ]
Nice way to build $_CD_MOD_PRE and $_CD_MOD_POST environment variables
for use with cd+ function.
Options:
-p MODS... Save all subsequent arguments to prefix mod (excepting -P)
-P MODS... Save all subsequent arguments to post mods
-h Print this help message
EOF
return 0
;;
*)
# escape hatch in case you need to send a literal -p or -P
[[ $ARG == "'-p'" ]] && ARG="-p"
[[ $ARG == "'-P'" ]] && ARG="-P"
case $VAR in
pre)
PRE+=("$ARG")
;;
post)
POST+=("$ARG")
;;
*)
echo Not sure what you are trying to modify
echo Use -h option for usage.
return 1
;;
esac
;;
esac
done
export _CD_MOD_PRE=( ${PRE[@]})
export _CD_MOD_POST=( ${POST[@]})
}
# cat_or_ls ()
#
# 4strid (Astrid Ivy)
# 2019
cat_or_ls () {
if [[ ! $1 ]]; then
$CAT
elif [[ -f $1 ]]; then
$CAT "$@"
elif [[ -d $1 ]]; then
$LS "$@"
else
echo "$1: Not a file or directory" 1>&2
return 127
fi
}
# ls_or_cat ()
#
# 4strid (Astrid Ivy)
# 2019
ls_or_cat () {
if [[ ! $1 ]] || [[ $1 = -* ]]; then
$LS --hide="lost+found" "$@"
elif [[ -d $1 ]]; then
$LS "$@"
elif [[ -f $1 ]]; then
$CAT "$@"
else
echo "$1: Not a file or directory" 1>&2
return 127
fi
}
# vim_or_cd ()
#
# oh bash, you know me so well
#
# 4strid (Astrid Ivy)
# 2019
vim_or_cd () {
if [[ $1 == -* ]]; then
$EDITOR $@
elif [[ ! "$1" ]]; then
$EDITOR
elif [[ -d "$1" ]]; then
cd+ "$1"
else
$EDITOR "$@"
fi
}
# cd_or_vim
#
# you know me all too well
#
# 4strid (Astrid Ivy)
# 2019
cd_or_vim () {
if [[ ! $1 ]]; then
cd+
elif [[ -f $1 ]]; then
$EDITOR "$@"
else
cd+ "$@"
fi
}
# determine if charging or not
#
# dependencies
# attempt
# /bin/acpi
#
# 4strid (Astrid Ivy)
# 2019
_bat_match () {
[[ `attempt acpi -i` =~ (Discharging|Charging|Unknown|Not charging),\ ([0-9]{1,3})%.*(remaining|until\ charged|.) ]]
}
# used in my terminal prompt. The @ sign is a battery indicator ^_^
#
# dependencies
# _bat_match
#
# 4strid (Astrid Ivy)
# 2019
batcolor () {
_bat_match
local CHARGE=${BASH_REMATCH[1]}
local NUM=${BASH_REMATCH[2]}
# hardcoded to pink for now
local COLOR=$'\033[1;35m' # "bold purple"
[[ $BASH_REMATCH ]] && {
if [[ $CHARGE == Discharging ]]; then
if (( $NUM <= 14 )); then
COLOR=$'\033[1;31m' # Critical = red
elif (( $NUM <= 42 )); then
COLOR=$'\033[1;33m' # Warning = yellow
else
COLOR=$'\033[1;32m' # Still Good = green
fi
elif [[ $CHARGE == 'Unknown' ]]; then
COLOR=$'\033[1;34m' # Unknown = purple
else
if (( $NUM <= 86 )); then
COLOR=$'\033[0;34m' # Charging = blue
fi
fi
}
echo $COLOR
}
# like acpi but with colors
#
# dependencies
# _bat_match
# batcolor
#
# 4strid (Astrid Ivy)
# 2019
bat () {
_bat_match
local MSG=$BASH_REMATCH
[[ ! $MSG ]] && MSG=`acpi -i`
local RESET=$'\033[0m'
echo `batcolor`${MSG}${RESET}
}
# whatbranch ()
#
# bash/git integration that's "good enough"
#
# dependencies
# /bin/git
# /bin/grep
#
# 4strid (Astrid Ivy)
# 2019-08-19
whatbranch () {
git branch 2>/dev/null | grep -s --no-filename '*' || :
}
# setadd ()
#
# don't ever add me or my son to the $PATH again
#
# 4strid (Astrid Ivy)
# 2019-09-15
setadd () {
local SET="$1"
shift
for ADD in "$@" ; do
if [[ ! $SET ]]; then
SET="$ADD"
elif [[ $SET != *$ADD* ]]; then
SET="$SET:$ADD"
fi
done
echo $SET
}
# somewhat misguided first try at setadd
# (left as a relic of abusing eval to set variables)
#
# 4strid (Astrid Ivy)
# 2019-09-15
setappend () {
local SETNAME="$1"
local SETREF="\$$SETNAME"
local SET=`eval echo $SETREF`
local VALUE="$2"
if [[ ! $SET ]]; then
eval $SETNAME=$VALUE
elif [[ $SET != *$VALUE* ]]; then
eval $SETNAME="$SET:$VALUE"
fi
}
#
# screenshot
#
# take a picture, it'll last longer
#
# dependencies
# /bin/xwd
# /bin/convert
#
# 4strid (Astrid Ivy)
# 2019-11-17
#
# (duplicated in ~/bin/screenshot)
screenshot () {
local ONE=0
local DIR="$HOME/doc/screenshots"
local FILE="${DIR}/`date +%F-%I.%M.%S-%P`.png"
OPTIND=1
while getopts "1D:" OPT; do
case $OPT in
1)
ONE=1
;;
D)
DIR=$OPTARG
;;
*)
return 1
;;
esac
done
[[ ! -d $DIR ]] && mkdir -p "$DIR"
if (( $ONE )); then
xwd | convert xwd:- png:- > $FILE
else
xwd -root -display :0 | convert xwd:- png:- > $FILE
fi
}
#
# sequence
#
# dependencies:
# - attempt
#
# spits out each file of a directory in a sequence
#
# 4strid (Astrid Ivy)
# 2019-11-17
#
sequence () {
local DIR="$1"
local SEQ="$1/.sequence"
local TEMP="$1/.temp"
local NEXT=
if [[ -d $DIR ]]; then
if [[ ! -f $SEQ ]]; then
ls -1 $DIR > $SEQ
fi
NEXT=`head -n 1 $SEQ`
tail -n +2 $SEQ > $TEMP
mv -f $TEMP $SEQ
echo $NEXT >> $SEQ
echo "$DIR/$NEXT"
#( eventually 10 rm $SEQ 2>/dev/null & )
return 0
fi
echo "usage: sequence <dir>"
return 127
}