animate: Add -c option for variable-width frames

Not all scripts produce fixed-width frames.  Variable-width frames leave
behind artifacts caused by characters from the previous frame not being
overwritten.

* regex/animate (usage): Document new `-c' flag.
  (main): Handle new `-c' flag.
    [refresh]: New variable.
* regex/base10-mul.sed: Remove extra whitespace used for frame clearing for
    animate script (just use `-c' now).
master
Mike Gerwitz 2018-11-24 23:57:48 -05:00
parent 2dd5b590f9
commit 3f5c397e83
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
2 changed files with 23 additions and 6 deletions

View File

@ -23,13 +23,19 @@ set -euo pipefail
usage()
{
cat <<EOF
Usage: $0 script frame [speed]
Usage: $0 [-c] script frame [speed]
Recursively render FRAME using SCRIPT
The current frame is stored in the file \`last-frame' in the working
directory. It may be used to continue the animation after quitting,
or save the state of the system.
To prevent flickering, each frame is rendered atop of the previous without
clearing the screen (your terminal must support this). This is desirable
for fixed-width frames, but not for scripts with variable-width output,
since that will leave behind artifacts. Provide the \`-c' flag to clear the
screen before rendering each frame.
SPEED should be a value recognized by the \`sleep' command. It defaults
to 0.25 to give some level of appreciation for each frame while still
looking animated. It looks fairly smooth at 0.05 but can go much faster on
@ -37,6 +43,7 @@ modern systems.
Example:
$0 fall.sed fall/frame-lalaloop
$0 -c base10-mul.sed <( echo 010 005 ) 0.1
EOF
exit 64 # EX_USAGE
@ -56,6 +63,15 @@ render()
main()
{
# Do not clear the screen on each frame by default (we'll be using tput
# to avoid flickering; see `render'). Certain scripts do not produce
# fixed-width frames, and so we must clear to avoid artifacts.
declare refresh=true
test "${1:-}" == -c && {
refresh=clear
shift
}
test $# -ge 2 -a $# -le 3 || usage
local -r script=${1?Missing script}
@ -67,7 +83,10 @@ main()
clear
render "$speed"
while sed -f "$script" -i last-frame; do render "$speed"; done
while sed -f "$script" -i last-frame; do
$refresh
render "$speed";
done
}
main "$@"

View File

@ -94,10 +94,8 @@ s/^\(...\) 000/000 001/
# register two is `001'), then we are done! If that's the case, clean up
# our output to display only the final result. The script will
# automatically exit with a non-zero status the next iteration because of
# the guard at the top of this script. The trailing spaces ensure that
# leftover output from previous iterations is erased in the animation
# script.
s/^\(...\) 001.*/\1 /
# the guard at the top of this script.
s/^\(...\) 001.*/\1/
/^... \+$/b