kevinschoon / pomo

Pomodoro CLI
https://kevinschoon.github.io/pomo
MIT License
528 stars 46 forks source link

Execute command on state change #61

Closed sboysel closed 2 years ago

sboysel commented 2 years ago

A more general solution that supersedes #60

sboysel commented 2 years ago

Ah. Need to check length of argument list in onEvent array

kevinschoon commented 2 years ago

This looks wonderful @sboysel and it's exactly what I had in mind! There is one tiny issue which is that if the script that is called blocks for any reason it will break the UI and cause it to become unresponsive. You can replicate this by changing your example script to something like sleep inf and observe that the UI is no longer responsive.

You can resolve this by simply running the script in a Go routine:

diff --git a/pkg/internal/runner.go b/pkg/internal/runner.go
index ea9fe17..46a802c 100644
--- a/pkg/internal/runner.go
+++ b/pkg/internal/runner.go
@@ -80,7 +80,7 @@ func (t *TaskRunner) SetState(state State) {
        t.state = state
        // execute onEvent command if variable is set
        if t.onEvent != nil {
-               t.runOnEvent()
+               go t.runOnEvent()
        }
 }

We can also consider adding a directory called contrib where we put community contributed scripts that extend the functionality of pomo. For example on my system I just added one called pomonag which produces a notification as part of the Sway window manager on my system.

#!/bin/sh

if [ "$POMO_STATE" == "BREAKING" ] ; then
    swaynag -m "It's time to take a break!"
fi

Just a suggestion, happy to approve this right away after fixing the Go routine issue. Thanks again so much, this is wonderful!

sboysel commented 2 years ago

Got it. Thanks for the tip! I'll make the change shortly.

I like the contrib directory idea. I can add this to the PR and include a sample script.