zsh-users
 help / Atom feed
* break loop with key, still don't exit the program + code
@ 2019-05-09  5:14 Emanuel Berg
  0 siblings, 0 replies; 1+ messages in thread
From: Emanuel Berg @ 2019-05-09  5:14 UTC (permalink / raw)
  To: zsh-users

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


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

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-09  5:14 break loop with key, still don't exit the program + code Emanuel Berg

zsh-users

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

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


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