|
|
|
@ -2,6 +2,9 @@ package pomo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
@ -21,6 +24,7 @@ type TaskRunner struct {
|
|
|
|
|
notifier Notifier
|
|
|
|
|
duration time.Duration
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
onEvent []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMockedTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
|
|
|
|
@ -55,6 +59,7 @@ func NewTaskRunner(task *Task, config *Config) (*TaskRunner, error) {
|
|
|
|
|
toggle: make(chan bool),
|
|
|
|
|
notifier: NewXnotifier(config.IconPath),
|
|
|
|
|
duration: task.Duration,
|
|
|
|
|
onEvent: config.OnEvent,
|
|
|
|
|
}
|
|
|
|
|
return tr, nil
|
|
|
|
|
}
|
|
|
|
@ -75,6 +80,20 @@ func (t *TaskRunner) SetState(state State) {
|
|
|
|
|
t.state = state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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...)
|
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
|
fmt.Sprintf("POMO_STATE=%s", t.state),
|
|
|
|
|
)
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *TaskRunner) run() error {
|
|
|
|
|
for t.count < t.nPomodoros {
|
|
|
|
|
// Create a new pomodoro where we
|
|
|
|
@ -85,6 +104,8 @@ 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
|
|
|
|
@ -104,6 +125,8 @@ 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
|
|
|
|
@ -114,6 +137,8 @@ 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()
|
|
|
|
@ -128,6 +153,8 @@ 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.
|
|
|
|
@ -138,6 +165,8 @@ func (t *TaskRunner) run() error {
|
|
|
|
|
}
|
|
|
|
|
t.notifier.Notify("Pomo", "Pomo session has completed!")
|
|
|
|
|
t.SetState(COMPLETE)
|
|
|
|
|
// Execute onEvent command
|
|
|
|
|
t.OnEvent()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|