zsh-workers
 help / color / Atom feed
* zsh watch function
@ 2020-05-16 12:20 Han Boetes
  2020-05-16 14:31 ` Han Boetes
  2020-05-16 14:36 ` Roman Perepelitsa
  0 siblings, 2 replies; 5+ messages in thread
From: Han Boetes @ 2020-05-16 12:20 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1: Type: text/plain, Size: 868 bytes --]

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
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh watch function
  2020-05-16 12:20 zsh watch function Han Boetes
@ 2020-05-16 14:31 ` Han Boetes
  2020-05-16 20:59   ` Daniel Shahaf
  2020-05-16 14:36 ` Roman Perepelitsa
  1 sibling, 1 reply; 5+ messages in thread
From: Han Boetes @ 2020-05-16 14:31 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1: Type: text/plain, Size: 1446 bytes --]

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@gmail.com> 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
> }
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh watch function
  2020-05-16 12:20 zsh watch function Han Boetes
  2020-05-16 14:31 ` Han Boetes
@ 2020-05-16 14:36 ` Roman Perepelitsa
  1 sibling, 0 replies; 5+ messages in thread
From: Roman Perepelitsa @ 2020-05-16 14:36 UTC (permalink / raw)
  To: Han Boetes; +Cc: Zsh hackers list

On Sat, May 16, 2020 at 2:21 PM Han Boetes <hboetes@gmail.com> wrote:
>
> https://unix.stackexchange.com/questions/260323/watch-equivalent-in-zsh

The accepted answer on this question is good.

Roman.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh watch function
  2020-05-16 14:31 ` Han Boetes
@ 2020-05-16 20:59   ` Daniel Shahaf
  2020-05-16 23:10     ` Han Boetes
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2020-05-16 20:59 UTC (permalink / raw)
  To: Han Boetes; +Cc: zsh-workers

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh watch function
  2020-05-16 20:59   ` Daniel Shahaf
@ 2020-05-16 23:10     ` Han Boetes
  0 siblings, 0 replies; 5+ messages in thread
From: Han Boetes @ 2020-05-16 23:10 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-workers


[-- Attachment #1: Type: text/plain, Size: 1320 bytes --]

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@daniel.shahaf.name>
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.
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-16 12:20 zsh watch function Han Boetes
2020-05-16 14:31 ` Han Boetes
2020-05-16 20:59   ` Daniel Shahaf
2020-05-16 23:10     ` Han Boetes
2020-05-16 14:36 ` Roman Perepelitsa

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git