Skip to content

Commit

Permalink
'each' now supports format strings %i, %n, %t
Browse files Browse the repository at this point in the history
Try: ffcast -ws each echo %%%%%n-%t_%i
  • Loading branch information
ropery committed Aug 30, 2014
1 parent 67b3997 commit 4c4cf97
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
30 changes: 25 additions & 5 deletions doc/ffcast.1.pod
Expand Up @@ -99,7 +99,8 @@ Note I<E<lt>argsE<gt>> to an internal I<E<lt>sub-commandE<gt>> are not
automatically subject to format string substitution, although sub-commands may
choose to do format string substitution on their own.

The following format strings are supported:
The following format strings are supported for all external commands, as well
as the sub-commands B<png> and B<rec>:

=over 4

Expand Down Expand Up @@ -147,12 +148,30 @@ Equivalent to B<%wx%h+%x+%y>

Equivalent to B<%wx%h>

=item B<%%>
=back

The B<each> sub-command supports the following format strings:

=over 4

=item B<%i>

A literal %
The identifier of the selection

=item B<%n>

This being the I<n>th selection

=item B<%t>

The type of the selection

=back

The format string B<%%> is always substituted for a literal C<%>, whenever
format string substitution is performed. Therefore, multiple levels of
escaping may be required when sub-commands are chained.

=head1 GEOMETRY SPECIFICATION

A I<E<lt>geospecE<gt>> specifies a geometry in one of the following forms:
Expand Down Expand Up @@ -228,9 +247,10 @@ head 0, using either of:
ffcast -ww ffcast -i -x 0 -g %g png

Take three individual screenshots of heads 0, 1, 2, one by one. The B<each>
command runs a command for each user selection consecutively:
command runs a command for each user selection consecutively; it supports
several format strings:

ffcast -x 0,1,2 each png head_%g.png
ffcast -x 0,1,2 each png %t_%i-%s.png

=head1 SEE ALSO

Expand Down
23 changes: 16 additions & 7 deletions src/subcmd.bash
Expand Up @@ -52,18 +52,27 @@ sub_cmdfuncs['%']=run_external_command
sub_commands['dump']='dump region-related variables in bash code'
sub_cmdfuncs['dump']=subcmd_dump
subcmd_dump() {
(( ! ${#head_ids[@]} )) || declare -p heads
declare -p {root,}{w,h} _{x,y} {x,y}_ offsets_list
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 offsets;
for offsets in "${offsets_list[@]}"; do
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 "$@"
run_subcmd_or_command "${args[@]}"
unset -n ref_rect
done
}

Expand Down Expand Up @@ -99,7 +108,7 @@ 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=()
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
Expand All @@ -113,7 +122,7 @@ sub_commands['rec']='record a screencast'
sub_cmdfuncs['rec']=subcmd_rec
subcmd_rec() {
: 'usage: rec [-m <n>] [filename.ext]'
local -a args=()
local -a args
local -a v=(fatal error info verbose debug) # ffmpeg loglevels
local m=1 opt
OPTIND=1
Expand Down

0 comments on commit 4c4cf97

Please sign in to comment.