-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-watch.plugin.zsh
61 lines (50 loc) · 1.42 KB
/
zsh-watch.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env zsh
ZSH_WATCH=${ZSH_WATCH:-watch}
ZSH_WATCH_FLAGS=${ZSH_WATCH_FLAGS:-"-n1 -t -d"} # Default flags for watch
_zsh_watch_run() {
if (( ! $+commands[$ZSH_WATCH] )); then
echo -e "\033[31mCommand $ZSH_WATCH not found.\033[0m"
return 1
fi
local watch_exec="$commands[$ZSH_WATCH]"
local watch_flags=() # The flags for watch exec, not for user command
if [ $# = 0 ]; then
$watch_exec -h
return
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
$watch_exec -h
return
;;
-n | --interval | -q | --equexit | -d | --differences)
watch_flags+=($1)
if [[ ! $2 = -* ]]; then
watch_flags+=($2)
shift
fi
;;
-*) watch_flags+=($1) ;;
*) break ;;
esac
shift
done
if [ $#watch_flags = 0 ]; then
watch_flags+=( ${=ZSH_WATCH_FLAGS} )
fi
if declare -f -- "$1" >/dev/null 2>&1; then
echo -e "\033[31mFunctions are not support yet.\033[0m"
return 1
fi
local cmd=$1
local cmd_args="${@:2}"
# expand aliases
unset 'functions[_zsh-watch-expand]'
functions[_zsh-watch-expand]=$cmd
(($+functions[_zsh-watch-expand])) && cmd=${functions[_zsh-watch-expand]#$'\t'}
$watch_exec ${watch_flags[*]} $cmd ${cmd_args[*]}
}
watch() {
_zsh_watch_run $@
}