mirror of
https://github.com/notwa/rc
synced 2024-11-05 03:29:02 -08:00
42 lines
1.1 KiB
Bash
Executable file
42 lines
1.1 KiB
Bash
Executable file
#!/usr/bin/env zsh
|
|
# YES_ZSH
|
|
|
|
monitor() { ### @-
|
|
### this is `watch` loosely reimplemented as a shell script.
|
|
local bottom=0 strip=0 interval=2 opt=
|
|
while getopts 'fsn:h' opt; do
|
|
case $opt in
|
|
f) bottom=1;; # align with bottom of terminal
|
|
s) strip=1;; # strip last character assuming it's a newline
|
|
n) interval="$OPTARG";;
|
|
?) echo -E "usage: $0 [-fs] [-n {period}] {command} [{args...}]"
|
|
[ $opt = h ] && return 0 || return 1;;
|
|
esac
|
|
done
|
|
shift $((OPTIND-1))
|
|
|
|
local cmd=${1:?no command specified}
|
|
shift
|
|
|
|
function _monclear() {
|
|
[ $bottom -eq 1 ] && printf ${(l:$((LINES*2))::\n:)}
|
|
}
|
|
function _monfit() {
|
|
if [ $strip -eq 1 ]; then
|
|
tail -$((LINES)) | head -c-1
|
|
else
|
|
tail -$((LINES-1))
|
|
fi
|
|
}
|
|
|
|
local stop=0
|
|
trap stop=1 INT
|
|
echo -en "\e[H\e[2J"
|
|
while [ $stop -eq 0 ]; do
|
|
echo -en "\e[1J\e[H"
|
|
{ _monclear; "$cmd" "$@" 2>&1 } | fold -w $COLUMNS | _monfit
|
|
sleep "$interval" || break
|
|
done
|
|
}
|
|
|
|
[ "${SOURCING:-0}" -gt 0 ] || monitor "$@"
|