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

Re: zsh watch function



Hello Daniel,

That's very observant indeed. Here is a new version. I also choose printf
'\033c' in favour of the external command "clear."

Please tell if I missed anything else.

watch () {
    local PAD
    local IN=2
    case $1 in
        -n)
            IN=$2
            shift 2
            ;;
    esac
    printf '\033c'
    local CM="$*"
    local LEFT="$(printf 'Every %.1f: %s' $IN $CM)"
    ((PAD = COLUMNS - ${#LEFT}))
    while :
    do
        echo -nE "$LEFT"
        printf "%${PAD}s\n" "$HOST $(date)"
        eval "$CM"
        sleep $IN
        printf '\033c'
    done
}

On Sat, May 16, 2020 at 10:59 PM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
wrote:

> Han Boetes wrote on Sat, 16 May 2020 16:31 +0200:
> > And then I saw the padding light, here an improved version:
> >
> > watch () {
> >     IN=2
>
> The code isn't WARN_CREATE_GLOBAL-clean.
>
> >     CM="$*"
> >     LEFT="$(printf 'Every %.1f: %s' $IN $CM)"
> ⋮
> >         printf "$LEFT%${PAD}s\n" "$HN $(date)"
>
> $LEFT may contain unescaped percent signs from the input.
>
> >         eval "$CM"
> >         sleep $IN
> >         clear
> >     done
> > }
>
> For context to others, note that watch(1) on FreeBSD does something
> entirely different to what watch(1) does on Linux.
>


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