night/regex/animate

93 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Repeatedly invoke script for animation
#
# Copyright (C) 2018 Mike Gerwitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
set -euo pipefail
usage()
{
cat <<EOF
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
modern systems.
Example:
$0 fall.sed fall/frame-lalaloop
$0 -c base10-mul.sed <( echo 010 005 ) 0.1
EOF
exit 64 # EX_USAGE
}
render()
{
local -r speed=${1?Missing speed}
# use tput rather than clear to avoid flicker
tput cup 0 0
cat last-frame
sleep "$speed"
}
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}
local -r frame=${2?Missing initial frame}
local -r speed=${3:-0.25}
cp "$frame" last-frame
clear
render "$speed"
while sed -f "$script" -i last-frame; do
$refresh
render "$speed";
done
}
main "$@"