Skip to content

Instantly share code, notes, and snippets.

@LSLeary
Last active March 8, 2023 23:07
Show Gist options
  • Save LSLeary/a7ddc196d119d52e2767f66873e24bdd to your computer and use it in GitHub Desktop.
Save LSLeary/a7ddc196d119d52e2767f66873e24bdd to your computer and use it in GitHub Desktop.
A reasonable approximation of ghcid for arbitrary files and interpreters, with nice scrolling and searching via less. Only needs inotifywait on the $PATH.
#! /usr/bin/env sh
# Example usage:
# $ watchfile Test.idr idris2 --check
# $ watchfile test.sh shellcheck
file=$1; shift
tmp="/tmp/watch.$(basename "$file")"
silently() {
"$@" > /dev/null 2>&1
}
cleanup() {
silently kill %%
silently rm "$tmp"
}
trap 'cleanup; exit' INT TERM HUP
hms() {
printf ' (at %s)' "$(date +%_H:%M:%S)"
}
title() {
printf '\033]2;watchfile %s: %s%s\a' "$file" "$1" "$(hms)"
}
sgr() {
printf '\033[%sm' "$1"
}
coloured() {
sgr "$1"
printf '%s' "$2"
sgr 0
}
while true; do
clear
output=$("$@" "$file")
ecode="$?"
if [ "$ecode" = "0" ]; then
colour=32 # green
result="All good"
else
colour=31 # red
result="Error"
fi
title "$result"
if [ -z "$output" ]; then
coloured "$colour" "$result"
hms
else
echo "$output" > "$tmp"
less "$tmp" &
fi
inotifywait --quiet --event modify "$file"
cleanup
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment