Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Last active September 7, 2023 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfsternberg/892e91ea27554746fa7709054170871d to your computer and use it in GitHub Desktop.
Save elfsternberg/892e91ea27554746fa7709054170871d to your computer and use it in GitHub Desktop.
A very (very!) simple screensaver for watching a folder with slow progressive downloads
#!/usr/bin/env bash
# Absolutely dumb progressive "screensaver". Basically, it watches a folder and, whenever a new
# image appears, puts that one up fullscreen, then tears down the prior one underneath. Useful for a
# folder where you're either doing slow downloads or rendering illustrations and just want to watch
# the results.
# Requires [feh](https://feh.finalrewind.org/),
# [tac](https://man7.org/linux/man-pages/man1/tac.1.html)
# inotifywait(https://github.com/inotify-tools/inotify-tools).
set -o errexit
set -o nounset
shopt -s nullglob
function _die() {
echo "ERROR $? IN ${BASH_SOURCE[0]} AT LINE ${BASH_LINENO[0]}" 1>&2
exit 1
}
trap _die ERR
PID=""
# Disabling because shellcheck has no good answer for "ls -t"
# shellcheck disable=SC2012
LATEST=$(ls -t ./*.png | head -1)
if [ -n "$LATEST" ]; then
feh --fullscreen --draw-filename --auto-zoom "$LATEST" &
PID="$!"
fi
inotifywait -m . -e create -e moved_to |
while read -r _path _action file; do
# Needed because create can be triggered before the file is completely coherent. Probably
# not the best way, but haven't seen in glitch yet.
sleep 1
feh --fullscreen --draw-filename --auto-zoom "$file" &
NPID="$!"
if [ -n "$PID" ]; then
if ps --pid "$PID" > /dev/null; then
# This delay prevents a flash as the prior process gets killed faster than X can set
# up the view for the new image.
sleep 1
kill $PID
fi
fi
PID="$NPID"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment