zsh-users
 help / color / mirror / code / Atom feed
From: Emanuel Berg <moasenwood@zoho.eu>
To: zsh-users@zsh.org
Subject: break loop with key, still don't exit the program + code
Date: Thu, 09 May 2019 07:14:29 +0200	[thread overview]
Message-ID: <8636loky4q.fsf@zoho.eu> (raw)

How can I prematurely break from a loop, or
a deliberately infinite loop for that matter,
while still don't exit the program?

I.e.,

#! /bin/zsh

while (true); do
    # do computations  
done
# when interrupted by the user,
# do a summary of the computations
# and output

# only then, exit program

The particular use case is yanked last.
Feel free to comment on the code as well, of
course!

The purpose is I stretch for ~1h every day
(e.g. today 1h 3m 55s) and use the below
functions to signal when too change position.
Only sometimes, I don't do the entire program,
say I stretch for ~30m or whatever instead.
With the output from the 'stretch' program,
when interrupted prematurely, I can do

$ echo $(s2hms $(( 28*65 )) )
30m 20s

and write it down it in my flipboard. That way,
digits won't lie :)

Only, I'd like to have this automated so
whenever 'stretch' is interrupted, last thing
it'll do is make the computation and output it.

Thanks in advance :)

#! /bin/zsh

# https://dataswamp.org/~incal/conf/.zsh/stretch
play-stretch-sound () {
    local sound=~/bo/up.mp3

    if [[ -f $sound ]]; then
        omx-play-whatever $OMX_SND_MOD $sound
    else
        echo "Error: Can't find sound file: $sound" >&2
    fi
}

stretch () {
    local init_time=19

    local s_time=65
    local stretches=70

    local total_s=$(( $s_time * $stretches ))
    local total_time="$(s2hms ${total_s})"

    echo "Total time: $total_time (${stretches}*${s_time}s)"
    play-stretch-sound

    local current=0
    sleep $init_time
    echo -n "current:"
    repeat $stretches {
            current=$(( $current + 1 ))
            echo -n " $current"

            play-stretch-sound
            sleep $s_time
    }
    repeat 2 play-stretch-sound
}
alias st=stretch

# https://dataswamp.org/~incal/conf/.zsh/time
s2hms () {
    local secs=$1

    local time_string
    time_string=$(units -t $secs\s hms)

    local -a data
    data=("${(@s/;/)time_string}")

    local h=$data[1]
    local m=$data[2]
    local s=$data[3]

    local out
    [[ $h > 0 ]] &&  out="${h}h "
    [[ $m > 0 ]] && out+="${m}m "
    [[ $s > 0 ]] && out+="${s}s"
    echo $out
}
alias hms=s2hms

-- 
underground experts united
http://user.it.uu.se/~embe8573


                 reply	other threads:[~2019-05-09  5:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=8636loky4q.fsf@zoho.eu \
    --to=moasenwood@zoho.eu \
    --cc=zsh-users@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).