banovski

Timer

Mar 27th, 2018 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function quit()
  4. {
  5.     say "$1"
  6.     kill $dialog_pid
  7.     exit "$2"
  8. }
  9.  
  10. function say()
  11. {
  12.     espeak -v english "$1" &
  13. }
  14.  
  15. function enter()
  16. {
  17.     input_time=$(zenity --entry --title="Remit" --text="Enter time interval in minutes" --entry-text=30)
  18.     if test "$?" -ne 0; then
  19.         quit "Aborted!" 1
  20.     fi
  21. }
  22.  
  23. function display()
  24. {
  25.     remaining_time_in_minutes=$1
  26.     if test $remaining_time_in_minutes -le 99; then
  27.         formated_number=$(printf "%02d" $remaining_time_in_minutes)
  28.         image_file_url="$image_directory_url/purple/$formated_number.png"
  29.         yad --notification --image="$image_file_url" --timeout=60
  30.     else
  31.         number_of_hours=$((remaining_time_in_minutes / 60))
  32.         remainder_in_minutes=$((remaining_time_in_minutes % 60))
  33.         number_of_tens_of_minutes=$((remainder_in_minutes / 10))
  34.         image_file_url="$image_directory_url/blue/$number_of_hours$number_of_tens_of_minutes.png"
  35.         yad --notification --image="$image_file_url" --timeout=60
  36.     fi
  37. }
  38.  
  39. function announce()
  40. {
  41.     remaining_time_in_minutes=$1
  42.     number_of_hours=$((remaining_time_in_minutes / 60))
  43.     number_of_minutes=$((remaining_time_in_minutes % 60))
  44.  
  45.     case $number_of_hours in
  46.         0)
  47.             hour_word=""
  48.             number_of_hours=""
  49.             ;;
  50.         1)
  51.             hour_word="hour"
  52.             ;;
  53.         *)
  54.             hour_word="hours"
  55.     esac
  56.  
  57.     case $number_of_minutes in
  58.         0)
  59.             minute_word=""
  60.             number_of_minutes=""
  61.             ;;
  62.         1)
  63.             minute_word="minute"
  64.             ;;
  65.         *)
  66.             minute_word="minutes"
  67.     esac
  68.     say "$number_of_hours $hour_word $number_of_minutes $minute_word left"
  69. }
  70.  
  71. image_directory_url="/home/skab/Data/izobrazheniya/sortirovannyye/kompyuter/znachki/tsyfry"
  72.  
  73. enter
  74. until [[ $input_time =~ ^[0-9]+$ ]]; do
  75.     say "Wrong input format"
  76.     enter
  77. done
  78.  
  79. announce "$input_time"
  80. announcement_time=$((input_time * 2 / 3))
  81. current_time=$input_time
  82.  
  83. zenity --error --title="Remit" --text="Stop the timer" &
  84. dialog_pid=$!
  85. dialog_flag=$(wmctrl -l | grep "Remit")
  86. until test "$dialog_flag"; do
  87.     dialog_flag=$(wmctrl -l | grep "Remit")
  88. done
  89. wmctrl -r "Remit" -t 2
  90.  
  91. while test "$current_time" -gt 0; do
  92.     if ! kill -0 $dialog_pid; then
  93.         quit "Aborted" 1
  94.     fi
  95.  
  96.     if test $current_time -eq $announcement_time; then
  97.         announcement_time=$((announcement_time * 2 / 3))
  98.         announce $current_time &
  99.     fi
  100.     display $current_time
  101.     current_time=$((current_time - 1))
  102. done
  103.  
  104. quit "Time's up!" 0
  105.  
Add Comment
Please, Sign In to add comment