Author: Sam Boysel [sboysel@gmail.com]
Hash: 3ba07e9a873427e0c8f08559ca37273845534f8d
Timestamp: Tue, 31 May 2022 20:35:15 +0000 (2 years ago)

+17 -14 +/-2 browse
check if onEvent is set, parse argument array
1diff --git a/README.md b/README.md
2index f0e0a66..3a1f32b 100644
3--- a/README.md
4+++ b/README.md
5 @@ -74,7 +74,6 @@ The new state will be exported as an environment variable `POMO_STATE` for this
6 command. Possible state values are `RUNNING`, `PAUSED`, `BREAKING`, or
7 `COMPLETE`.
8
9-
10 For example, to trigger a terminal bell when a session completes, add the
11 following to `config.json`:
12 ```json
13 diff --git a/pkg/internal/runner.go b/pkg/internal/runner.go
14index 9a83fef..ea9fe17 100644
15--- a/pkg/internal/runner.go
16+++ b/pkg/internal/runner.go
17 @@ -78,15 +78,29 @@ func (t *TaskRunner) TimePauseDuration() time.Duration {
18
19 func (t *TaskRunner) SetState(state State) {
20 t.state = state
21+ // execute onEvent command if variable is set
22+ if t.onEvent != nil {
23+ t.runOnEvent()
24+ }
25 }
26
27 // execute script command specified by `onEvent` on state change
28- func (t *TaskRunner) OnEvent() error {
29- app, args := t.onEvent[0], t.onEvent[1:len(t.onEvent)]
30- cmd := exec.Command(app, args...)
31+ func (t *TaskRunner) runOnEvent() error {
32+ var cmd *exec.Cmd
33+ // parse command arguments
34+ numArgs := len(t.onEvent)
35+ app := t.onEvent[0]
36+ if numArgs > 1 {
37+ args := t.onEvent[1:(numArgs + 1)]
38+ cmd = exec.Command(app, args...)
39+ } else {
40+ cmd = exec.Command(app)
41+ }
42+ // set state in command environment
43 cmd.Env = append(os.Environ(),
44 fmt.Sprintf("POMO_STATE=%s", t.state),
45 )
46+ // run command
47 err := cmd.Run()
48 if err != nil {
49 return err
50 @@ -104,8 +118,6 @@ func (t *TaskRunner) run() error {
51 pomodoro.Start = time.Now()
52 // Set state to RUNNIN
53 t.SetState(RUNNING)
54- // Execute onEvent command
55- t.OnEvent()
56 // Create a new timer
57 timer := time.NewTimer(t.duration)
58 // Record our started time
59 @@ -125,8 +137,6 @@ func (t *TaskRunner) run() error {
60 remaining := t.TimeRemaining()
61 // Change state to PAUSED
62 t.SetState(PAUSED)
63- // Execute onEvent command
64- t.OnEvent()
65 // Wait for the user to press [p]
66 <-t.pause
67 // Resume the timer with previous
68 @@ -137,8 +147,6 @@ func (t *TaskRunner) run() error {
69 t.duration = remaining
70 // Restore state to RUNNING
71 t.SetState(RUNNING)
72- // Execute onEvent command
73- t.OnEvent()
74 goto loop
75 }
76 pomodoro.End = time.Now()
77 @@ -153,8 +161,6 @@ func (t *TaskRunner) run() error {
78 break
79 }
80 t.SetState(BREAKING)
81- // Execute onEvent command
82- t.OnEvent()
83 t.notifier.Notify("Pomo", "It is time to take a break!")
84 // Reset the duration incase it
85 // was paused.
86 @@ -165,8 +171,6 @@ func (t *TaskRunner) run() error {
87 }
88 t.notifier.Notify("Pomo", "Pomo session has completed!")
89 t.SetState(COMPLETE)
90- // Execute onEvent command
91- t.OnEvent()
92 return nil
93 }
94