Last active
September 6, 2024 04:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env sh | |
# Example usage: | |
# $ watchfile Test.idr idris2 --check | |
# $ watchfile test.sh shellcheck | |
# $ watchfile Main.hs -- cabal test | |
file=$1; shift | |
tmp="/tmp/watch.$(basename "$file")" | |
if [ "$1" = "--" ]; then | |
shift; | |
pass=true | |
else | |
pass=false | |
fi | |
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 | |
if "$pass"; then | |
output=$("$@" 2>&1) | |
else | |
output=$("$@" "$file" 2>&1) | |
fi | |
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 --recursive --event modify "$file" | |
cleanup | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment