pass config for initialization, add marshaling for status

This commit is contained in:
Kevin 2019-07-04 19:12:58 -04:00
parent 00ea73813a
commit e3732717f5
3 changed files with 22 additions and 14 deletions

View File

@ -40,9 +40,9 @@ func start(config *Config) func(*cli.Cmd) {
task.ID = id task.ID = id
return nil return nil
})) }))
runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath)) runner, err := NewTaskRunner(task, config)
maybe(err) maybe(err)
server, err := NewServer(config.SocketPath, runner) server, err := NewServer(runner, config)
maybe(err) maybe(err)
server.Start() server.Start()
defer server.Stop() defer server.Stop()
@ -110,9 +110,9 @@ func begin(config *Config) func(*cli.Cmd) {
task.Pomodoros = []*Pomodoro{} task.Pomodoros = []*Pomodoro{}
return nil return nil
})) }))
runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath)) runner, err := NewTaskRunner(task, config)
maybe(err) maybe(err)
server, err := NewServer(config.SocketPath, runner) server, err := NewServer(runner, config)
maybe(err) maybe(err)
server.Start() server.Start()
defer server.Stop() defer server.Stop()

View File

@ -20,7 +20,11 @@ type TaskRunner struct {
duration time.Duration duration time.Duration
} }
func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) { func NewTaskRunner(task *Task, config *Config) (*TaskRunner, error) {
store, err := NewStore(config.DBPath)
if err != nil {
return nil, err
}
tr := &TaskRunner{ tr := &TaskRunner{
taskID: task.ID, taskID: task.ID,
taskMessage: task.Message, taskMessage: task.Message,
@ -30,7 +34,7 @@ func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, er
state: State(0), state: State(0),
pause: make(chan bool), pause: make(chan bool),
toggle: make(chan bool), toggle: make(chan bool),
notifier: notifier, notifier: NewXnotifier(config.IconPath),
duration: task.Duration, duration: task.Duration,
} }
return tr, nil return tr, nil
@ -44,6 +48,10 @@ func (t *TaskRunner) TimeRemaining() time.Duration {
return (t.duration - time.Since(t.started)).Truncate(time.Second) return (t.duration - time.Since(t.started)).Truncate(time.Second)
} }
func (t *TaskRunner) SetState(state State) {
t.state = state
}
func (t *TaskRunner) run() error { func (t *TaskRunner) run() error {
for t.count < t.nPomodoros { for t.count < t.nPomodoros {
// Create a new pomodoro where we // Create a new pomodoro where we
@ -52,8 +60,8 @@ func (t *TaskRunner) run() error {
pomodoro := &Pomodoro{} pomodoro := &Pomodoro{}
// Start this pomodoro // Start this pomodoro
pomodoro.Start = time.Now() pomodoro.Start = time.Now()
// Set state to RUNNING // Set state to RUNNIN
t.state = RUNNING t.SetState(RUNNING)
// 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
@ -61,7 +69,7 @@ func (t *TaskRunner) run() error {
loop: loop:
select { select {
case <-timer.C: case <-timer.C:
t.state = BREAKING t.SetState(BREAKING)
t.count++ t.count++
case <-t.toggle: case <-t.toggle:
// Catch any toggles when we // Catch any toggles when we
@ -72,7 +80,7 @@ func (t *TaskRunner) run() error {
// Record the remaining time of the current pomodoro // Record the remaining time of the current pomodoro
remaining := t.TimeRemaining() remaining := t.TimeRemaining()
// Change state to PAUSED // Change state to PAUSED
t.state = PAUSED t.SetState(PAUSED)
// 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
@ -82,7 +90,7 @@ func (t *TaskRunner) run() error {
t.started = time.Now() t.started = time.Now()
t.duration = remaining t.duration = remaining
// Restore state to RUNNING // Restore state to RUNNING
t.state = RUNNING t.SetState(RUNNING)
goto loop goto loop
} }
pomodoro.End = time.Now() pomodoro.End = time.Now()
@ -106,7 +114,7 @@ func (t *TaskRunner) run() error {
} }
t.notifier.Notify("Pomo", "Pomo session has completed!") t.notifier.Notify("Pomo", "Pomo session has completed!")
t.state = COMPLETE t.SetState(COMPLETE)
return nil return nil
} }

View File

@ -38,8 +38,8 @@ func (s *Server) Stop() {
s.listener.Close() s.listener.Close()
} }
func NewServer(path string, runner *TaskRunner) (*Server, error) { func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
listener, err := net.Listen("unix", path) listener, err := net.Listen("unix", config.SocketPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }