Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # The time to count down is entered as a number of minutes, via a
- # dialog. The remaining time is shown in the notification area as an
- # icon with two digits. A number of minutes greater than 99 is
- # indicated as the number of hours and the number of tens of minutes
- # and is shown in an icon with separate formatting. When the number of
- # remaining minutes is triangular, the remaining time is spoken; so
- # the intervals between the times announced as audio decrease, e.g.
- # ... 36, 28, 21, 15, 10, 6, 3, 1. The script has been tested on
- # Xubuntu. It requires espeak and yad. It uses images for icons that
- # appear in the notification area, so they need to be ready, properly
- # named -- 00.png, 01.png, 10.png, etc. -- and the paths to the actual
- # directories containing them should be specified in the script. The
- # ones tested have the size 16x16 pixels and backgrounds of two
- # different colors.
- function quit()
- {
- espeak "$1" &
- exit "$2"
- }
- function value_is_triangular()
- {
- # If the square root of a number preliminarily multiplied by eight
- # and incremented is an integer, the number is triangular. If the
- # number is an integer, bc outputs it with a dot and trailing
- # zeros nonetheless. The dot and the zeros are pattern-matched by
- # grep.
- if echo "sqrt($1 * 8 + 1)" | bc -l | grep -Eo '\.0+$' > /dev/null; then
- return 0
- else
- return 1
- fi
- }
- function format_announcement_values_units()
- {
- value="$1"
- if [ "$1" -eq 0 ]; then
- value=""
- unit=""
- elif [ "$1" -eq 1 ]; then
- unit="$2"
- else
- unit="${2}s"
- fi
- }
- function announce()
- {
- hours_number=$(($1 / 60))
- format_announcement_values_units "$hours_number" "hour"
- hours="$value $unit"
- minutes_number=$(($1 % 60))
- format_announcement_values_units "$minutes_number" "minute"
- minutes="$value $unit"
- espeak -v "whisper" "$hours $minutes left"
- }
- function display_minutes()
- {
- file_name_no_extension=$(printf "%02d" "$1")
- yad --notification --image "${MINUTES_IMAGES_DIRECTORY}/${file_name_no_extension}.png" --timeout 60 && quit "Aborted!" 1
- }
- function display_hours_minutes()
- {
- hours_number=$(($1 / 60))
- tens_of_minutes_number=$(($1 % 60 / 10))
- file_name_no_extension="${hours_number}${tens_of_minutes_number}"
- yad --notification --image "${HOURS_MINUTES_IMAGES_DIRECTORY}/${file_name_no_extension}.png" --timeout 60 && quit "Aborted!" 1
- }
- # Change the following paths to the ones containing actual images for
- # the notification area.
- MINUTES_IMAGES_DIRECTORY=~/images/icons/timer/purple
- HOURS_MINUTES_IMAGES_DIRECTORY=~/images/icons/timer/blue
- VALID_INPUT_PATTERN='^[1-9][0-9]*$'
- input=$(yad --entry --title "Timer" --text "Enter the number of minutes: " --entry-text "30" --borders "8") || quit "Aborted!" 1
- [[ $input =~ $VALID_INPUT_PATTERN ]] || quit "Invalid input! Exiting!" 1
- [ "$input" -lt 960 ] || quit "Maximum value of 959 is exceeded! Exiting!" 1
- remaining_time=$input
- while [ "$remaining_time" -gt 0 ]; do
- if value_is_triangular "$remaining_time"; then
- announce "$remaining_time" &
- fi
- if [ "$remaining_time" -lt 100 ]; then
- display_minutes "$remaining_time"
- else
- display_hours_minutes "$remaining_time"
- fi
- remaining_time=$((remaining_time - 1))
- done
- yad --notification --image "emblem-urgent" &
- quit "Time's up!" 0
Advertisement
Add Comment
Please, Sign In to add comment