-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
snag
executable file
·1168 lines (1016 loc) · 30.1 KB
/
snag
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Utility for swaywm which unifies the task of capturing images and videos of
# your screen.
#
# Copyright 2020-2022 Maddison Hellstrom <https://github.com/b0o>
# MIT License
# TODO: configuration file
# TODO: screenshot history
# TODO: edit screenshot w/ swappy
set -Eeuo pipefail
shopt -s inherit_errexit
declare prog="snag"
declare version="v0.1.2"
declare authors=("Maddison Hellstrom <github.com/b0o>")
declare repository="github.com/b0o/snag"
declare license="MIT"
declare license_url="https://mit-license.org/"
declare -a require=(
"sway-util"
)
# setup {{{
declare -g basedir libdir runtimedir pidfile
basedir="$(realpath -e "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")")"
libdir="${LIBDIR:-$(realpath -e "${basedir}/lib/")}"
runtimedir="${XDG_RUNTIME_DIR:-/run}"
pidfile="$runtimedir/snag.pid"
# }}}
# globals {{{
declare -i help=0
declare -i copy=0
declare -i delay=0
declare -i cursor=0
declare upload=""
declare -i notify=0
declare -i rofi=0
declare mode_capture="image"
declare out=""
declare out_desc=""
declare out_dir="$PWD"
declare -i out_name_manual=0
declare out_name=""
declare out_namef=""
declare format=""
declare selection=""
declare region=""
declare -a rest=()
declare -a cmd=()
declare -a upload_cmd=()
declare -i passthru=0
declare -i grim_quality=80
declare -i grim_level=6
declare -a args=()
declare -a passthru_args=()
declare out_url=""
# }}}
function usage() {
mapfile -t usage <<EOF
$prog $version ($repository)
Usage: $prog [opts] [-- [pass-thru opts ..]]
Options:
General:
-h --help Print this help message.
-v --version Print program version.
-x --copy-path After snagging a screenshot, copy the output file path
to the clipboard using wl-copy.
-N --notify Send a notification with the snag result on exit.
-U <service> --upload Upload the output file.
<service> may be one of: drop, imgur (image only)
-R --rofi Use rofi as an interactive snag UI.
-D <ms> --delay Delay capture for <ms> milliseconds.
-C --cursor Include cursor in captures (image only).
Capture Mode:
-I [format] --image Capture a still image using grim.
This is the default mode.
[format] may be one of: png (default), jpeg, ppm.
-V [format] --video Capture a video using wf-recorder.
[format] may be one of: mp4 (default), gif, webm.
Capture Quality:
--grim-quality Set the grim JPEG filetype quality 0-100. Defaults to 80.
--grim-level Set the grim PNG filetype compression level 0-9. Defaults to 6.
Capture Region:
If none of the following options are specified, the focused output will be
captured by default.
-r [region] --region Capture a rectangular region.
[region] is of the form "<x>,<y> <width>x<height>"
If [region] is not specified, the user will be able to
select it graphically with the mouse via the slurp
utility.
-c [id] --container Capture a container.
[id] is a sway container ID.
If [id] is not specified, the user will be able to
select the target container graphically with the mouse.
If [id] is a the string 'foc' or 'focused', the
focused container will be used.
-w [name] --workspace Capture a workspace.
[name] is a sway workspace name.
If [name] is not specified, the user will be able to
select the target workspace graphically with the mouse.
If [name] is a the string 'foc' or 'focused', the
focused workspace will be used.
-o [output] --output Capture an output.
[output] is a wayland output name.
If [output] is not specified, the user will be able to
select the target output graphically with the mouse.
If [output] is a the string 'foc' or 'focused', the
focused output will be used.
-a --all Capture all outputs.
Output File:
-d <dir> --dir Save the screenshot in <dir>.
Default: current working directory.
-n <name> --name Use <name> as the literal output file name.
The default file name will be generated by the -f
format of "%Y-%m-%d_%H-%M-%S_{desc}.{ext}"
-f <format> --namef Use <format> to construct the output filename.
Sequences recognised by date(1) or the following will
be interpolated:
{ext} the file extension as set in the -I / -V opts
(without a leading .).
{desc} a description of what was selected:
- if a region was selected: "region"
- if a container was selected:
- wayland apps: container's app_id
- xwayland apps: container's class
- if an output was selected: its name
- if all outputs were selected: "all"
{region} the captured region's coordinates, of the
form "<x>,<y>_<width>x<height>".
To pass on arguments to the capture command (grim or wf-recorder), a -- can be
passed after the final snag argument and any remaining arguments will be passed
through to the capture command.
(c) 2020-$(date +%Y) ${authors[*]}
License: $license ($license_url)
EOF
printf '%s\n' "${usage[@]}"
}
function kill_recursive() {
local -i pid=-1
while read -r pid; do
ps --ppid="$pid" -o pid= | kill_recursive "$@" || true
kill "${1:--INT}" "$pid"
done
}
function init() {
[[ -d "$runtimedir" ]] || mkdir -p "$runtimedir"
local -i pid=-1
if [[ -e "$pidfile" ]]; then
if [[ -s "$pidfile" ]]; then
pid="$(head -1 "$pidfile")"
if [[ $pid -gt 1 ]] && ps "$pid" &>/dev/null; then
# TODO: sanity check to ensure that $pid is actually a snag instance
kill_recursive <<<"$pid"
exit 0
fi
fi
rm "$pidfile"
fi
echo "$$" >"$pidfile"
}
function cleanup() {
local -i code=$?
[[ $help -eq 1 ]] && return 0
[[ -e "$pidfile" ]] && rm "$pidfile"
[[ -e "$out" ]] || {
echo "$prog: error: capture failed: output file not created" >&2
return 1
}
upload
echo "${out_url:-$out}"
local copy_msg=""
[[ $copy -eq 0 ]] || {
copy_msg="Copied "
wl-copy "${out_url:-$out}"
}
[[ $notify -eq 0 ]] || notify-send -t 5000 -i "$out" "snag" "$copy_msg${out_url:-$out}"
exit "$code"
}
function handle() {
local sig="${1:-}"
echo "caught $sig" >&2
exit
}
function _rofi() {
local -a rofi_opts=()
if [[ "${SNAG_WRAP_ROFI:-1}" -eq 1 ]]; then
if [[ -v ROFI_DMENU_PROMPT && -n "$ROFI_DMENU_PROMPT" ]]; then
rofi_opts+=(-p "$ROFI_DMENU_PROMPT")
fi
if [[ ! $* =~ -m(onitor)?[[:space:]] ]]; then
mapfile -t xw_outputs <<<"$(xrandr | awk '/^.* connected/{ print $1 }')"
wl_output_idx="$(swaymsg -t get_outputs | jq 'to_entries | map(select(.value.focused == true))[0].key')"
rofi_opts+=(-m "${xw_outputs[$wl_output_idx]}")
fi
fi
rofi "${rofi_opts[@]}" "$@"
}
function is_valid_region() {
[[ $# -eq 1 && "$1" =~ ^[0-9]{1,},[0-9]{1,}[[:space:]][0-9]{1,}x[0-9]{1,}$ ]]
}
function fmt_name() {
local fmt="$1"
local ext="$2"
local desc="$3"
local region="$4"
mapfile -t replace <<"EOF"
{
gsub(/\{ext\}/, ext, $0)
gsub(/\{desc\}/, desc, $0)
gsub(/\{region\}/, region, $0)
print $0
}
EOF
date "+$(
awk \
-v "ext=$ext" \
-v "desc=$desc" \
-v "region=$region" \
"$(printf '%s\n' "${replace[@]}")" \
<<<"$fmt"
)"
}
function slurp_multi() {
local -a geoms=()
mapfile -t geoms <<<"$(sed -e '/^$/d')"
if [[ ${#geoms[@]} -gt 1 ]]; then
slurp <<<"$(printf '%s\n' "${geoms[@]}")"
else
[[ -n "${geoms[0]:-}" ]] || return 1
echo "${geoms[0]:-}"
fi
}
function select_node() {
local -a node_ids=()
local -a node_rects=()
while read -r l; do
if [[ ${#node_ids[@]} -eq ${#node_rects[@]} ]]; then
node_ids+=("$l")
else
node_rects+=("$l")
fi
done <<<"$(jq -r "" 'if type != "array" then [ . ] else . end | .[] | .id, s::fmt_rect')"
! [[ ${#node_ids[@]} -eq 0 || ${#node_rects[@]} -eq 0 || ${#node_ids[@]} -ne ${#node_rects[@]} ]] || return 1
local sel_rect
sel_rect="$(slurp_multi <<<"$(printf '%s\n' "${node_rects[@]}")")"
local desc="region"
local i
while read -r i; do
[[ ${node_rects[$i]} == "$sel_rect" ]] || continue
desc="$(
tree_by_id "${node_ids[$i]}" -r \
'if .type == "root" then
"all"
elif .type == "output" or .type == "workspace" then
.name
elif .type == "con" or .type == "floating_con" then
.app_id // .window_properties.class
else
null
end // "region"'
)"
done < <(seq 0 $((${#node_ids[@]} - 1)))
printf '%s\n%s\n' "$sel_rect" "$desc"
}
function delay() {
local secs
secs="$(awk '{ print $0 / 1000.0 }' <<<"$delay")"
if ((delay > 0)); then
echo "delay ${delay}ms / ${secs}s" >&2
sleep "$secs"
fi
}
function capture() {
delay
echo "capture: ${cmd[*]}" >&2
"${cmd[@]}" >&2 || {
echo "$prog: error: capture failed" >&2
return 1
}
echo "capture done" >&2
}
function upload() {
if [[ ${#upload_cmd[@]} -gt 0 ]]; then
out_url="$("${upload_cmd[@]}")"
fi
}
function menu_opt_capture() {
local -i rofi_result=0
local -i n=-1
if [[ -z "$mode_capture" || "$mode_capture" == "image" ]]; then
case "$format" in
"" | png) n=0 ;;
ppm) n=1 ;;
jpeg) n=2 ;;
esac
elif [[ "$mode_capture" == "video" ]]; then
case "$format" in
"" | mp4) n=3 ;;
webm) n=4 ;;
webp) n=5 ;;
esac
fi
rofi_result=$(
{
cat <<EOF
image / png
image / ppm
image / jpeg
video / mp4
video / webm
video / webp
EOF
} |
awk -v "n=$n" \
'{
o = "→ ";
if (FNR - 1 == n) {
o = o "<span weight=\"heavy\">[" $0 "]</span>"
} else {
o = o $0
}
print o
}' |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row "$n" \
-p "Capture Mode"
) || return 1
case $rofi_result in
0) mode_capture="image" format="png" ;;
1) mode_capture="image" format="ppm" ;;
2) mode_capture="image" format="jpeg" ;;
3) mode_capture="video" format="mp4" ;;
4) mode_capture="video" format="webm" ;;
5) mode_capture="video" format="webp" ;;
esac
}
function menu_opt_notify() {
local -i rofi_result=0
rofi_result=$(
{
cat <<EOF
0
1
EOF
} |
awk -v "n=$notify" \
'{
o = "→ ";
if (FNR - 1 == n) {
o = o "<span weight=\"heavy\">[" $0 "]</span>"
} else {
o = o $0
}
print o
}' |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row "$notify" \
-p "Notify"
) || return 1
case $rofi_result in
0) notify=0 ;;
1) notify=1 ;;
esac
}
function menu_opt_delay() {
local -i rofi_result
rofi_result=$(
_rofi -dmenu -markup-rows -format f -i \
-async-pre-read 0 \
-filter "$delay" \
-mesg "<span size='small' alpha='70%'>Delay capture for this many milliseconds</span>" \
-p "Delay"
) || return 1
if ((rofi_result < 0)); then
return 1
fi
delay="${rofi_result:-$delay}"
}
function menu_opt_cursor() {
local -i rofi_result=0
rofi_result=$(
{
cat <<EOF
0
1
EOF
} |
awk -v "n=$cursor" \
'{
o = "→ ";
if (FNR - 1 == n) {
o = o "<span weight=\"heavy\">[" $0 "]</span>"
} else {
o = o $0
}
print o
}' |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row "$cursor" \
-p "Cursor"
) || return 1
case $rofi_result in
0) cursor=0 ;;
1) cursor=1 ;;
esac
}
function menu_opt_upload() {
local -i rofi_result=0
local -i n=-1
case "$upload" in
imgur) n=0 ;;
drop) n=1 ;;
"") n=2 ;;
esac
rofi_result=$(
{
cat <<EOF
imgur
drop
-
EOF
} |
awk -v "n=$n" \
'{
o = "→ ";
if (FNR - 1 == n) {
o = o "<span weight=\"heavy\">[" $0 "]</span>"
} else {
o = o $0
}
print o
}' |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row "$n" \
-p "Upload"
) || return 1
case $rofi_result in
0) upload="imgur" ;;
1) upload="drop" ;;
2) upload="" ;;
esac
}
function menu_opt_copy() {
local -i rofi_result=0
rofi_result=$(
{
cat <<EOF
0
1
EOF
} |
awk -v "n=$copy" \
'{
o = "→ ";
if (FNR - 1 == n) {
o = o "<span weight=\"heavy\">[" $0 "]</span>"
} else {
o = o $0
}
print o
}' |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row "$copy" \
-p "Copy"
) || return 1
case $rofi_result in
0) copy=0 ;;
1) copy=1 ;;
esac
}
function menu_opt_out_dir() {
local rofi_result
while true; do
rofi_result="$(
cat \
<(echo -e "→ [Accept]\n→ [Custom]\n..\0icon\x1ffolder") \
<(find "$out_dir" -mindepth 1 -maxdepth 1 -type d | awk '{ print $0 "\0icon\x1ffolder" }') |
_rofi -dmenu -markup-rows -i \
-async-pre-read 0 \
-mesg "<b>${out_dir:-}</b>" \
-p "Output Dir"
)" || return 1
case "$rofi_result" in
"→ [Accept]")
return
;;
"→ [Custom]")
rofi_result="$(
_rofi -dmenu -markup-rows \
-format f \
-async-pre-read 0 \
-mesg "${out_dir:-}" \
-p "Output Dir [Custom]"
)" || continue
out_dir="$rofi_result"
;;
"..")
out_dir="$(realpath "$out_dir/..")"
;;
*)
out_dir="$rofi_result"
;;
esac
done
}
function menu_opt_out_name() {
local -i rofi_exit=0
local rofi_result
while true; do
rofi_result="$(
_rofi -dmenu -markup-rows \
-format f \
-async-pre-read 0 \
-filter "${out_name:-}" \
-kb-custom-1 "Ctrl+c" \
-mesg "<span size='small' alpha='70%'><C-c> or empty string: use format <i>$out_namef</i></span>" \
-p "Output Filename"
)" || rofi_exit=$?
case $rofi_exit in
0) ;;
10) rofi_result="" ;;
*) return $? ;;
esac
if [[ "$rofi_result" =~ / ]]; then
_rofi -e "Error: invalid filename"
continue
fi
break
done
if [[ -z "$rofi_result" ]]; then
out_name_manual=0
else
out_name_manual=1
fi
out_name="$rofi_result"
}
function menu_opt_region() {
local -i rofi_result=0
if [[ $# -gt 0 ]]; then
rofi_result=$1
else
rofi_result=$(
{
cat <<EOF
→ <Select Region>
→ <Select Container>
→ <Select Workspace>
→ <Select Output>
→ <Select All>
→ [Custom]
EOF
} |
_rofi -dmenu -markup-rows -format i -i \
-async-pre-read 0 \
-selected-row 0 \
-p "Region"
) || return 1
fi
case $rofi_result in
0)
selection="$(printf '%s\n%s\n' "$(slurp)" "region")"
;;
1)
selection="$(containers_get_visible | select_node)"
;;
2)
selection="$(workspaces_get_visible | select_node)"
;;
3)
selection="$(outputs_get_active | select_node)"
;;
4)
selection="$(tree_by_id 1 | select_node)"
;;
5)
while true; do
local rofi_selection
rofi_selection="$(
_rofi -dmenu \
-format f \
-async-pre-read 0 \
-mesg "<x>,<y> <w>x<h>" \
-filter "${region:-}" \
-p "Region [Custom]"
)" || break
[[ -n "$rofi_selection" ]] || break
is_valid_region "$rofi_selection" || {
_rofi -e "Invalid region: $rofi_selection"
continue
}
selection="$(printf '%s\n%s\n' "$rofi_selection" "region")"
break
done
;;
esac
}
function menu_main() {
local -i rofi_exit=0
local -i rofi_result=0
local -i rofi_prev=1
local -i menu_finished=0
while [[ $menu_finished -eq 0 ]]; do
# rofi_result=1
rofi_exit=0
local -a rofi_input=(
"<span weight='heavy'>Options</span>" # rofi_result=0
"Capture Mode: <span style='italic'>${mode_capture:--}${mode_capture:+${format:+ / $format}}</span>" # rofi_result=1
"Region: <span style='italic'>${region:--}</span>" # rofi_result=2
"Notify: <span style='italic'>${notify:-0}</span>" # rofi_result=3
"Delay: <span style='italic'>${delay:-0}</span>" # rofi_result=4
"Cursor: <span style='italic'>${cursor:-0}</span>" # rofi_result=5
"Upload: <span style='italic'>${upload:--}</span>" # rofi_result=6
"Copy: <span style='italic'>${copy:-0}</span>" # rofi_result=7
"Output Dir: <span style='italic'>${out_dir:--}</span>" # rofi_result=8
"Output Filename: <span style='italic'>${out_name:--}</span>" # rofi_result=9
"" # rofi_result=10
"→ Capture Region <span size='small' alpha='70%'><C-r></span>" # rofi_result=11
"→ Capture Container <span size='small' alpha='70%'><C-c></span>" # rofi_result=12
"→ Capture Workspace <span size='small' alpha='70%'><C-w></span>" # rofi_result=13
"→ Capture Output <span size='small' alpha='70%'><C-o></span>" # rofi_result=14
"→ Capture All <span size='small' alpha='70%'><C-a></span>" # rofi_result=15
"" # rofi_result=16
"<span weight='heavy'>→ Capture</span> <span size='small' alpha='70%'><C-Return></span>" # rofi_result=17
)
local -a rofi_opts=(
-dmenu
-markup-rows
-format i
-i
-async-pre-read 0
-selected-row "$rofi_prev"
-kb-accept-custom "Ctrl+Alt+Return"
-kb-clear-line "Ctrl+Alt+w"
-kb-move-front "Ctrl+Alt+a"
-kb-custom-1 "Ctrl+r" # Capture Region rofi_exit=10
-kb-custom-2 "Ctrl+c" # Capture Container rofi_exit=11
-kb-custom-3 "Ctrl+w" # Capture Window rofi_exit=12
-kb-custom-4 "Ctrl+o" # Capture Output rofi_exit=13
-kb-custom-5 "Ctrl+a" # Capture All rofi_exit=14
-kb-custom-6 "Ctrl+Return" # Capture rofi_exit=15
-kb-custom-7 "Alt+r" # Select Region rofi_exit=16
-kb-custom-8 "Alt+c" # Select Container rofi_exit=17
-kb-custom-9 "Alt+w" # Select Window rofi_exit=18
-kb-custom-10 "Alt+o" # Select Output rofi_exit=19
-kb-custom-11 "Alt+a" # Select All rofi_exit=20
-kb-custom-12 "Alt+m" # Mode Toggle rofi_exit=21
-kb-custom-13 "Alt+i" # Mode Image rofi_exit=22
-kb-custom-14 "Alt+v" # Move Video rofi_exit=23
-kb-custom-15 "Alt+u" # Upload Toggle rofi_exit=24
-kb-custom-16 "Alt+I" # Upload Imgur rofi_exit=25
-kb-custom-17 "Alt+D" # Upload Drop rofi_exit=26
-kb-custom-18 "Alt+n" # Notify Toggle rofi_exit=27
-kb-custom-19 "Alt+x" # Copy Toggle rofi_exit=28
-mesg "<span size='small' alpha='70%'><M-m> mode toggle / <M-i> image mode / <M-v> video mode / <M-u> upload toggle / <M-S-i> imgur upload / <M-S-d> drop upload / <M-n> notify toggle / <M-x> copy toggle</span>"
-mesg "foo"
-p "snag"
)
rofi_result=$(_rofi "${rofi_opts[@]}" < <(printf '%s\n' "${rofi_input[@]}")) || rofi_exit=$?
rofi_prev=$rofi_result
case $rofi_exit in
0) ;;
# rofi -kb-custom-{1-19} map to rofi_exit {10-28}
10) rofi_result=11 menu_finished=1 ;; # Capture Region
11) rofi_result=12 menu_finished=1 ;; # Capture Container
12) rofi_result=13 menu_finished=1 ;; # Capture Window
13) rofi_result=14 menu_finished=1 ;; # Capture Output
14) rofi_result=15 menu_finished=1 ;; # Capture All
15) rofi_result=17 menu_finished=1 ;; # Capture
16 | 17 | 18 | 19 | 20)
rofi_prev=2
;;&
16) rofi_result=11 ;; # Select Region
17) rofi_result=12 ;; # Select Container
18) rofi_result=13 ;; # Select Window
19) rofi_result=14 ;; # Select Output
20) rofi_result=15 ;; # Select All
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28)
# rofi_exit codes 21-28 do not enter sub-menus, so reset rofi_result to 0
rofi_result=0
;;&
21) # Mode Toggle
rofi_prev=1
case "$mode_capture" in
image) mode_capture="video" format="mp4" ;;
video) mode_capture="image" format="png" ;;
esac
;;
22) rofi_prev=1 mode_capture="image" format="png" ;; # Mode Image
23) rofi_prev=1 mode_capture="video" format="mp4" ;; # Mode Video
24) # Upload Toggle
rofi_prev=5
case "$upload" in
imgur) upload="drop" ;;
drop) upload="" ;;
*) upload="imgur" ;;
esac
;;
25) rofi_prev=5 upload="imgur" ;; # Upload Imgur
26) rofi_prev=5 upload="drop" ;; # Upload Drop
27) rofi_prev=3 notify=$((notify * -1 + 1)) ;; # Notify Toggle
28) rofi_prev=6 copy=$((copy * -1 + 1)) ;; # Copy Toggle
*) return "$rofi_exit" ;;
esac
case $rofi_result in
0) ;;
1) menu_opt_capture || true ;;
2) menu_opt_region || true ;;
3) menu_opt_notify || true ;;
4) menu_opt_delay || true ;;
5) menu_opt_cursor || true ;;
6) menu_opt_upload || true ;;
7) menu_opt_copy || true ;;
8) menu_opt_out_dir || true ;;
9)
menu_opt_out_name || true
# if [[ -n "$out_name" ]]; then
# out_namef=""
# fi
;;
11) menu_opt_region 0 || true ;;
12) menu_opt_region 1 || true ;;
13) menu_opt_region 2 || true ;;
14) menu_opt_region 3 || true ;;
15) menu_opt_region 4 || true ;;
17) menu_finished=1 ;;
*)
continue
;;
esac
parse_opts
done
}
function parse_opts() {
[[ -n "$format" ]] || case "$mode_capture" in
image) format="png" ;;
video) format="mp4" ;;
esac
[[ -n "$selection" ]] || selection="$(outputs_get_focused | select_node)"
local -a selection_result=()
mapfile -t selection_result <<<"$selection"
[[ ${#selection_result[@]} -eq 2 ]] || {
echo "$prog: error: selection failed" >&2
return 1
}
region="${selection_result[0]}"
out_desc="${selection_result[1]}"
[[ $out_name_manual -eq 1 ]] || {
[[ -n "$out_namef" ]] || out_namef="%Y-%m-%d_%H-%M-%S_{desc}.{ext}"
out_name="$(fmt_name "$out_namef" "$format" "$out_desc" "$region")"
}
# TODO: use awk or sed instead of perl
out_name="$(perl -pe 's/\0|\/|[^[:ascii:]]//g' <<<"$out_name")"
out="$out_dir/$out_name"
[[ ! -e "$out" ]] || {
echo "$prog: error: file exists: $out" >&2
return 1
}
cmd=()
case "$mode_capture" in
image)
local grim_opts=()
if [[ "$format" == "jpeg" ]]; then
grim_opts+=(-q "$grim_quality")
elif [[ "$format" == "png" ]]; then
grim_opts+=(-l "$grim_level")
fi
if [[ $cursor -eq 1 ]]; then
grim_opts+=(-c)
fi
cmd+=(grim -g "$region" -t "$format" "${grim_opts[@]}" "${passthru_args[@]}" "$out")
;;
video)
cmd+=(wf-recorder -g "$region" "${passthru_args[@]}" -f "$out")
;;
esac
# local -a upload_cmd=()
[[ -z "$upload" ]] || case "$upload" in
drop)
upload_cmd=("$(type -P drop)" "$out") || {
echo "$prog: error: upload service helper not found: drop" >&2
return 1
}
;;
imgur)
# TODO
# [[ "$mode_capture" == "image" ]] || {
# echo "$prog: error: upload service imgur only supports images" >&2
# return 1
# }
upload_cmd=("$(type -P imgur.sh)" "$out") || {
echo "$prog: error: upload service helper not found: imgur.sh" >&2
return 1
}
;;
*)
echo "$prog: error: unsupported upload service: $upload" >&2
return 1
;;
esac
}
function main() {
local -a args=("$@")
local -i i=0
local opt arg
while ((i < ${#args[@]})); do
arg=""
opt="${args[$i]}"
# argument not beginning with - ends options
if [[ (! "$opt" =~ ^-) || "$opt" == "--" ]]; then
rest=("${args[@]:$i:${#args[@]}}")
break
fi
# expand combined opts (e.g. -wNU -> -w -N -U)
if [[ "$opt" =~ ^-[[:alnum:]]{2,} ]]; then
local -a expanded=()
local -i j
for ((j = 1; j < ${#opt}; j++)); do
expanded+=("-${opt:$j:1}")
done
args=("${args[@]:0:$i}" "${expanded[@]}" "${args[@]:$((i + 1)):${#args[@]}}")
continue
fi
i=$((i + 1))
case "$opt" in
# populate the `arg` variable for options that require an argument
-D | --delay) ;&
-U | --upload) ;&
-d | --dir) ;&
-n | --name) ;&
-f | --namef)
if ((i >= ${#args[@]})) || [[ "${args[$i]}" =~ ^- ]]; then
echo "$prog: error: $opt: expected argument" >&2
return 1
fi
arg="${args[$i]}"
i=$((i + 1))
;;&
# populate the `arg` variable for options that accept optional an argument
# if there is a following positional parameter that doesn't start with a -
-I | --image) ;&
-V | --video) ;&
-r | --region) ;&
-c | --container) ;&
-w | --workspace) ;&
-o | --output) ;&
--grim-quality) ;&
--grim-level)
if ((i < ${#args[@]})) && [[ ! "${args[$i]}" =~ ^- ]]; then
arg="${args[$i]}"
i=$((i + 1))
fi
;;&
# General {{{
-h | --help)
help=1
usage
exit 0
;;
-v | --version)
echo "$version"
exit 0
;;
-x | --copy-path)
copy=1
;;
-N | --notify)
notify=1
;;
-R | --rofi)