1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-22 11:31:30 +01:00

zsh: rename watch_file to watch_and_run, allow watching directories

This commit is contained in:
bretello 2024-08-08 17:36:58 +02:00
parent 015dd26a90
commit f03f0f6105

View File

@ -315,15 +315,15 @@ function mangrep() {
} }
# watches the given file and executes the given action whenever the file is changed. Usage: watch_file <file> <command> # watches the given file/directory and executes the given action whenever anything is changed. Usage: watch_and_run <file|directory> <command>
function watch_file() { function watch_and_run() {
if ! which inotifywait 2>&1 &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi if ! which inotifywait 2>&1 &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi
[[ -z "$1" ]] && echo "Usage: $0 <file> <action>" && return 1 [[ -z "$1" ]] && echo "Usage: $0 <file|directory> <action>" && return 1
local file=$1 local to_watch=$1
shift shift
[[ -z "$@" ]] && echo "Usage: $0 <file> <action>" && return 1 [[ -z "$@" ]] && echo "Usage: $0 <file|directory> <action>" && return 1
local action="$@" local action="$@"
local _bg_job_pid local _bg_job_pid
@ -340,8 +340,8 @@ function watch_file() {
{ {
set +m # disable job control messages set +m # disable job control messages
while true; do while true; do
if [[ ! -f "$file" ]]; then if [[ ! -e "$to_watch" ]]; then
echo "File \"$file\" does not exist"; echo "\"$to_watch\" does not exist";
break break
fi fi
zsh -c "$action" & zsh -c "$action" &
@ -349,8 +349,8 @@ function watch_file() {
disown disown
echo -e "$Cyan ==> Running \"$action\" with pid=${_bg_job_pid}$CLEAR" echo -e "$Cyan ==> Running \"$action\" with pid=${_bg_job_pid}$CLEAR"
# block until the file has been written to # block until the file/directory has been written to
inotifywait -e close_write "$file" &>/dev/null inotifywait -e close_write -r "$to_watch" &>/dev/null
# the job might have failed, ignore errors # the job might have failed, ignore errors
kill -9 ${_bg_job_pid} &>/dev/null || true kill -9 ${_bg_job_pid} &>/dev/null || true