Mike Gerwitz

Free Software Hacker+Activist

aboutsummaryrefslogtreecommitdiffstats
blob: 6586b15f55b4ee2c724b9b2f96e5d71a11843192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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 "$@"