1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-22 03:21: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>
function watch_file() {
# watches the given file/directory and executes the given action whenever anything is changed. Usage: watch_and_run <file|directory> <command>
function watch_and_run() {
if ! which inotifywait 2>&1 &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi
[[ -z "$1" ]] && echo "Usage: $0 <file> <action>" && return 1
local file=$1
[[ -z "$1" ]] && echo "Usage: $0 <file|directory> <action>" && return 1
local to_watch=$1
shift
[[ -z "$@" ]] && echo "Usage: $0 <file> <action>" && return 1
[[ -z "$@" ]] && echo "Usage: $0 <file|directory> <action>" && 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