-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgb
executable file
·867 lines (760 loc) · 28.8 KB
/
gb
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/gb
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/gb
## https://davoudarsalani.ir
function get_datetime { ## {{{
case "$1" in
ymdhms )
printf '%(%Y%m%d%H%M%S)T\n' ;; ## PREVIOUSLY: printf '%s\n' "$(date +%Y%m%d%H%M%S)"
ymd )
printf '%(%Y%m%d)T\n' ;; ## PREVIOUSLY: printf '%s\n' "$(date +%Y%m%d)"
hms )
printf '%(%H%M%S)T\n' ;; ## PREVIOUSLY: printf '%s\n' "$(date +%H%M%S)"
seconds )
printf '%(%s)T\n' ;; ## PREVIOUSLY: printf '%s\n' "$(date +%s)"
weekday )
printf '%(%A)T\n' ;; ## PREVIOUSLY: printf '%s\n' "$(date +%A)"
jymdhms )
printf '%s\n' "$(jdate +%Y%m%d%H%M%S)" ;;
jymd )
printf '%s\n' "$(jdate +%Y%m%d)" ;;
jhms )
printf '%s\n' "$(jdate +%H%M%S)" ;;
jseconds )
printf '%s\n' "$(jdate +%s)" ;;
jweekday )
printf '%s\n' "$(jdate +%A)" ;;
esac
}
## }}}
function heading { ## {{{
source "$HOME"/main/scripts/gb-color
echo -e "$(green "$@")" ## JUMP_2 can't use printf as it returns literal foo\nbar if 'foo\nbar' is passed to it
}
## }}}
function flag { ## {{{
source "$HOME"/main/scripts/gb-color
echo -e "$(purple "$@")" ## JUMP_2 can't use printf as it returns literal foo\nbar if 'foo\nbar' is passed to it
}
## }}}
function action_now { ## {{{
source "$HOME"/main/scripts/gb-color
echo -e "$(green '→') $(grey "$@")" ## JUMP_2 can't use printf as it returns literal foo\nbar if 'foo\nbar' is passed to it
}
## }}}
function accomplished { ## {{{
source "$HOME"/main/scripts/gb-color
echo -e "$(green '✔') $(grey "$@")" ## JUMP_2 can't use printf as it returns literal foo\nbar if 'foo\nbar' is passed to it
}
## }}}
function wrap_fzf_choice { ## {{{
source "$HOME"/main/scripts/gb-color
printf '%s %s %s\n' "$(brown '--=[')" "$@" "$(brown ']=--')"
}
## }}}
function select_file { ## {{{
## NOTE stolen from "$HOME"/.fzf/shell/key-bindings.bash with modifications
## any change you make here, make sure to apply the changes there too
## NOTE used also in "$HOME"/.config/lfrc
local item
eval "$FZF_CTRL_T_COMMAND" | sed "s#$HOME#~#" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf "$@" | \
while read -r item; do
item="$(head -2 <<< "$item" | tail -1)"
printf '%s\n' "${item/\~/$HOME}"
done
}
## }}}
function select_directory { ## {{{
## NOTE stolen from "$HOME"/.fzf/shell/key-bindings.bash with modifications
## any change you make here, make sure to apply the changes there too
## NOTE used also in "$HOME"/.config/lfrc
## usages: this function can accept two args to limit results
## dir="$(select_directory)"
## dir="$(select_directory 'git')"
## dir="$(select_directory 'git' 'public')"
local dir
[ "$1" == 'git' ] && { ## display git repositories only
[ "$2" ] && FZF_ALT_C_COMMAND_GIT+=" | \grep "$2"" ## display specific git repositories only
FZF_ALT_C_COMMAND="$FZF_ALT_C_COMMAND_GIT"
}
dir="$(eval "$FZF_ALT_C_COMMAND" | sed "s#$HOME#~#" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS --header ''" fzf)"
[ "$dir" ] && printf '%q\n' "${dir/\~/$HOME}"
}
## }}}
function pipe_to_fzf { ## {{{
## usages:
## item="$(pipe_to_fzf "${items[@]}")"
## item="$(pipe_to_fzf "${items[@]}" 'header=my header')"
local header last regex
declare -a received=( "$@" )
last="${received[-1]}"
regex='^header='
## remove last member if it is a header
[[ "$last" =~ $regex ]] && {
header="${last#*=}" ## JUMP_3 "${last/$regex}" works too but the latter one does not work for pipe_to_rofi and pipe_to_dmenu so we picked this way
unset 'received[-1]'
}
if [ "$header" ]; then
printf '%s\n' "${received[@]}" | fzf --header "$header"
else
printf '%s\n' "${received[@]}" | fzf
fi
}
## }}}
function pipe_to_rofi { ## {{{
## usages:
## item="$(pipe_to_rofi "${items[@]}")"
## item="$(pipe_to_rofi "${items[@]}" 'header=my header' 'bg=red')" ## NOTE header comes first
## item="$(pipe_to_rofi "${items[@]}" 'header=my header')"
## item="$(pipe_to_rofi "${items[@]}" 'bg=red')"
local last theme header bg
declare -a received=( "$@" )
last="${received[-1]}"
bg_regex='^bg='
header_regex='^header='
theme='onedark.rasi'
## NOTE as bg is passed as last arg, bg_regex should be checked first (e.i. before header_regex)
[[ "$last" =~ $bg_regex ]] && {
bg="${last#*=}" ## JUMP_3 "${last/$bg_regex}" didn't work
unset 'received[-1]'
theme='onedark-red.rasi'
}
last="${received[-1]}"
[[ "$last" =~ $header_regex ]] && {
header="${last#*=}" ## JUMP_3 "${last/$header_regex}" didn't work
unset 'received[-1]'
}
printf '%s\n' "${received[@]}" | rofi -theme "$HOME"/.config/rofi/"$theme" -dmenu -i -p "$header"
}
## }}}
function pipe_to_dmenu { ## {{{
## usages:
## item="$(pipe_to_dmenu "${items[@]}")"
## item="$(pipe_to_dmenu "${items[@]}" 'header=my header' 'bg=red')" ## NOTE header comes first
## item="$(pipe_to_dmenu "${items[@]}" 'header=my header')"
## item="$(pipe_to_dmenu "${items[@]}" 'bg=red')"
local last header bg dmenusb
declare -a received=( "$@" )
last="${received[-1]}"
bg_regex='^bg='
header_regex='^header='
theme='onedark.rasi'
## NOTE as bg is passed as last arg, bg_regex should be checked first (e.i. before header_regex)
[[ "$last" =~ $bg_regex ]] && {
bg="${last#*=}" ## JUMP_3 "${last/$bg_regex}" didn't work
unset 'received[-1]'
dmenusb="$bg"
}
last="${received[-1]}"
[[ "$last" =~ $header_regex ]] && {
header="${last#*=}" ## JUMP_3 "${last/$header_regex}" didn't work
unset 'received[-1]'
}
printf '%s\n' "${received[@]}" | dmenu -i -l "$dmenulines" -nb "$dmenunb" -nf "$dmenunf" -sb "$dmenusb" -sf "$dmenusf" -fn "$dmenufn" -p "$header"
}
## }}}
function lsblk_full { ## {{{
\lsblk -o NAME,LABEL,SIZE,UUID,FSTYPE,TYPE,MOUNTPOINT,OWNER,GROUP,MODE,MAJ:MIN,RM,RO
}
## }}}
function mounted_drives { ## {{{
if [ "$1" ]; then
printf '%s\n' "$(df -hT)"
else
printf '%s\n' "$(df -T -x devtmpfs -x tmpfs -x usbfs -x loop)" ## -x ARG instances help get rid of unnecessary drives
fi
}
## }}}
function get_mountable_umountable { ## {{{
readarray -t mountable < <(\lsblk -nro NAME,LABEL,SIZE,UUID,FSTYPE,TYPE,MOUNTPOINT | \grep -iE 'part $|rom $' | \grep -viE ' swap part $|^nvme|^sda|^sdb')
readarray -t umountable < <(\lsblk -nro NAME,LABEL,SIZE,UUID,FSTYPE,TYPE,MOUNTPOINT | \grep -i '/tmp/')
}
## }}}
function msgn { ## {{{
notify-send --urgency=normal "$1" "$2" -i "$3" -t 10000
}
## }}}
function msgc { ## {{{
notify-send --urgency=critical "$1" "$2" -i "$3"
}
## }}}
function copy_pacman_log { ## {{{
source "$HOME"/main/scripts/gb-color
cp /var/log/pacman.log "$HOME"/main/linux/pacman.log
msgn "<span color=\"${orange}\">/var/log/pacman.log</span> copied"
}
## }}}
function clear_playlist { ## {{{
audtool --playlist-clear
}
## }}}
function turn_off_shuffle { ## {{{
source "$HOME"/main/scripts/gb-audacious
[ "$shuffle_status" == 'on' ] && audtool --playlist-shuffle-toggle
}
## }}}
function turn_on_shuffle { ## {{{
source "$HOME"/main/scripts/gb-audacious
[ "$shuffle_status" == 'off' ] && audtool --playlist-shuffle-toggle
}
## }}}
function play_shutdown_track { ## {{{
source "$HOME"/main/scripts/gb-audio
pacmd play-file "$HOME"/main/linux/sounds/shutdown.ogg "$def_sink_index"
}
## }}}
function relative_date { ## {{{
## usage:
## relative_date '2019-05-03T07:07:11Z'
## relative_date 1556867231
local start_date_in_seconds now_in_seconds
if [ "$1" -eq "$1" ] 2>/dev/null; then
start_date_in_seconds="$1"
else
start_date_in_seconds="$(convert_to_second "$1")"
fi
now_in_seconds="$(get_datetime 'seconds')"
printf '%s ago\n' "$(convert_second "(( now_in_seconds - start_date_in_seconds ))" 'verbose')"
}
## }}}
function convert_to_second { ## {{{ convert 2021-04-15T11:10:03+0430 to 1618468803
printf '%s\n' "$(date --date "$1" '+%s')"
}
## }}}
function convert_second { ## {{{
local seconds sec min hour day mont year result
seconds="${1%.*}" ## remove decimal in case arg is a float
if (( seconds < 1 )); then
if [ "$2" == 'verbose' ]; then
result="less than a second"
else
result="00:00:00"
fi
else
sec="$(printf "%02d" "$(( "$seconds" % 60 ))")"
min="$(printf "%02d" "$(( "$seconds" / 60 % 60 ))")"
hour="$(printf "%02d" "$(( "$seconds" / 3600 % 24 ))")"
day="$(printf "%02d" "$(( "$seconds" / 3600 / 24 % 30 ))")"
mont="$(printf "%02d" "$(( "$seconds" / 3600 / 24 / 30 % 12 ))")"
year="$(printf "%02d" "$(( "$seconds" / 3600 / 24 / 30 / 12 ))")"
## NOTE used [ "$var" -eq 0 ] instead of (( var == 0 )) because
## 1. (( var == 0 )) is stupid (i.e. it returns true even if var does not exist)
## 2. ints here are two-digit and (( var == 0 )) can't handle that
if [ "$year" -eq 0 ] && [ "$mont" -eq 0 ] && [ "$day" -eq 0 ]; then
if [ "$2" == 'verbose' ]; then
result="$hour hours, $min minutes and $sec seconds"
else
result="${hour}:${min}:${sec}"
fi
elif [ "$year" -eq 0 ] && [ "$mont" -eq 0 ]; then
if [ "$2" == 'verbose' ]; then
result="$day days, $hour hours, $min minutes and $sec seconds"
else
result="${day}:${hour}:${min}:${sec}"
fi
elif [ "$year" -eq 0 ]; then
if [ "$2" == 'verbose' ]; then
result="$mont months, $day days, $hour hours, $min minutes and $sec seconds"
else
result="${mont}:${day}:${hour}:${min}:${sec}"
fi
else
if [ "$2" == 'verbose' ]; then
result="$year years, $mont months, $day days, $hour hours, $min minutes and $sec seconds"
else
result="${year}:${mont}:${day}:${hour}:${min}:${sec}"
fi
fi
## NOTE the same modifications in JUMP_4 are applied in
## 1. convert_second function in gb.py
## 2. 'relative' method of whatever-its-name-is class in models.py in django app
## so any changes you make here, make sure to update them too
## JUMP_4 remove items whose values are 00, and adjust comma and 'and'
result="$(printf '%s\n' "$result" | \
sed 's|00 [a-z]\+s, ||g' | \
sed 's|00 [a-z]\+s and ||' | \
sed 's|00 [a-z]\+s$||' | \
sed 's|, \([0-9][0-9] [a-z]\+s \)| and \1|' | \
sed 's|and 00 [a-z]\+s ||' | \
sed 's| and $||' | \
sed 's|, \([0-9][0-9] [a-z]\+\)$| and \1|' | \
sed 's| and \([0-9][0-9] [a-z]\+\) and|, \1 and|g' | \
sed 's|, \+$||' | \
sed 's|, \([0-9][0-9] [a-z]\+s\)$| and \1|' \
)"
## JUMP_4 remove plural s when value is 01
result="$(printf '%s\n' "$result" | sed 's|\(01 [a-z]\+\)s |\1 |g;s|\(01 [a-z]\+\)s, |\1, |g;s|\(01 [a-z]\+\)s$|\1|')"
fi
printf '%s\n' "$result"
}
## }}}
function insert_comma { ## {{{
printf "%'d\n" "$1"
}
## }}}
function clear_clipboard { ## {{{
greenclip clear
msgn 'cleared' '' "$HOME"/main/linux/themes/greenclip.png
}
## }}}
function random_wallpaper { ## {{{
local current_wallpaper_file rand_wall
current_wallpaper_file=/tmp/current_wallpaper
## same wallpaper for both screens
rand_wall="$(shuf -n 1 < <(find "$HOME"/main/wallpapers -mindepth 1 -maxdepth 1 -type f -iname '*.jpg'))" ## JUMP_1
printf '%s\n' "${rand_wall##*/}" > "$current_wallpaper_file"
feh --bg-scale "$rand_wall"
}
## }}}
function copy_random_wallpaper_for_startup { ## {{{
source "$HOME"/main/scripts/gb-color
local rand_wall dest_dir
rand_wall="$(shuf -n 1 < <(find "$HOME"/main/wallpapers -mindepth 1 -maxdepth 1 -type f -iname '*.jpg'))" ## JUMP_1
dest_dir=/usr/share/wallpapers
[ -d "$dest_dir" ] || sudo mkdir "$dest_dir"
sudo cp "$rand_wall" "$dest_dir"/startup_background.jpg && \
msgn "<span color=\"${orange}\">${rand_wall##*/}</span> copied"
}
## }}}
function set_widget { ## {{{
## only the awesome-widgets use this function but it'd better be here (rather than the script itself)
## because sometimes it's needed by 0-test
local widget attr value
widget="$1"
attr="$2"
value="$3"
awesome-client "${widget}.${attr} = '${value}'" ## NOTE do NOT change quotes
}
## }}}
function get_input { ## {{{
source "$HOME"/main/scripts/gb-color
local input
unset input
read -p "$(olive "$@") " input
printf '%s\n' "$input"
}
## }}}
function get_single_input { ## {{{
source "$HOME"/main/scripts/gb-color
local single_input
unset single_input
read -p "$(olive "$@") " -n 1 single_input
printf '%s\n' "$single_input"
}
## }}}
function line { ## {{{
source "$HOME"/main/scripts/gb-color
grey "$(printf "%$(tput cols)s\n" | sed "s/ /▬/g")"
}
## }}}
function rand_str { ## {{{
printf '%s\n' "$(mktemp --dry-run | sed 's/.*\.//g')" ## returns e.g. leQPAUyfZP
# sed 's/-//g' < /proc/sys/kernel/random/uuid ## returns e.g. a8a29c0d109a40468051bcbaf70af17e
}
## }}}
function get_kaddy_counterpart { ## {{{
printf '%s\n' "$(sed -e "s|\($HOME\)/main\(\)|\1/kaddy\2|" <<< "$1")"
}
## }}}
function remove_older_pkgs { ## {{{
source "$HOME"/main/scripts/gb
source "$HOME"/main/scripts/gb-color
local src_dir src_kaddy_dir uniq count file exceed idx remove_propmt
declare -a files uniques repeats to_be_removed
declare -i keep=3
src_dir="$HOME"/main/linux-pkg
src_kaddy_dir="$(get_kaddy_counterpart "$src_dir")"
readarray -t files < <(find "$src_dir" -mindepth 1 -maxdepth 1 -type f ! -iname '*.sig' ! -iname '*:*' | sort)
readarray -t uniques < <(printf '%s\n' "${files[@]}" | sed 's|-[0-9]\+[^:]*||g' | sort | uniq --count)
for uniq in "${uniques[@]}"; {
read count file <<< "$uniq" ## 8 /home/nnnn/main/linux-pkg/alsa-card-profiles
(( count <= keep )) && continue
(( exceed="count - keep" )) ## 5
readarray -t repeats < <(find "$src_dir" -mindepth 1 -maxdepth 1 -type f -iname "${file##*/}*" ! -iname '*.sig' ! -iname '*:*' | sort)
for ((idx=0; idx<${exceed}; idx++)); {
to_be_removed+=( "${repeats[${idx}]}" )
}
}
if [ "$to_be_removed" ]; then
printf '%s\n' "${to_be_removed[@]##*/}" | sort | column
remove_propmt="$(get_single_input "remove ${#to_be_removed[@]} pkgs?")" && printf '\n'
case "$remove_propmt" in
y )
action_now "removing from ${src_dir/$HOME/\~}"
rm -v "${to_be_removed[@]}"
action_now "removing from ${src_kaddy_dir/$HOME/\~}"
rm -v "${to_be_removed[@]/$src_dir/$src_kaddy_dir}"
;;
esac
else
red 'nothing to remove'
fi
}
## }}}
function expand_ips_from_ranges { ## {{{
source "$HOME"/main/scripts/gb-color
local ip1 ip2 dest_file ip1_4_new
local ip1_1 ip1_2 ip1_3 ip1_4 ip2_1 ip2_2 ip2_3 ip2_4
[ "$3" ] || {
red "args needed"
red "USAGE: $FUNCNAME <IP1> <IP2> ~/main/downloads/ip_ranges.py "
exit
}
[ -f "$3" ] && {
red "$3 already exists"
exit
}
ip1="$1"
ip2="$2"
dest_file="$3"
ip1_1="$( printf '%s\n' "$ip1" | cut -f 1 -d '.' )"
ip1_2="$( printf '%s\n' "$ip1" | cut -f 2 -d '.' )"
ip1_3="$( printf '%s\n' "$ip1" | cut -f 3 -d '.' )"
ip1_4="$( printf '%s\n' "$ip1" | cut -f 4 -d '.' )"
ip2_1="$( printf '%s\n' "$ip2" | cut -f 1 -d '.' )"
ip2_2="$( printf '%s\n' "$ip2" | cut -f 2 -d '.' )"
ip2_3="$( printf '%s\n' "$ip2" | cut -f 3 -d '.' )"
ip2_4="$( printf '%s\n' "$ip2" | cut -f 4 -d '.' )"
printf 'ips = {\n' > "$dest_file"
while (( ip1_3 <= ip2_3 )); do
ip1_4_new="$ip1_4"
while (( ip1_4_new <= ip2_4 )); do
# printf '%s %s\n' "$ip1_3" "$ip1_4_new"
printf "\t'%s.%s.%s.%s',\n" "$ip1_1" "$ip1_2" "$ip1_3" "$ip1_4_new" >> "$dest_file"
((ip1_4_new++))
done
((ip1_3++))
done
printf '}\n' >> "$dest_file"
}
## }}}
## for website:
function create_timeline_thumbnails { ## {{{
source "$HOME"/main/scripts/gb-color
local src_dir src_dir_base dest_dir vid_base vid_name
declare -a vids
[ "$1" ] || {
red "arg needed"
red "USAGE: $FUNCNAME ~/main/dl/video/bright-side"
exit
}
[ -d "$1" ] || {
red "$1 does not exist"
exit
}
src_dir="$1"
src_dir_base="${src_dir##*/}"
dest_dir=~/main/website/static/files/page/video/00-timeline-thumbnail/"$src_dir_base"
[ -d "$dest_dir" ] && \rm -r "$dest_dir"
\mkdir -p "$dest_dir"
readarray -t vids < <(find "$src_dir" -mindepth 1 -maxdepth 1 -type f -iname "*.mp4" | sort)
for v in "${vids[@]}"; {
vid_base="${v##*/}" ## 003.mp4
printf '%s\n' "$vid_base"
vid_name="${vid_base%%.*}" ## 003
## https://www.youtube.com/watch?v=ZeNyjnneq_w
ffmpeg -i "$v" -vf fps=1/10,scale=120:-1 "$dest_dir"/timeline-thumbnail-"${vid_name}"-%03d.webp 2>/dev/null
}
}
## }}}
function create_exhibit_thumbnails { ## {{{
source "$HOME"/main/scripts/gb
source "$HOME"/main/scripts/gb-color
local src_dir src_dir_base dest_dir vid_base vid_name
declare -a vids first_jpgs
[ "$1" ] || {
red "arg needed"
red "USAGE: $FUNCNAME ~/main/dl/video/bright-side"
exit
}
[ -d "$1" ] || {
red "$1 does not exist"
exit
}
src_dir="$1"
src_dir_base="${src_dir##*/}"
dest_dir=~/main/website/static/files/page/video/00-exhibit-thumbnail/"$src_dir_base"
[ -d "$dest_dir" ] && \rm -r "$dest_dir"
\mkdir -p "$dest_dir"
readarray -t vids < <(find "$src_dir" -mindepth 1 -maxdepth 1 -type f -iname "*.mp4" | sort)
for v in "${vids[@]}"; {
vid_base="${v##*/}" ## 003.mp4
printf '%s\n' "$vid_base"
vid_name="${vid_base%%.*}" ## 003
## https://www.youtube.com/watch?v=ZeNyjnneq_w
ffmpeg -i "$v" -vf fps=1/10,scale=200:-1 "$dest_dir"/exhibit-thumbnail-"${vid_name}"-%03d.jpg 2>/dev/null ## NOTE 200 is also used as 200px in video.css
}
action_now 'converting first jpgs to webp'
readarray -t first_jpgs < <(find "$dest_dir" -mindepth 1 -maxdepth 1 -type f -iname '*-001.jpg'| sort)
for f in "${first_jpgs[@]}"; {
ffmpeg -i "$f" "${f/.jpg/.webp}" 2>/dev/null
}
action_now 'removing jpgs'
\rm "$dest_dir"/*jpg
}
## }}}
function create_waveform_v2 { ## {{{
source "$HOME"/main/scripts/gb-color
## [CHECKING_OS]
[ "$(command -v apt)" ] || {
red 'can be run only on mint at the moment'
red 'due to troubles installing dependencies'
exit
}
local type_ ext src_dir d_base f_base dest_media dest_png dest_js
local -a dirs_ files
type_='audio' ## audio/dictionary
ext='.mp3' ## .mp3
src_dir="$HOME"/main/dl/"$type_"
readarray -t dirs_ < <(find "$src_dir" -mindepth 1 -maxdepth 1 -type d | sort)
for d in "${dirs_[@]}"; {
d_base="${d##*/}" ## 6-minute-english
printf '%s\n' "$d_base"
readarray -t files < <(find "$src_dir"/"$d_base" -mindepth 1 -maxdepth 1 -type f -iname "*$ext" | sort)
for f in "${files[@]}"; {
f_base="${f##*/}" ## 001.mp3
dest_media="$HOME"/main/website/static/files/base/page/"$type_"/"$d_base"/00-waveform-v2/"$f_base"
dest_png="${dest_media/$ext/.png}"
dest_js="${dest_media/$ext/.js}"
## create waveform (https://github.com/andrewrk/waveform)
#
## (for transparent wave with black background)
# --png-color-bg 000000ff \
# --png-color-center 00000000 \
# --png-color-outer 00000000 \
#
## (for creating json containing levels)
# --wjs-width 840 \
# --wjs-precision 4 \
# --waveformjs {dest_js})
#
## (flags for creating json containing levels:
## --wjs-width 840
## --wjs-precision 4
## --waveformjs "$dest_js")
"$HOME"/main/linux/sources/waveform/waveform \
--png-width 840 \
--png-height 128 \
--png-color-center ffffffff \
--png-color-outer ffffffff \
--png "$dest_png" \
\
"$f"
## convert png to webp
## NOTE if statement because some audio files are empty
## leading to the creation of no dest_png in the previous step
if [ -f "$dest_png" ]; then
ffmpeg -i "$dest_png" "${dest_png/.png/.webp}" 2>/dev/null
fi
}
}
}
## }}}
# function find_duplicate_images { ## {{{
# [ "$1" ] || {
# red 'two args arg needed:'
# red "usage: $FUNCNAME SRC_DIR_PATH FILE_EXTENSION (e.g. json)"
# exit
# }
# dest_dir="$1"
# [ -d "$dest_dir" ] || {
# red "no such dir as ${dest_dir}"
# exit
# }
# dups_file=/tmp/duplicates-"$(date '+%s')"
# dups_string=''
# declare -A dups_array
# readarray -t files < <(find "$1" -mindepth 1 -type f -iname "*.${2}" | sort)
# orig_count="${#files[@]}"
# for ((idx=0; idx<"${orig_count}"; idx++)); {
# f_1="${files[$idx]}"
# ## skip comparing if f_1 is already in dups_string
# \grep -q "$f_1" <<< "$dups_string" && continue
# printf '%s/%s %s\n' "$((idx+1))" "${orig_count}" "$f_1"
# for f_2 in "${files[@]}"; {
# ## prevent comparing with itself
# [ "$f_1" == "$f_2" ] || {
# [ "$(diff "$f_1" "$f_2")" ] || {
# printf '%s\n%s removed\n\n' "$f_1" "$f_2" >> "$dups_file"
# dups_string+="${f_1} ${f_2} "
# dups_array+=([${f_1}]="$f_2")
# }
# }
# }
# unset "files[${idx}]"
# (( "${#dups_array[@]}" > 0 )) && printf ' -> %s found\n' "${#dups_array[@]}"
# }
# ## printf duplicates if any
# if (( "${#dups_array[@]}" > 0 )); then ## NOTE dups_array is an associative array and '[ "$dups_array" ] &&' didn't work to check if it's full
# printf '\n\n %s set(s) of duplicates found (file names also saved at %s):\n\n' "${#dups_array[@]}" "$dups_file"
# for key in "${!dups_array[@]}"; {
# rm "${dups_array[${key}]}"
# printf '%s\n%s removed\n\n' "$key" "${dups_array[${key}]}"
# }
# else
# printf '\nok\n'
# fi
# }
# }}}
# function compare_network_speed { ## {{{
# ## this function takes only one arg which can only be either old or new
# source "$HOME"/main/scripts/gb-calculation
# source "$HOME"/main/scripts/gb-color
# declare -a loop_durs loops_durs
# durs_file=/tmp/0-durs
# totals_file=/tmp/0-totals
# loops_count=10
# position="$1"
# [ "$position" == 'old' ] || [ "$position" == 'new' ] || {
# printf '%s\n' "$(red "arg can only be either old or new")"
# exit
# }
# ## if position is old it means we want to have a fresh start
# ## so let's empty totals_file first
# [ "$position" == "old" ] && > "$totals_file"
# for ((i=1; i<="$loops_count"; i++)); {
# printf 'loop %s\n' "$i"
# > "$durs_file"
# for ((j=1; j<="$loops_count"; j++)); {
# { time -p "$HOME"/main/scripts/awesome-widgets network ;} &>>"$durs_file"
# printf '\n' >> "$durs_file"
# sleep 1
# }
# loop_dur=0
# readarray -t loop_durs < <(\grep 'real' "$durs_file" | awk '{print $NF}')
# for a_sm in "${loop_durs[@]}"; {
# loop_dur="$(float_pad "${loop_dur}+${a_sm}" 1 2)"
# }
# printf '%s %s\n' "$position" "$loop_dur" >> "$totals_file"
# }
# loops_dur=0
# readarray -t loops_durs < <(\grep "$position" "$totals_file" | awk '{print $NF}')
# for a_big in "${loops_durs[@]}"; {
# loops_dur="$(float_pad "${loops_dur}+${a_big}" 1 2)"
# }
# printf '%s = %s\n\n' "$position" "$loops_dur" >> "$totals_file"
# ## compare
# old_loops_total="$(\grep 'old =' "$totals_file" | awk '{print $NF}' 2>/dev/null)"
# new_loops_total="$(\grep 'new =' "$totals_file" | awk '{print $NF}' 2>/dev/null)"
# [ "$new_loops_total" ] && [ "$old_loops_total" ] && {
# is_faster="$(compare_floats "$new_loops_total" '<' "$old_loops_total")"
# is_slower="$(compare_floats "$new_loops_total" '>' "$old_loops_total")"
# if [ "$is_faster" == 'true' ]; then
# diff="$(float_pad "${old_loops_total}-${new_loops_total}")"
# msg="$diff faster"
# elif [ "$is_slower" == 'true' ]; then
# diff="$(float_pad "${new_loops_total}-${old_loops_total}")"
# msg="$diff slower"
# else
# msg="$old_loops_total = $new_loops_total"
# fi
# printf 'compare\n%s\n' "$msg" >> "$totals_file"
# }
# }
## }}}
# timer and record_* {{{
# function timer { ## {{{
# source "$HOME"/main/scripts/gb-color
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# start="$(get_datetime 'jhms')"
# for ((i=1; i<="$1"; i++)); {
# current="$(get_datetime 'jhms')"
# (( seconds="current - start" ))
# echo -en '\r \r'
# (( h="seconds / 3600 % 24" ))
# (( m="seconds / 60 % 60" ))
# (( s="seconds % 60" ))
# w="$(printf "$2 %02d:%02d:%02d" "$h" "$m" "$s" ## NOTE no \n ??
# set_widget 'record' 'markup' "<span color=\"${red}\">${record_icon} <b>${w}</b></span>"
# sleep 1
# }
# }
## }}}
# function record_audio { ## {{{
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# timer "$3" "$4" &
# ffmpeg -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# -t "$1" "$2" 2>/dev/null
# }
## }}}
# function record_audio_ul { ## {{{
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# ffmpeg -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# "$1" 2>/dev/null
# }
## }}}
# function record_screen { ## {{{
# source "$HOME"/main/scripts/gb-screen
# timer "$3" "$6" &
# ffmpeg -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# -f x11grab -r 30 -s "$4" -i :0.0+"$5",0 \
# -vcodec libx264 -preset veryfast -crf 18 -acodec libmp3lame -q:a 1 -pix_fmt yuv420p \
# -t "$1" "$2" 2>/dev/null
# }
## }}}
# function record_screen_ul { ## {{{
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# ffmpeg -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# -f x11grab -r 30 -s "$2" -i :0.0+"$3",0 \
# -vcodec libx264 -preset veryfast -crf 18 -acodec libmp3lame -q:a 1 -pix_fmt yuv420p \
# "$1" 2>/dev/null
# }
## }}}
# function record_video { ## {{{
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# timer "$3" "$4" &
# ffmpeg -f v4l2 -framerate 25 -video_size 1366x768 -i "$def_video_dev" \
# -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# -t "$1" "$2" 2>/dev/null
# }
## }}}
# function record_video_ul { ## {{{
# source "$HOME"/main/scripts/gb-screen
# source "$HOME"/main/scripts/gb-audio
# ffmpeg -f v4l2 -framerate 25 -video_size 1366x768 -i "$def_video_dev" \
# -f pulse -i "$def_source_mon_index" \
# -f pulse -i default \
# -filter_complex amix=inputs=2 \
# "$1" 2>/dev/null
# }
## }}}
# function record_mic_mon_on { ## {{{
# source "$HOME"/main/scripts/gb-audio
# unmute_mic
# mic_25
# unmute_mon
# mon_100
# }
## }}}
# function record_mon_on { ## {{{
# source "$HOME"/main/scripts/gb-audio
# unmute_mon
# mon_100
# }
## }}}
# function record_mic_mon_off { ## {{{
# source "$HOME"/main/scripts/gb-audio
# mute_mic
# mic_0
# mute_mon
# mon_0
# }
## }}}
## }}}