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
`COMPLETE`.
For example, to trigger a terminal bell when a session completes, add the
following to `config.json`:
```json

View File

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