Compare commits

..

2 commits

Author SHA1 Message Date
GaspardCulis
63d7ad6356 perf(eww -> timer): Now using dash interpreter 2024-10-07 11:00:57 +02:00
GaspardCulis
7daa31ce3a refactor(eww -> timer): Now using bash script for timer logic 2024-10-07 10:44:20 +02:00
2 changed files with 78 additions and 46 deletions

71
bar/eww/scripts/timer Executable file
View file

@ -0,0 +1,71 @@
#!/usr/bin/env dash
WORK_TIME=1200
PAUSE_TIME=300
timer_state_pid=0
timer_state_state="inactive"
timer_state_time="00:00"
timer_state_progress=100
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
i=$duration
while [ "$i" -ge 0 ] ; do
timer_state_progress=$((i * 100 / duration))
timer_state_time="$(date -u -d @$i +'%M:%S')"
update_timer_state
sleep 1
i=$(( i - 1 ))
done
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"
timer_state_progress=100
update_timer_state
else
start_timer "work" &
sub_timer_pid=$!
# Will wait forever
wait $sub_timer_pid
fi
}
# Set up the SIGUSR1 signal handler
trap handle_signal 10
timer_state_pid=$$; update_timer_state
while true; do
sleep infinity &
wait
done

View file

@ -1,57 +1,18 @@
(defvar WORK_TIME_SEC 1200)
(defvar PAUSE_TIME_SEC 300)
(defvar timer-state "inactive")
(defvar timer-start-time 0)
(defpoll timer-time
:interval "900ms"
:run-while {timer-state != "inactive"}
`date +%s`)
(defpoll timer-work-period
:interval "20m"
:initial "first"
:run-while {timer-state == "work"}
`if [ "$(eww get timer-work-period)" == "first" ]; then
eww update timer-start-time=$(date +%s)
echo next
else
notify-send -c grind-timer -i ~/Images/Icons/moai.jpg "Work period over" "You can take a little nap now."
eww update timer-state=pause timer-pause-period=first
echo final
fi`) ; TODO: Put in a separate script
(defpoll timer-pause-period
:interval "5m"
:initial "first"
:run-while {timer-state == "pause"}
`if [ "$(eww get timer-pause-period)" == "first" ]; then
eww update timer-start-time=$(date +%s)
echo next
else
notify-send -c grind-timer -i ~/Images/Icons/moai.jpg "Pause period over" "Back to grinding we go."
eww update timer-state=work timer-work-period=first
echo final
fi`) ; TODO: Put in a separate script
(deflisten timer_state :initial '{"pid": 0, "state": "inactive", "time": "00:00", "progress": 100}' "~/.config/eww/scripts/timer")
(defwidget start-button []
(button
:class "start-button"
:vexpand true
:onclick "eww update timer-state=${timer-state == "inactive" ? "work" : "inactive"} timer-work-period=first timer-pause-period=first"
:onclick "kill -10 ${timer_state.pid}"
(overlay
(circular-progress
:class "circle-progress"
:value "${
timer-state == "inactive" ? 100 :
100 * (timer-start-time + (timer-state == "work" ? WORK_TIME_SEC : PAUSE_TIME_SEC) - timer-time)
/ (timer-state == "work" ? WORK_TIME_SEC : PAUSE_TIME_SEC)
}"
:value "${timer_state.progress}"
:thickness 6
:start-at 75
:clockwise true)
(label :class "start-icon" :text "${timer-state == "inactive" ? 'Start' : (timer-state == "work" ? "Working" : "Pause")}")
(label :class "start-icon" :text "${timer_state.state == "inactive" ? 'Start' : (timer_state.state == "work" ? "Working" : "Pause")}")
)
)
)
@ -60,13 +21,13 @@
(box
:orientation "v"
:space-evenly false
:class "timer ${timer-state}"
:class "timer ${timer_state.state}"
(label :class "timer-title" :text "Grind timer")
(start-button :valign "center" :vexpand true)
(revealer
:transition "slideup"
:reveal {timer-state != "inactive"}
(label :class "timer-text" :text "${timer-state != "inactive" ? formattime(timer-start-time - (3600 - (timer-state == "work" ? WORK_TIME_SEC : PAUSE_TIME_SEC)) - timer-time, "%H:%M:%S") : "00:20:00"}")
:reveal {timer_state.state != "inactive"}
(label :class "timer-text" :text "${timer_state.time}")
)
)
)