Skip to content

Instantly share code, notes, and snippets.

@leighleighleigh
Created December 18, 2022 05:57
Show Gist options
  • Save leighleighleigh/e2aa0e25a85af4bc57f14d27131bfcee to your computer and use it in GitHub Desktop.
Save leighleighleigh/e2aa0e25a85af4bc57f14d27131bfcee to your computer and use it in GitHub Desktop.
svgbob_live.sh - live preview of ASCII diagrams as SVG using svgbob
#!/usr/bin/env bash
### svgbob live preview ###
# DESCRIPTION
# Runs svgbob_cli whenever a file is changed (specified at launch)
# and open it with eog (eye of gnome)
# AUTHOR
# Leigh Oliver, 18/12/2022
# USAGE
# svgbob_live <filename>
# REQUIREMENTS
# inotify-tools (apt package)
# svgbob_cli (cargo crate)
# check required commands are installed
if ! command -v svgbob_cli &> /dev/null
then
echo "svgbob_cli could not be found"
echo "try: cargo install svgbob_cli"
exit 1
fi
if ! command -v inotifywait &> /dev/null
then
echo "inotifywait could not be found"
echo "try: apt install inotify-tools"
exit 1
fi
# store input filename
inputfile="${1}"
# create a temporary output file for svgbob_live
outputfile="$(mktemp).svg"
# run svgbob once, and open eog
svgbob_cli "$inputfile" > "$outputfile"
eog -gn "$outputfile" &
# store the PID of eog, so we can exit the loop below if it's closed
eogpid="$!"
# while eog is running
while ps -p "$eogpid" > /dev/null
do
# when file changes, re-run svgbob command.
# eog will then automagically refresh with the new changes!
# timeout of 1 second, preventing crazy loop speeds
inotifywait -qq -e close_write "$inputfile" -t 1
result="$?"
if [ "$result" != "2" ];
then
svgbob_cli "$inputfile" > "$outputfile"
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment