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

Re: zsh watch function



And then I saw the padding light, here an improved version:

watch () {
    IN=2
    case $1 in
        -n)
            IN=$2
            shift 2
            ;;
    esac
    clear
    HN="$(hostname)"
    CM="$*"
    LEFT="$(printf 'Every %.1f: %s' $IN $CM)"
    ((PAD = COLUMNS - ${#LEFT}))
    while :
    do
        DT=$(date)
        printf "$LEFT%${PAD}s\n" "$HN $(date)"
        eval "$CM"
        sleep $IN
        clear
    done
}

On Sat, May 16, 2020 at 2:20 PM Han Boetes <hboetes@xxxxxxxxx> wrote:

> 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