Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

zsh watch function



In this online question someone asked if a watch internal command was
available, like with bash:

https://unix.stackexchange.com/questions/260323/watch-equivalent-in-zsh

I couldn't help myself but to write the code for that. Fun fact is that
code-colouring and aliases work. This code is probably not fool proof. Feel
free to improve. Please consider adding it in improved form to the ZSH
distribution.

watch () {
    IN=2
    case $1 in
        -n)
            IN=$2
            shift 2
            ;;
    esac
    clear
    HN="$(hostname)"
    HD="$(printf 'Every %.1f: ' $IN)"
    CM="$*"
    # Where does that -2 come from?
    ((PAD = COLUMNS - ${#HD} - ${#CM} - ${#DT} - 2))
    while :
    do
        DT=$(date)
        printf "$HD%s%${PAD}s: %s\n\n" "$CM" "$HN" "$DT"
        # echo "$LFT $RHT"
        eval "$CM"
        sleep $IN
        clear
    done
}


Messages sorted by: Reverse Date, Date, Thread, Author