only use libnotify on Linux

This commit is contained in:
Kevin Schoon 2018-01-28 00:42:13 +08:00
parent 3389d46410
commit 70fa81c663
4 changed files with 19 additions and 5 deletions

11
main.go
View File

@ -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())
}
}
}

View File

@ -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

View File

@ -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)
}

View File

@ -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 {