-
Notifications
You must be signed in to change notification settings - Fork 3
/
bash-functions
executable file
·794 lines (553 loc) · 13.7 KB
/
bash-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
791
792
793
794
#!/bin/bash
# adb //////////////////////////////////////////////////////////////////////////
adb_download () {
if [[ -z $(command -v adb) ]]
then
frobulator.require adb
fi
}
adb_set_environment () {
if [[ $(command -v adb) ]]
then
export ANDROID_NO_USE_FWMARK=1
if [ ! -d "${HOME}"/.adb ]
then
mkdir -p "${HOME}"/.adb
fi
export ADB_SERVER_SOCKET=localfilesystem:"${HOME}"/.adb/adb_server_socket
fi
}
adb_screenshot () {
adb_download
adb_set_environment
adb shell screencap -p /sdcard/"${screenshot}"
adb pull /sdcard/"${screenshot}" screenshot/"${screenshot}"
adb shell rm /sdcard/"${screenshot}"
}
adb_record () {
adb_download
adb_set_environment
adb shell screenrecord /storage/emulated/0/Videos/"${recording}" &
adb_record_PID=$!
read -r -p 'Press enter to stop recording...'
kill ${adb_record_PID} > /dev/null 2&>1
sleep 0.5
adb pull /sdcard/"${recording}" screenrecord/"${recording}"
adb shell rm /sdcard/"${recording}"
}
adb_pull_package () {
adb_download
adb_set_environment
if [[ ! -d ./apk ]]
then
mkdir -p "${HOME}"/Android
cd "${HOME}"/Android
fi
for package in $(adb shell pm list packages | sed -n 's/^package://p')
do
adb pull "$(adb shell pm path "${package}" | grep /data/app | sed -n "s/^package://p")"
mv base.apk "${package}".apk
done
}
adb_install_package () {
adb_download
adb_set_environment
package="${1}"
adb install "${package}"
}
adb_remove_package () {
adb_download
adb_set_environment
package="${1}"
adb uninstall --user 0 "${package}"
}
adb_settings_get () {
adb_download
adb_set_environment
setting="${1}"
adb shell settings get "${setting}"
}
adb_settings_put () {
adb_download
adb_set_environment
setting="${1}"
adb shell settings put "${setting}"
}
adb_disable_phantom_killer () {
adb_download
adb_set_environment
adb shell /system/bin/device_config set_sync_disabled_for_tests persistent
adb shell /system/bin/device_config put activity_manager max_phantom_processes 2000000000
adb shell /system/bin/settings get global device_config_sync_disabled
}
adb_vendor_ids () {
adb_download
adb_set_environment
mkdir -p "${HOME}"/.android
for id in {0..65535}
do
printf '0x%04x\n' "${id}"
done >> "{HOME}"/.android/adb_usb.ini
chmod -R 777 "${HOME}"/.android
if [[ $(dpkg -l udev) ]]
then
while read -r vendor_id
do
echo SUBSYSTEM==\"usb\", SYSFS{idVendor}==\""${vendor_id:2}"\", MODE=\"0666\" \;
done <<< $(cat "${HOME}"/.android/adb_usb.ini) >> /etc/udev/rules.d/51-android.rules
fi
}
adb_keycode () {
adb_download
adb_set_environment
adb "${device}" shell input keyevent "${1}"
# https://developer.android.com/reference/android/view/KeyEvent.html
# 0 - KEYCODE_0 # 49 - KEYCODE_U
# 1 - KEYCODE_SOFT_LEFT # 50 - KEYCODE_V
# 2 - KEYCODE_SOFT_RIGHT # 51 - KEYCODE_W
# 3 - KEYCODE_HOME # 52 - KEYCODE_X
# 4 - KEYCODE_BACK # 53 - KEYCODE_Y
# 5 - KEYCODE_CALL # 54 - KEYCODE_Z
# 6 - KEYCODE_ENDCALL # 55 - KEYCODE_COMMA
# 7 - KEYCODE_0 # 56 - KEYCODE_PERIOD
# 8 - KEYCODE_1 # 57 - KEYCODE_ALT_LEFT
# 9 - KEYCODE_2 # 58 - KEYCODE_ALT_RIGHT
# 10 - KEYCODE_3 # 59 - KEYCODE_SHIFT_LEFT
# 11 - KEYCODE_4 # 60 - KEYCODE_SHIFT_RIGHT
# 12 - KEYCODE_5 # 61 - KEYCODE_TAB
# 13 - KEYCODE_6 # 62 - KEYCODE_SPACE
# 14 - KEYCODE_7 # 63 - KEYCODE_SYM
# 15 - KEYCODE_8 # 64 - KEYCODE_EXPLORER
# 16 - KEYCODE_9 # 65 - KEYCODE_ENVELOPE
# 17 - KEYCODE_STAR # 66 - KEYCODE_ENTER
# 18 - KEYCODE_POUND # 67 - KEYCODE_DEL
# 19 - KEYCODE_DPAD_UP # 68 - KEYCODE_GRAVE
# 20 - KEYCODE_DPAD_DOWN # 69 - KEYCODE_MINUS
# 21 - KEYCODE_DPAD_LEFT # 70 - KEYCODE_EQUALS
# 22 - KEYCODE_DPAD_RIGHT # 71 - KEYCODE_LEFT_BRACKET
# 23 - KEYCODE_DPAD_CENTER # 72 - KEYCODE_RIGHT_BRACKET
# 24 - KEYCODE_VOLUME_UP # 73 - KEYCODE_BACKSLASH
# 25 - KEYCODE_VOLUME_DOWN # 74 - KEYCODE_SEMICOLON
# 26 - KEYCODE_POWER # 75 - KEYCODE_APOSTROPHE
# 27 - KEYCODE_CAMERA # 76 - KEYCODE_SLASH
# 28 - KEYCODE_CLEAR # 77 - KEYCODE_AT
# 29 - KEYCODE_A # 78 - KEYCODE_NUM
# 30 - KEYCODE_B # 79 - KEYCODE_HEADSETHOOK
# 31 - KEYCODE_C # 80 - KEYCODE_FOCUS
# 32 - KEYCODE_D # 81 - KEYCODE_PLUS
# 33 - KEYCODE_E # 82 - KEYCODE_MENU
# 34 - KEYCODE_F # 83 - KEYCODE_NOTIFICATION
# 35 - KEYCODE_G # 84 - KEYCODE_SEARCH
# 36 - KEYCODE_H # 85 - KEYCODE_MEDIA_PLAY_PAUSE
# 37 - KEYCODE_I # 86 - KEYCODE_MEDIA_STOP
# 38 - KEYCODE_J # 87 - KEYCODE_MEDIA_NEXT
# 39 - KEYCODE_K # 88 - KEYCODE_MEDIA_PREVIOUS
# 40 - KEYCODE_L # 89 - KEYCODE_MEDIA_REWIND
# 41 - KEYCODE_M # 90 - KEYCODE_MEDIA_FAST_FORWARD
# 42 - KEYCODE_N # 91 - KEYCODE_MUTE
# 43 - KEYCODE_O # 92 - KEYCODE_PAGE_UP
# 44 - KEYCODE_P # 93 - KEYCODE_PAGE_DOWN
# 45 - KEYCODE_Q # 94 - KEYCODE_PICTSYMBOLS
# 46 - KEYCODE_R # [ ... ]
# 47 - KEYCODE_S # 122 KEYCODE_MOVE_HOME
# 48 - KEYCODE_T # 123 KEYCODE_MOVE_END
}
adb_device () {
adb_download
adb_set_environment
if [ $# -gt 1 ]
then
device="-s ${@:2}"
adb connect ${@:2}
sleep 2
fi
}
adb_write () {
adb_download
adb_set_environment
hostname=$(adb ${device} shell getprop net.hostname)
write_command"${@}"
echo ${write_command@Q} | sed "s/ /%s/g" | xargs -0 adb "${device}" shell input text
# enter
adb_keycode 66
# sleep for consecutive writes
sleep 0.125
}
adb_reset_apk () {
adb_download
adb_set_environment
application="${1}"
activity="${2}"
adb "${device}" shell \
pm clear "${application}"
adb "${device}" shell \
am start \
--user 0 \
-n "${application}"/"${application}"."${activity}"
}
# address //////////////////////////////////////////////////////////////////////
address () {
frobulator.require curl
url="ipinfo.io/ip"
echo
curl \
--silent \
--location "${url}"
echo
unset url
}
# audio ////////////////////////////////////////////////////////////////////////
audio () {
frobulator.require ffmpeg
directory="${PWD}"
extension="${1}"
convert_command () {
ffmpeg \
-y \
-i "${file}" \
-f mp3 \
-map_metadata 0 \
-id3v2_version 3 "${file%.*}.mp3"
}
if [ "${extension}" = "flac" ]
then
convert_command () {
ffmpeg \
-y \
-i "${file}" \
-ac b:a 320k \
-map_metadata 0 \
-id3v2_version 3 "${file%.*}.mp3"
}
fi
for file in *."${extension}"
do
convert_command
rm \
--recursive \
--force "${file}"
done
}
# colors ///////////////////////////////////////////////////////////////////////
colors () {
list=(
tput
printf
)
frobulator.require ${list[@]}
list=()
# print palette
for foreground_color in {0..7}
do
set_foreground=$(tput setaf "${foreground_color}")
for background_color in {0..7}
do
set_background=$(tput setab "${background_color}")
echo -n "${set_background} ${set_foreground}"
printf 'F:%s B:%s ' "${foreground_color}" "${background_color}"
done
# reset
echo $(tput sgr0)
done
echo
}
hex () {
# set IFS
IFS=' '
read r g b <<< "${@}"
printf '%02x%02x%02x\n' ${r} ${g} ${b}
# unset IFS
IFS=''
}
rgb () {
frobulator.require printf
read hex <<< "${1}"
[[ ${hex:0:1} = "#" ]] && hex=${hex:1:6}
r=$((16#${hex:0:2}))
g=$((16#${hex:2:2}))
b=$((16#${hex:4:2}))
printf '%03d %03d %03d\n' ${r} ${g} ${b}
}
# directory ////////////////////////////////////////////////////////////////////
directory () {
frobulator.require mkdir
mkdir -p "${1}"
cd "${1}"
}
# duplicate ////////////////////////////////////////////////////////////////////
duplicate () {
frobulator.require dd
dd \
if="${1}" \
of="${2}" \
bs="${3}" \
status=progress
}
# extract //////////////////////////////////////////////////////////////////////
extract () {
list+=(
7zip
bunzip2
gunzip
tar
unrar
unzip
)
frobulator.require ${list[@]}
# unset array
list=()
file="${1}"
directory="${2}"
extension="${3}"
archive="${file##*/}"
if [ -z "${directory}" ]
then
directory="${PWD}"
fi
if [ -z "${extension}" ]
then
extension="${file##*.}"
fi
if [ ! -d "${directory}" ]
then
mkdir -p "${directory}"
fi
case "${extension}"
in
tar.bz2|tbz2)
tar \
--extract \
--bzip2 \
--file="${file}" \
--directory="${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
tar.gz|tgz)
tar \
--extract \
--gzip \
--file="${file}" \
--directory="${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
tar)
tar \
--extract \
--file="${file}" \
--directory="${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
7z)
7z \
x "${file}" \
-o "${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
rar)
unrar e "${file}" "${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
zip)
unzip \
-o \
-qq "${file}" \
-d "${directory}"
frobulator.inf "Extracting" "[ ${archive} ]"
echo
;;
*)
frobulator.err "Unsupported archive type" "[ '${archive}' ]"
echo
exit 1
;;
esac
}
# icons ////////////////////////////////////////////////////////////////////////
icons () {
frobulator.require gtk-update-icon-cache
icons_directory_list=(
"${HOME}"/.icons
"${PREFIX}"/share/icons
/usr/share/icons
)
for icons_directory in ${icons_directory_list[@]}
do
if [ -d "${icons_directory}" ]
then
cd "${icons_directory}"
fi
if [ -d "${icons_directory}" ]
then
find "${icons_directory}" -mindepth 1 -maxdepth 1 -type d | while read -r theme
do
if [ -f "${theme}"/index.theme ]
then
theme_name="${theme##*/}"
gtk-update-icon-cache -f -q "${theme}"
frobulator.scs "Cache update success" "[ ${theme_name} ]"
echo
else
frobulator.wrn "Skipping" "[ ${theme_name} ]"
echo
fi
done
fi
done
}
# inkscape /////////////////////////////////////////////////////////////////////
inkscape-export () {
frobulator.require inkscape
export_types_list=(
pdf
png
)
for file in *.svg
do
for export_type in ${export_types_list[@]}
do
inkscape "${file}" \
--batch-process \
--export-area-page \
--export-type="${export_type}" \
--export-filename="${file%.*}"."${export_type}"
done
done
}
# preview //////////////////////////////////////////////////////////////////////
preview () {
count="${1}"
file="${2}"
if [[ $(command -v cat) ]] && \
[[ $(command -v head) ]]
then
frobulator.inf "Previewing file" "[ ${count} lines // ${file} ]"
echo
frobulator.separate
cat "${file}" | head -n "${count}"
echo
frobulator.separate
fi
}
# record ///////////////////////////////////////////////////////////////////////
record () {
list=(
recordmydesktop
wmctrl
)
(frobulator.require ${list[@]})
list=()
if [ -f "${HOME}"/Videos ]
then
path="${HOME}"/Videos
else
path="${HOME}"
fi
stamp="$(date +"%m-%d-%Y-%H-%M-%S")"
window_id="$(xwininfo | grep "Window id" | cut -d ' ' -f 4)"
wmctrl \
-i \
-R "${window_id}"
frobulator.wait 1
recordmydesktop \
--no-cursor \
--no-wm-check \
--no-sound \
--overwrite \
--windowid "${window_id}" \
--output="${path}"/clip-"${stamp}".ogv
}
# size /////////////////////////////////////////////////////////////////////////
size () {
columns=$(tput cols)
lines=$(tput lines)
echo "Terminal size:"
echo "W: ${columns} x H: ${lines}"
}
# snippet //////////////////////////////////////////////////////////////////////
snippet () {
url="http://sprunge.us"
if (($#))
then
curl -F 'sprunge=<-' "${url}" < "${1}"
else
curl -F 'sprunge=<-' "${url}"
fi
}
# terminate ////////////////////////////////////////////////////////////////////
terminate () {
process_name="${1}"
if [[ $(command -v pgrep) ]] && \
[[ $(command -v pkill) ]]
then
while [[ $(pgrep -f "${process_name}") ]]
do
pkill -f "${process_name}"
done
fi
}
# weather //////////////////////////////////////////////////////////////////////
weather () {
url="wttr.in"
echo
curl -s "${url}" | head -n -2
echo
unset url
}
# youtube //////////////////////////////////////////////////////////////////////
youtube () {
if [[ -z $(command -v yt-dlp) ]]
then
(frobulator.download get.trmnl.me "${HOME}"/.local/bin yt-dlp)
fi
frobulator.silence "${HOME}/.local/bin/yt-dlp --update"
url="${1}"
path="${2}"
if [ -z "${path}" ]
then
path="${PWD}"
fi
if [ -d "${path}" ]
then
cd "${path}"
else
exit 1
fi
format="bestaudio"
audio_format="mp3"
audio_quality="160k"
"${HOME}"/.local/bin/yt-dlp \
--ignore-errors \
--yes-playlist \
--extract-audio \
--format "${format}" \
--audio-format "${audio_format}" \
--audio-quality "${audio_quality}" \
--output '%(uploader)s/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' \
"${url}"
}
# find /////////////////////////////////////////////////////////////////////////
findf () {
type="f"
if [[ $(command -v find) ]]
then
find \
"${PWD}" \
-type "${type}" \
-iname "*${1}*"
fi
}
findd () {
type="d"
if [[ $(command -v find) ]]
then
find \
"${PWD}" \
-type "${type}" \
-iname "*${1}*"
fi
}