Skip to content

Instantly share code, notes, and snippets.

@aaronNGi
Created January 31, 2021 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronNGi/b9331cb708425f468c22fe43b5878c85 to your computer and use it in GitHub Desktop.
Save aaronNGi/b9331cb708425f468c22fe43b5878c85 to your computer and use it in GitHub Desktop.
Small utilities to draw a stopwatch as seven-segment display to the terminal. Usage: timer.sh | redraw.sh | segments.awk
#!/bin/sh
esc() {
printf '\033%s' "$@" >&2
}
clean_exit() {
_exit_status=$?
trap - EXIT
# Show cursor again.
esc '[?25h'
exit "$_exit_status"
}; trap clean_exit EXIT TERM HUP INT
stty -echo -echonl </dev/tty
# Clear screen, store cursor position and hide cursor.
esc '[H' '[J' '7' '[?25l'
while read -r line; do
# Restore cursor position and clear to end of screen.
esc '8' '[J'
printf '%s\n' "$line"
done
#!/usr/bin/awk -f
#
# Digital segment display.
# Usage: segments [height=<height>] [width=<width>]
function repeat(char, n, str) {
while (n-- > 0)
str = str char
return str
}
BEGIN {
height = 1
width = 2
# Segment matrix.
s["0_1"] = "111"; s["0_2"] = "101"; s["0_3"] = "101"
s["0_4"] = "101"; s["0_5"] = "111"
s["1_1"] = "001"; s["1_2"] = "001"; s["1_3"] = "001"
s["1_4"] = "001"; s["1_5"] = "001"
s["2_1"] = "111"; s["2_2"] = "001"; s["2_3"] = "111"
s["2_4"] = "100"; s["2_5"] = "111"
s["3_1"] = "111"; s["3_2"] = "001"; s["3_3"] = "111"
s["3_4"] = "001"; s["3_5"] = "111"
s["4_1"] = "101"; s["4_2"] = "101"; s["4_3"] = "111"
s["4_4"] = "001"; s["4_5"] = "001"
s["5_1"] = "111"; s["5_2"] = "100"; s["5_3"] = "111"
s["5_4"] = "001"; s["5_5"] = "111"
s["6_1"] = "111"; s["6_2"] = "100"; s["6_3"] = "111"
s["6_4"] = "101"; s["6_5"] = "111"
s["7_1"] = "111"; s["7_2"] = "001"; s["7_3"] = "001"
s["7_4"] = "001"; s["7_5"] = "001"
s["8_1"] = "111"; s["8_2"] = "101"; s["8_3"] = "111"
s["8_4"] = "101"; s["8_5"] = "111"
s["9_1"] = "111"; s["9_2"] = "101"; s["9_3"] = "111"
s["9_4"] = "001"; s["9_5"] = "111"
s["-_1"] = "00"; s["-_2"] = "00"; s["-_3"] = "11"
s["-_4"] = "00"; s["-_5"] = "00"
s[":_1"] = "0"; s[":_2"] = "1"; s[":_3"] = "0"
s[":_4"] = "1"; s[":_5"] = "0"
s["._1"] = "0"; s["._2"] = "0"; s["._3"] = "0"
s["._4"] = "0"; s["._5"] = "1"
vert_segments = 5
} NR==1 {
on = repeat("\xe2\x96\x88", width)
off = repeat(" ", width)
} {
for (line = 1; line <= vert_segments; line++) {
str = ""
i = 1
while (char = substr($0, i, 1)) {
if (i == 1)
str = s[char "_" line]
else
str = str "0" s[char "_" line]
i++
}
gsub(/0/, off, str)
gsub(/1/, on, str)
for (i = 1; i <= height; i++)
printf "%s%s", (line==1 && i==1) ? "" : "\n", str
}
fflush("/dev/stdout")
} END {
print ""
}
#!/bin/sh
#
# A stopwatch like timer which resets to zero when enter is pressed.
interval=1
clean_exit() {
_exit_status=$?
trap - EXIT
kill "$child"
exit "$_exit_status"
}
trap 'clean_exit' EXIT TERM HUP INT
{
trap secs=0 USR1
secs=0
while :; do
printf "%02d:%02d\n" "$((secs/60))" "$((secs%60))"
secs=$((secs+interval))
sleep "$interval" &
wait
done
} & child=$!
while read -r _; do
kill -USR1 "$child"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment