1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-14 08:29:03 -08:00
rc/sh/monitor

51 lines
1.5 KiB
Text
Raw Normal View History

#!/usr/bin/env zsh
# compat: -ash -bash -dash -hush -ksh -mksh -oksh -osh -posh -yash +zsh
2013-10-26 03:49:18 -07:00
2021-07-30 17:57:08 -07:00
monitor() { ### @-
2021-08-01 09:27:25 -07:00
### this is [watch(1)](https://www.man7.org/linux/man-pages/man1/watch.1.html)
### loosely reimplemented as a shell script.
###
2021-08-01 09:45:19 -07:00
### ```
### usage: monitor [-fs] [-n {period}] {command} [{args...}]
### ```
###
### **NOTE:** there also exists monitor(1) programs provided by
### the *389-ds-base* and *dmucs* packages that i don't use.
2021-07-29 00:37:35 -07:00
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))
2013-10-26 03:49:18 -07:00
2021-07-29 00:37:35 -07:00
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
2013-10-26 03:49:18 -07:00
}
[ -n "${preload+-}" ] || monitor "$@"