Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shopt -s checkwinsize # for COLUMNS
- # Requires bash-boost: https://github.com/tomocafe/bash-boost
- source "$HOME/bash-boost/latest/bash-boost.sh"
- bb_load cli util
- bb_setprog "timer"
- bb_addopt H:hours "Hours" 0
- bb_addopt M:mins "Minutes" 0
- bb_addopt S:secs "Seconds" 0
- bb_addopt red "Make display red at R seconds remaining (default: 10)" 10
- bb_addopt yellow "Make display yellow at Y seconds remaining (default: 60)" 60
- bb_addopt F:timefmt "Time format (default: %H:%M:%S)" "%H:%M:%S"
- bb_addopt i:interval interval 1
- bb_parseargs "$@"
- bb_getopt -v h hours
- bb_getopt -v m mins
- bb_getopt -v s secs
- bb_getopt -v interval interval
- bb_getopt -v red red
- bb_getopt -v yellow yellow
- bb_getopt -v fmt timefmt
- countdown=$(( h*3600 + m*60 + s ))
- [[ $countdown -gt 0 ]] || exit 0
- bb_info "Countdown will complete at $(bb_timefmt "%F %T" $(bb_now "+${countdown}s"))"
- while [[ $countdown -gt 0 ]]; do
- # convert countdown seconds to time string
- TZ= printf -v text "%($fmt)T" $countdown
- # center the uncolored time string
- bb_centerstr -v line $COLUMNS "$text"
- # determine color, then add color to the time string
- color="green"
- if [[ $countdown -le $red ]]; then
- color="red"
- elif [[ $countdown -le $yellow ]]; then
- color="yellow"
- fi
- colortext="$(bb_rawcolor "$color" "$text")"
- # replace uncolored centered text with colored text, print it on the existing line
- printf "\r%s" "${line/$text/$colortext}"
- # sleep based on interval
- bb_min -v i $countdown $interval
- (( countdown -= i ))
- sleep "$i"
- done
- trap "echo" EXIT # make sure we finish the line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement