banovski

Timer (simplified)

Oct 31st, 2025 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.43 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # The time to count down is entered as a number of minutes, via a
  4. # dialog. The remaining time is shown in the notification area as an
  5. # icon with two digits. A number of minutes greater than 99 is
  6. # indicated as the number of hours and the number of tens of minutes
  7. # and is shown in an icon with separate formatting. When the number of
  8. # remaining minutes is triangular, the remaining time is spoken; so
  9. # the intervals between the times announced as audio decrease, e.g.
  10. # ... 36, 28, 21, 15, 10, 6, 3, 1. The script has been tested on
  11. # Xubuntu. It requires espeak and yad. It uses images for icons that
  12. # appear in the notification area, so they need to be ready, properly
  13. # named -- 00.png, 01.png, 10.png, etc. -- and the paths to the actual
  14. # directories containing them should be specified in the script. The
  15. # ones tested have the size 16x16 pixels and backgrounds of two
  16. # different colors.
  17.  
  18. function quit()
  19. {
  20.     espeak "$1" &
  21.     exit "$2"
  22. }
  23.  
  24. function value_is_triangular()
  25. {
  26.     # If the square root of a number preliminarily multiplied by eight
  27.     # and incremented is an integer, the number is triangular. If the
  28.     # number is an integer, bc outputs it with a dot and trailing
  29.     # zeros nonetheless. The dot and the zeros are pattern-matched by
  30.     # grep.
  31.  
  32.     if echo "sqrt($1 * 8 + 1)" | bc -l | grep -Eo '\.0+$' > /dev/null; then
  33.         return 0
  34.     else
  35.         return 1
  36.     fi
  37. }
  38.  
  39. function format_announcement_values_units()
  40. {
  41.     value="$1"
  42.     if [ "$1" -eq 0 ]; then
  43.         value=""
  44.         unit=""
  45.     elif [ "$1" -eq 1 ]; then
  46.         unit="$2"
  47.     else
  48.         unit="${2}s"
  49.     fi
  50. }
  51.  
  52. function announce()
  53. {
  54.     hours_number=$(($1 / 60))
  55.     format_announcement_values_units "$hours_number" "hour"
  56.     hours="$value $unit"
  57.     minutes_number=$(($1 % 60))
  58.     format_announcement_values_units "$minutes_number" "minute"
  59.     minutes="$value $unit"
  60.     espeak -v "whisper" "$hours $minutes left"
  61. }
  62.  
  63. function display_minutes()
  64. {
  65.     file_name_no_extension=$(printf "%02d" "$1")
  66.     yad --notification --image "${MINUTES_IMAGES_DIRECTORY}/${file_name_no_extension}.png" --timeout 60 && quit "Aborted!" 1
  67. }
  68.  
  69. function display_hours_minutes()
  70. {
  71.     hours_number=$(($1 / 60))
  72.     tens_of_minutes_number=$(($1 % 60 / 10))
  73.     file_name_no_extension="${hours_number}${tens_of_minutes_number}"
  74.     yad --notification --image "${HOURS_MINUTES_IMAGES_DIRECTORY}/${file_name_no_extension}.png" --timeout 60 && quit "Aborted!" 1
  75. }
  76.  
  77. # Change the following paths to the ones containing actual images for
  78. # the notification area.
  79. MINUTES_IMAGES_DIRECTORY=~/images/icons/timer/purple
  80. HOURS_MINUTES_IMAGES_DIRECTORY=~/images/icons/timer/blue
  81.  
  82. VALID_INPUT_PATTERN='^[1-9][0-9]*$'
  83.  
  84. input=$(yad --entry --title "Timer" --text "Enter the number of minutes: " --entry-text "30" --borders "8") || quit "Aborted!" 1
  85.  
  86. [[ $input =~ $VALID_INPUT_PATTERN ]] || quit "Invalid input! Exiting!" 1
  87. [ "$input" -lt 960 ] || quit "Maximum value of 959 is exceeded! Exiting!" 1
  88.  
  89. remaining_time=$input
  90.  
  91. while [ "$remaining_time" -gt 0 ]; do
  92.  
  93.     if value_is_triangular "$remaining_time"; then
  94.         announce "$remaining_time" &
  95.     fi
  96.  
  97.     if [ "$remaining_time" -lt 100 ]; then
  98.         display_minutes "$remaining_time"
  99.     else
  100.         display_hours_minutes "$remaining_time"
  101.     fi
  102.  
  103.     remaining_time=$((remaining_time - 1))
  104. done
  105.  
  106. yad --notification --image "emblem-urgent" &
  107. quit "Time's up!" 0
  108.  
Advertisement
Add Comment
Please, Sign In to add comment