Skip to content

Instantly share code, notes, and snippets.

@EricDriussi
Last active November 1, 2022 12:02
Show Gist options
  • Save EricDriussi/2af0e1be3f4ff3eedf4053ef20522449 to your computer and use it in GitHub Desktop.
Save EricDriussi/2af0e1be3f4ff3eedf4053ef20522449 to your computer and use it in GitHub Desktop.
Generic test watcher / hot reload function for zsh and bash shells (depends on inotify-tools).
alias watch="run_on_change"; run_on_change() {
color_command(){
# Modify these values to change behavior
pass_terms="pass|ok"
fail_terms="fail|failed"
pass_color=$'\e[1;32m'
fail_color=$'\e[1;31m'
reset_color=$'\e[0m'
colored_pass_terms="✅ ${pass_color}&${reset_color}"
colored_fail_terms="❌ ${fail_color}&${reset_color}"
# Use sed to parse output and color it
"$@" | sed \
-Ee "s/${pass_terms}/${colored_pass_terms}/I" \
-Ee "s/${fail_terms}/${colored_fail_terms}/I"
}
# Run command once
color_command "$@"
# Loop and use inotify-tools to re-run on change
while true; do
inotifywait -qq -r -e create,modify,move,delete ./ &&
printf "\n[ . . . Re-running command . . . ]\n" &&
color_command "$@"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment