2024-10-07 11:00:57 +02:00
|
|
|
#!/usr/bin/env dash
|
2024-10-07 10:44:20 +02:00
|
|
|
|
|
|
|
WORK_TIME=1200
|
|
|
|
PAUSE_TIME=300
|
|
|
|
|
|
|
|
timer_state_pid=0
|
|
|
|
timer_state_state="inactive"
|
|
|
|
timer_state_time="00:00"
|
2024-10-07 21:57:33 +02:00
|
|
|
timer_state_progress=100000
|
2024-10-07 10:44:20 +02:00
|
|
|
|
|
|
|
sub_timer_pid=0
|
|
|
|
|
|
|
|
start_timer() {
|
|
|
|
timer_type=$1
|
|
|
|
duration=0
|
|
|
|
|
|
|
|
timer_state_state="$timer_type" # Update done later
|
|
|
|
|
|
|
|
if [ "$timer_type" = "work" ]; then
|
|
|
|
duration=$WORK_TIME
|
|
|
|
timer_type="pause"
|
|
|
|
elif [ "$timer_type" = "pause" ]; then
|
|
|
|
duration=$PAUSE_TIME
|
|
|
|
timer_type="work"
|
|
|
|
else
|
|
|
|
echo "Invalid argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-10-07 11:00:57 +02:00
|
|
|
i=$duration
|
|
|
|
while [ "$i" -ge 0 ] ; do
|
2024-10-07 21:57:33 +02:00
|
|
|
timer_state_progress=$((i * 100000 / duration))
|
2024-10-07 10:44:20 +02:00
|
|
|
timer_state_time="$(date -u -d @$i +'%M:%S')"
|
|
|
|
update_timer_state
|
|
|
|
sleep 1
|
2024-10-07 11:00:57 +02:00
|
|
|
i=$(( i - 1 ))
|
2024-10-07 10:44:20 +02:00
|
|
|
done
|
|
|
|
|
2024-10-07 13:45:05 +02:00
|
|
|
# Notify user
|
|
|
|
if [ "$timer_type" = "pause" ]; then
|
|
|
|
notify-send -c grind-timer -i ~/Images/Icons/moai.jpg "Work period over" "You can take a little nap now."
|
|
|
|
elif [ "$timer_type" = "work" ]; then
|
|
|
|
notify-send -c grind-timer -i ~/Images/Icons/moai.jpg "Pause period over" "Back to grinding we go."
|
|
|
|
fi
|
|
|
|
|
2024-10-07 10:44:20 +02:00
|
|
|
start_timer "$timer_type"
|
|
|
|
}
|
|
|
|
|
|
|
|
update_timer_state() {
|
|
|
|
echo "{\"pid\": $timer_state_pid, \"state\": \"$timer_state_state\", \"time\": \"$timer_state_time\", \"progress\": $timer_state_progress}"
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_signal() {
|
|
|
|
# If a timer is running, kill it
|
|
|
|
if [ $sub_timer_pid -ne 0 ]; then
|
|
|
|
kill -9 $sub_timer_pid
|
|
|
|
sub_timer_pid=0
|
|
|
|
timer_state_state="inactive"
|
2024-10-08 15:33:32 +02:00
|
|
|
timer_state_progress=100000
|
2024-10-07 10:44:20 +02:00
|
|
|
update_timer_state
|
|
|
|
else
|
|
|
|
start_timer "work" &
|
|
|
|
sub_timer_pid=$!
|
|
|
|
# Will wait forever
|
|
|
|
wait $sub_timer_pid
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:00:57 +02:00
|
|
|
# Set up the SIGUSR1 signal handler
|
|
|
|
trap handle_signal 10
|
2024-10-07 10:44:20 +02:00
|
|
|
|
|
|
|
timer_state_pid=$$; update_timer_state
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
sleep infinity &
|
|
|
|
wait
|
|
|
|
done
|
|
|
|
|