Advertisement
Guest User

timer demo

a guest
Aug 4th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 0
  1. shopt -s checkwinsize # for COLUMNS
  2.  
  3. # Requires bash-boost: https://github.com/tomocafe/bash-boost
  4. source "$HOME/bash-boost/latest/bash-boost.sh"
  5.  
  6. bb_load cli util
  7.  
  8. bb_setprog "timer"
  9. bb_addopt H:hours "Hours" 0
  10. bb_addopt M:mins "Minutes" 0
  11. bb_addopt S:secs "Seconds" 0
  12. bb_addopt red "Make display red at R seconds remaining (default: 10)" 10
  13. bb_addopt yellow "Make display yellow at Y seconds remaining (default: 60)" 60
  14. bb_addopt F:timefmt "Time format (default: %H:%M:%S)" "%H:%M:%S"
  15. bb_addopt i:interval interval 1
  16. bb_parseargs "$@"
  17.  
  18. bb_getopt -v h hours
  19. bb_getopt -v m mins
  20. bb_getopt -v s secs
  21. bb_getopt -v interval interval
  22. bb_getopt -v red red
  23. bb_getopt -v yellow yellow
  24. bb_getopt -v fmt timefmt
  25.  
  26. countdown=$(( h*3600 + m*60 + s ))
  27. [[ $countdown -gt 0 ]] || exit 0
  28.  
  29. bb_info "Countdown will complete at $(bb_timefmt "%F %T" $(bb_now "+${countdown}s"))"
  30.  
  31. while [[ $countdown -gt 0 ]]; do
  32.     # convert countdown seconds to time string
  33.     TZ= printf -v text "%($fmt)T" $countdown
  34.     # center the uncolored time string
  35.     bb_centerstr -v line $COLUMNS "$text"
  36.     # determine color, then add color to the time string
  37.     color="green"
  38.     if [[ $countdown -le $red ]]; then
  39.         color="red"
  40.     elif [[ $countdown -le $yellow ]]; then
  41.         color="yellow"
  42.     fi
  43.     colortext="$(bb_rawcolor "$color" "$text")"
  44.     # replace uncolored centered text with colored text, print it on the existing line
  45.     printf "\r%s" "${line/$text/$colortext}"
  46.     # sleep based on interval
  47.     bb_min -v i $countdown $interval
  48.     (( countdown -= i ))
  49.     sleep "$i"
  50. done
  51.  
  52. trap "echo" EXIT # make sure we finish the line
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement