Skip to content

Commit

Permalink
Separate format string processor from mapping data
Browse files Browse the repository at this point in the history
Strip out the format string mapping data from the format string
translator function, so we can re-use this worker function with other
format string mapping rules.

Note `eval` is used in substitute_format_strings() to evaluate variables
at runtime. Keep this in mind when writing format string mapping rules.
  • Loading branch information
ropery committed Aug 30, 2014
1 parent 924e807 commit 67b3997
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
69 changes: 40 additions & 29 deletions src/ffcast.bash
Expand Up @@ -39,6 +39,19 @@ declare -A heads=() windows=() heads_all=()
declare -- rootw=0 rooth=0 _x=0 _y=0 x_=0 y_=0 w=0 h=0
declare -- borders=0 frame=0 frame_extents_support=1 intersection=0

declare -A fmtmap=(
['D']='$DISPLAY'
['h']='$h'
['w']='$w'
['x']='$_x'
['y']='$_y'
['X']='$x_'
['Y']='$y_'
['c']='$_x,$_y'
['C']='$x_,$y_'
['g']='${w}x$h+$_x+$_y'
['s']='${w}x$h')

#---
# Functions

Expand Down Expand Up @@ -108,32 +121,33 @@ for ((i=3; i<${#logl[@]}; ++i)); do
}"
done

format_to_string() {
local fmt=$1 str c
printf '%s' "$fmt" |
while IFS= read -r -n 1 -d '' c; do
if [[ $c == '%' ]]; then
IFS= read -r -n 1 -d '' c || :
case $c in
'%') str+='%';;
'D') str+=$DISPLAY;;
'h') str+=$h;;
'w') str+=$w;;
'x') str+=$_x;;
'y') str+=$_y;;
'X') str+=$x_;;
'Y') str+=$y_;;
'c') str+=$_x,$_y;;
'C') str+=$x_,$y_;;
'g') str+=${w}x$h+$_x+$_y;;
's') str+=${w}x$h;;
*) str+="%$c";;
esac
else
str+=$c
fi
# $1: array variable of format string mappings
# $2: array variable to assign substitution results to
# ${@:3} are strings to be substituted
substitute_format_strings() {
local -n ref_fmtmap=$1
local -n ref_strarr=$2
local fmt str c
ref_strarr=()
for fmt in "${@:3}"; do
str=
printf '%s' "$fmt" |
while IFS= read -r -n 1 -d '' c; do
if [[ $c == '%' ]]; then
IFS= read -r -n 1 -d '' c || :
if [[ -v ref_fmtmap[$c] ]]; then
eval "str+=${ref_fmtmap[$c]}"
elif [[ $c == '%' ]]; then
str+='%'
else
str+="%$c"
fi
else
str+=$c
fi
done
ref_strarr+=("$str")
done
printf '%s' "$str";
}

printf '%s %s\n' max '>' min '<' | while IFS=' ' read -r mom cmp; do
Expand Down Expand Up @@ -321,10 +335,7 @@ run_external_command() {
shift || return 0
local -a args=()
# always substitute format strings for external commands
while (( $# )); do
args+=("$(format_to_string "$1")")
shift
done
substitute_format_strings fmtmap args "$@"
# make sure it's an external command -- a disk file
if ! extcmd=$(type -P "$cmd"); then
error "external command '%s' not found" "$cmd"
Expand Down
10 changes: 2 additions & 8 deletions src/subcmd.bash
Expand Up @@ -100,10 +100,7 @@ sub_cmdfuncs['png']=subcmd_png
subcmd_png() {
: 'usage: png [filename]'
local -a args=()
while (( $# )); do
args+=("$(format_to_string "$1")")
shift
done
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 -- \
Expand All @@ -126,10 +123,7 @@ subcmd_rec() {
esac
done
shift $(( OPTIND - 1 ))
while (( $# )); do
args+=("$(format_to_string "$1")")
shift
done
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 -- \
Expand Down

0 comments on commit 67b3997

Please sign in to comment.