From f03f0f61053b3ad7c679bcb297ace84562d8176f Mon Sep 17 00:00:00 2001 From: bretello Date: Thu, 8 Aug 2024 17:36:58 +0200 Subject: [PATCH] zsh: rename watch_file to watch_and_run, allow watching directories --- functions/misc.zsh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/functions/misc.zsh b/functions/misc.zsh index 784bf8a..08c6b59 100755 --- a/functions/misc.zsh +++ b/functions/misc.zsh @@ -315,15 +315,15 @@ function mangrep() { } -# watches the given file and executes the given action whenever the file is changed. Usage: watch_file -function watch_file() { +# watches the given file/directory and executes the given action whenever anything is changed. Usage: watch_and_run +function watch_and_run() { if ! which inotifywait 2>&1 &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi - [[ -z "$1" ]] && echo "Usage: $0 " && return 1 - local file=$1 + [[ -z "$1" ]] && echo "Usage: $0 " && return 1 + local to_watch=$1 shift - [[ -z "$@" ]] && echo "Usage: $0 " && return 1 + [[ -z "$@" ]] && echo "Usage: $0 " && return 1 local action="$@" local _bg_job_pid @@ -340,8 +340,8 @@ function watch_file() { { set +m # disable job control messages while true; do - if [[ ! -f "$file" ]]; then - echo "File \"$file\" does not exist"; + if [[ ! -e "$to_watch" ]]; then + echo "\"$to_watch\" does not exist"; break fi zsh -c "$action" & @@ -349,8 +349,8 @@ function watch_file() { disown echo -e "$Cyan ==> Running \"$action\" with pid=${_bg_job_pid}$CLEAR" - # block until the file has been written to - inotifywait -e close_write "$file" &>/dev/null + # block until the file/directory has been written to + inotifywait -e close_write -r "$to_watch" &>/dev/null # the job might have failed, ignore errors kill -9 ${_bg_job_pid} &>/dev/null || true