From 4c4cf97364b1342b4230f0f0d6285651267e49a3 Mon Sep 17 00:00:00 2001 From: lolilolicon Date: Sat, 30 Aug 2014 13:33:43 +0800 Subject: [PATCH] 'each' now supports format strings %i, %n, %t Try: ffcast -ws each echo %%%%%n-%t_%i --- doc/ffcast.1.pod | 30 +++++++++++++++++++++++++----- src/subcmd.bash | 23 ++++++++++++++++------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/doc/ffcast.1.pod b/doc/ffcast.1.pod index 8b75124..7423124 100644 --- a/doc/ffcast.1.pod +++ b/doc/ffcast.1.pod @@ -99,7 +99,8 @@ Note IargsE> to an internal Isub-commandE> 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 and B: =over 4 @@ -147,12 +148,30 @@ Equivalent to B<%wx%h+%x+%y> Equivalent to B<%wx%h> -=item B<%%> +=back + +The B 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 Ith 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 IgeospecE> specifies a geometry in one of the following forms: @@ -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 -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 diff --git a/src/subcmd.bash b/src/subcmd.bash index e80caba..6918736 100644 --- a/src/subcmd.bash +++ b/src/subcmd.bash @@ -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 } @@ -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 @@ -113,7 +122,7 @@ sub_commands['rec']='record a screencast' sub_cmdfuncs['rec']=subcmd_rec subcmd_rec() { : 'usage: rec [-m ] [filename.ext]' - local -a args=() + local -a args local -a v=(fatal error info verbose debug) # ffmpeg loglevels local m=1 opt OPTIND=1