/
subcmd.bash
144 lines (136 loc) · 4.67 KB
/
subcmd.bash
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
#!/bin/bash
#
# To register a sub-command 'foo', you MUST add this,
#
# sub_commands['foo']='description'
#
# By default, running sub-command 'foo' will execute the function 'foo'.
# But you MAY explicitly associate sub-command 'foo' with function 'bar',
#
# sub_cmdfuncs['foo']=bar
#
# This is useful in the cases when your sub-command isn't a valid function
# name, or when it conflicts with an existing function.
#
# As this file is sourced after all the geometry processing has been done, you
# have access to, among others, the variables $w $h $_x $_y $x_ $y_ and all the
# functions defined in ffcast.
#
# The positional arguments to a sub-command function are all the arguments
# after the sub-command as specified on the command line by the user.
sub_commands['help']='print help for a sub-command, or list all sub-commands'
sub_cmdfuncs['help']=subcmd_help
subcmd_help() {
: 'usage: help [sub-command]'
local sub_cmd=$1
if ! (( $# )); then
for sub_cmd in "${!sub_commands[@]}"; do
printf "%s\t%s\n" "$sub_cmd" "${sub_commands[$sub_cmd]}"
done | sort -k 1,1
return
fi
if [[ -v sub_commands[$sub_cmd] ]]; then
local sub_cmd_func=${sub_cmdfuncs[$sub_cmd]:-$sub_cmd}
printf '%s: %s\n' "$sub_cmd" "${sub_commands[$sub_cmd]}"
if [[ $(type -t "$sub_cmd_func") == function ]]; then
declare -fp "$sub_cmd_func"
return
else
error "sub-command '%s' function '%s' not found" "$sub_cmd" \
"$sub_cmd_func"
fi
else
error "no such sub-command '%s'; use 'help' to get a list." "$sub_cmd"
fi
exit 1
}
sub_commands['%']='look up external command only; bypass sub-commands'
sub_cmdfuncs['%']=run_external_command
sub_commands['dump']='dump region-related variables in bash code'
sub_cmdfuncs['dump']=subcmd_dump
subcmd_dump() {
declare -p {root,}{w,h} _{x,y} {x,y}_ offsets_list \
rects heads regions windows
}
sub_commands['each']='run a sub-command on each selection consecutively'
sub_cmdfuncs['each']=subcmd_each
subcmd_each() {
: 'usage: each [sub-command]'
local -A fmtmap_each=(['i']='$i' ['n']='$((n + 1))' ['t']='$t')
local -a args
local -i n
local -- i t
for ((n=0; n<${#rects[@]}; ++n)); do
local -n ref_rect=${rects[n]}
t=${rects[n]%%\[*}; t=${t%s}
i=${rects[n]#*\[}; i=${i%\]}
substitute_format_strings fmtmap_each args "$@"
offsets=$ref_rect
set_region_vars_by_offsets 1 || continue
run_subcmd_or_command "${args[@]}"
unset -n ref_rect
done
}
sub_commands['pad']='Add CSS-style padding to region'
sub_cmdfuncs['pad']=subcmd_pad
subcmd_pad() {
: 'usage: pad <padding> [sub-command]'
local -- t r b l
IFS=' \t,' read -r t r b l <<< "$1"
shift || return 0
if [[ -z $t ]]; then
return
elif [[ -z $r ]]; then
local -i t=$t r=$t b=$t l=$t
elif [[ -z $b ]]; then
local -i t=$t r=$r b=$t l=$r
elif [[ -z $l ]]; then
local -i t=$t r=$r b=$b l=$r
else
local -i t=$t r=$r b=$b l=$l
fi
(( _x -= l )) || :
(( _y -= t )) || :
(( x_ -= r )) || :
(( y_ -= b )) || :
verbose 'padding: top=%d right=%d bottom=%d left=%d' "$t" "$r" "$b" "$l"
offsets="$_x $_y $x_ $y_"
set_region_vars_by_offsets || exit
run_subcmd_or_command "$@"
}
sub_commands['png']='take a screenshot and save it as a PNG image'
sub_cmdfuncs['png']=subcmd_png
subcmd_png() {
: 'usage: png [filename]'
local -a args
substitute_format_strings fmtmap args "$@"
: ${args[0]="$(printf '%s-%(%s)T_%dx%d.png' screenshot -1 "$w" "$h")"}
msg 'saving to file: %s' "${args[-1]}" # unreliable
verbose_run command -- \
ffmpeg -loglevel error -f x11grab -draw_mouse 0 -show_region 1 \
-video_size "${w}x$h" -i "$DISPLAY+$_x,$_y" \
-f image2 -codec:v png -frames:v 1 "${args[@]}"
}
sub_commands['rec']='record a screencast'
sub_cmdfuncs['rec']=subcmd_rec
subcmd_rec() {
: 'usage: rec [-m <n>] [filename.ext]'
local -a args
local -a v=(fatal error info verbose debug) # ffmpeg loglevels
local m=1 opt
OPTIND=1
while getopts ':m:' opt; do
case $opt in
m) m=$OPTARG;;
esac
done
shift $(( OPTIND - 1 ))
substitute_format_strings fmtmap args "$@"
: ${args[0]="$(printf '%s-%(%s)T.mkv' screencast -1)"}
msg 'saving to file: %s' "${args[-1]}" # unreliable
verbose_run command -- \
ffmpeg -loglevel "${v[verbosity]}" -f x11grab -show_region 1 \
-framerate 25 -video_size "${w}x$h" -i "$DISPLAY+$_x,$_y" \
-filter:v crop="iw-mod(iw\\,$m):ih-mod(ih\\,$m)" "${args[@]}"
}
# vim:ts=4:sw=4:et:cc=80: