zsh-workers
 help / color / mirror / code / Atom feed
From: Han Boetes <hboetes@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: zsh watch function
Date: Sat, 16 May 2020 16:31:00 +0200	[thread overview]
Message-ID: <CAOzo9e4phyeSNmPAY_hk3YChrivB_7y76b_PS+CXaMyATO2f-w@mail.gmail.com> (raw)
In-Reply-To: <CAOzo9e5HaXkoZSaVtb8gfJzV7ZThY1dStC8ToxLK68WtSBZJLQ@mail.gmail.com>

[-- 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
> }
>

  reply	other threads:[~2020-05-16 14:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-16 12:20 Han Boetes
2020-05-16 14:31 ` Han Boetes [this message]
2020-05-16 20:59   ` Daniel Shahaf
2020-05-16 23:10     ` Han Boetes
2020-05-16 14:36 ` Roman Perepelitsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAOzo9e4phyeSNmPAY_hk3YChrivB_7y76b_PS+CXaMyATO2f-w@mail.gmail.com \
    --to=hboetes@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).