From e3732717f528be61fcef20d13f9ce44e2bf34aad Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 4 Jul 2019 19:12:58 -0400 Subject: [PATCH] pass config for initialization, add marshaling for status --- main.go | 8 ++++---- runner.go | 24 ++++++++++++++++-------- server.go | 4 ++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index a7ebcf9..e8675d4 100644 --- a/main.go +++ b/main.go @@ -40,9 +40,9 @@ func start(config *Config) func(*cli.Cmd) { task.ID = id return nil })) - runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath)) + runner, err := NewTaskRunner(task, config) maybe(err) - server, err := NewServer(config.SocketPath, runner) + server, err := NewServer(runner, config) maybe(err) server.Start() defer server.Stop() @@ -110,9 +110,9 @@ func begin(config *Config) func(*cli.Cmd) { task.Pomodoros = []*Pomodoro{} return nil })) - runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath)) + runner, err := NewTaskRunner(task, config) maybe(err) - server, err := NewServer(config.SocketPath, runner) + server, err := NewServer(runner, config) maybe(err) server.Start() defer server.Stop() diff --git a/runner.go b/runner.go index 97869d6..f8cbadf 100644 --- a/runner.go +++ b/runner.go @@ -20,7 +20,11 @@ type TaskRunner struct { 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{ taskID: task.ID, taskMessage: task.Message, @@ -30,7 +34,7 @@ func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, er state: State(0), pause: make(chan bool), toggle: make(chan bool), - notifier: notifier, + notifier: NewXnotifier(config.IconPath), duration: task.Duration, } return tr, nil @@ -44,6 +48,10 @@ func (t *TaskRunner) TimeRemaining() time.Duration { return (t.duration - time.Since(t.started)).Truncate(time.Second) } +func (t *TaskRunner) SetState(state State) { + t.state = state +} + func (t *TaskRunner) run() error { for t.count < t.nPomodoros { // Create a new pomodoro where we @@ -52,8 +60,8 @@ func (t *TaskRunner) run() error { pomodoro := &Pomodoro{} // Start this pomodoro pomodoro.Start = time.Now() - // Set state to RUNNING - t.state = RUNNING + // Set state to RUNNIN + t.SetState(RUNNING) // Create a new timer timer := time.NewTimer(t.duration) // Record our started time @@ -61,7 +69,7 @@ func (t *TaskRunner) run() error { loop: select { case <-timer.C: - t.state = BREAKING + t.SetState(BREAKING) t.count++ case <-t.toggle: // Catch any toggles when we @@ -72,7 +80,7 @@ func (t *TaskRunner) run() error { // Record the remaining time of the current pomodoro remaining := t.TimeRemaining() // Change state to PAUSED - t.state = PAUSED + t.SetState(PAUSED) // Wait for the user to press [p] <-t.pause // Resume the timer with previous @@ -82,7 +90,7 @@ func (t *TaskRunner) run() error { t.started = time.Now() t.duration = remaining // Restore state to RUNNING - t.state = RUNNING + t.SetState(RUNNING) goto loop } pomodoro.End = time.Now() @@ -106,7 +114,7 @@ func (t *TaskRunner) run() error { } t.notifier.Notify("Pomo", "Pomo session has completed!") - t.state = COMPLETE + t.SetState(COMPLETE) return nil } diff --git a/server.go b/server.go index 2ae8767..c57ed82 100644 --- a/server.go +++ b/server.go @@ -38,8 +38,8 @@ func (s *Server) Stop() { s.listener.Close() } -func NewServer(path string, runner *TaskRunner) (*Server, error) { - listener, err := net.Listen("unix", path) +func NewServer(runner *TaskRunner, config *Config) (*Server, error) { + listener, err := net.Listen("unix", config.SocketPath) if err != nil { return nil, err }