check if onEvent is set, parse argument array

This commit is contained in:
Sam Boysel 2022-05-31 13:35:15 -07:00
parent caded9b68b
commit 3ba07e9a87
2 changed files with 17 additions and 14 deletions

View File

@ -74,7 +74,6 @@ The new state will be exported as an environment variable `POMO_STATE` for this
command. Possible state values are `RUNNING`, `PAUSED`, `BREAKING`, or command. Possible state values are `RUNNING`, `PAUSED`, `BREAKING`, or
`COMPLETE`. `COMPLETE`.
For example, to trigger a terminal bell when a session completes, add the For example, to trigger a terminal bell when a session completes, add the
following to `config.json`: following to `config.json`:
```json ```json

View File

@ -78,15 +78,29 @@ func (t *TaskRunner) TimePauseDuration() time.Duration {
func (t *TaskRunner) SetState(state State) { func (t *TaskRunner) SetState(state State) {
t.state = state t.state = state
// execute onEvent command if variable is set
if t.onEvent != nil {
t.runOnEvent()
}
} }
// execute script command specified by `onEvent` on state change // execute script command specified by `onEvent` on state change
func (t *TaskRunner) OnEvent() error { func (t *TaskRunner) runOnEvent() error {
app, args := t.onEvent[0], t.onEvent[1:len(t.onEvent)] var cmd *exec.Cmd
cmd := exec.Command(app, args...) // parse command arguments
numArgs := len(t.onEvent)
app := t.onEvent[0]
if numArgs > 1 {
args := t.onEvent[1:(numArgs + 1)]
cmd = exec.Command(app, args...)
} else {
cmd = exec.Command(app)
}
// set state in command environment
cmd.Env = append(os.Environ(), cmd.Env = append(os.Environ(),
fmt.Sprintf("POMO_STATE=%s", t.state), fmt.Sprintf("POMO_STATE=%s", t.state),
) )
// run command
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return err return err
@ -104,8 +118,6 @@ func (t *TaskRunner) run() error {
pomodoro.Start = time.Now() pomodoro.Start = time.Now()
// Set state to RUNNIN // Set state to RUNNIN
t.SetState(RUNNING) t.SetState(RUNNING)
// Execute onEvent command
t.OnEvent()
// Create a new timer // Create a new timer
timer := time.NewTimer(t.duration) timer := time.NewTimer(t.duration)
// Record our started time // Record our started time
@ -125,8 +137,6 @@ func (t *TaskRunner) run() error {
remaining := t.TimeRemaining() remaining := t.TimeRemaining()
// Change state to PAUSED // Change state to PAUSED
t.SetState(PAUSED) t.SetState(PAUSED)
// Execute onEvent command
t.OnEvent()
// Wait for the user to press [p] // Wait for the user to press [p]
<-t.pause <-t.pause
// Resume the timer with previous // Resume the timer with previous
@ -137,8 +147,6 @@ func (t *TaskRunner) run() error {
t.duration = remaining t.duration = remaining
// Restore state to RUNNING // Restore state to RUNNING
t.SetState(RUNNING) t.SetState(RUNNING)
// Execute onEvent command
t.OnEvent()
goto loop goto loop
} }
pomodoro.End = time.Now() pomodoro.End = time.Now()
@ -153,8 +161,6 @@ func (t *TaskRunner) run() error {
break break
} }
t.SetState(BREAKING) t.SetState(BREAKING)
// Execute onEvent command
t.OnEvent()
t.notifier.Notify("Pomo", "It is time to take a break!") t.notifier.Notify("Pomo", "It is time to take a break!")
// Reset the duration incase it // Reset the duration incase it
// was paused. // was paused.
@ -165,8 +171,6 @@ func (t *TaskRunner) run() error {
} }
t.notifier.Notify("Pomo", "Pomo session has completed!") t.notifier.Notify("Pomo", "Pomo session has completed!")
t.SetState(COMPLETE) t.SetState(COMPLETE)
// Execute onEvent command
t.OnEvent()
return nil return nil
} }