From 70fa81c663f440ab369c52b044810725e152c23a Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sun, 28 Jan 2018 00:42:13 +0800 Subject: [PATCH] only use libnotify on Linux --- main.go | 11 +++++++++-- task.go | 6 ++++-- task_test.go | 2 +- types.go | 5 +++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 6078ef8..d52c830 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,18 @@ import ( "encoding/json" "github.com/jawher/mow.cli" "os" + "runtime" "sort" "time" ) +func notifier() Notifier { + if runtime.GOOS == "linux" { + return NewLibNotifier() + } + return NoopNotifier{} +} + func start(path *string) func(*cli.Cmd) { return func(cmd *cli.Cmd) { cmd.Spec = "[OPTIONS] MESSAGE" @@ -29,11 +37,10 @@ func start(path *string) func(*cli.Cmd) { NPomodoros: *pomodoros, Duration: parsed, } - runner, err := NewTaskRunner(task, db) + runner, err := NewTaskRunner(task, db, notifier()) maybe(err) runner.Start() startUI(runner) - //maybe(runner.Run()) } } } diff --git a/task.go b/task.go index d53fb71..0c94aba 100644 --- a/task.go +++ b/task.go @@ -19,7 +19,7 @@ type TaskRunner struct { duration time.Duration } -func NewTaskRunner(task *Task, store *Store) (*TaskRunner, error) { +func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) { taskID, err := store.CreateTask(*task) if err != nil { return nil, err @@ -33,7 +33,7 @@ func NewTaskRunner(task *Task, store *Store) (*TaskRunner, error) { state: State(0), pause: make(chan bool), toggle: make(chan bool), - notifier: NewLibNotifier(), + notifier: notifier, duration: task.Duration, } return tr, nil @@ -97,6 +97,8 @@ func (t *TaskRunner) run() error { if t.count == t.nPomodoros { break } + + t.notifier.Notify("Pomo", "It is time to take a break!") // Reset the duration incase it // was paused. t.duration = t.origDuration diff --git a/task_test.go b/task_test.go index 07765d9..8c35da7 100644 --- a/task_test.go +++ b/task_test.go @@ -21,7 +21,7 @@ func TestTaskRunner(t *testing.T) { Duration: time.Second * 2, NPomodoros: 2, Message: fmt.Sprint("Test Task"), - }, store) + }, store, NoopNotifier{}) if err != nil { t.Error(err) } diff --git a/types.go b/types.go index e6d04b3..66fb76c 100644 --- a/types.go +++ b/types.go @@ -139,6 +139,11 @@ type Notifier interface { Notify(string, string) error } +// NoopNotifier does nothing +type NoopNotifier struct{} + +func (n NoopNotifier) Notify(string, string) error { return nil } + // LibNotifier implements a Linux // notifier client. type LibNotifier struct {