From 7bffb5fe7eb6bb5e5d4d5e738e3be74d48fbcf73 Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Wed, 31 Jan 2018 07:43:41 -0500 Subject: [PATCH] simplify notifications --- libnotify/libnotify.go | 47 --------------------------------- main.go | 15 +---------- types.go | 59 +++++------------------------------------- 3 files changed, 7 insertions(+), 114 deletions(-) delete mode 100644 libnotify/libnotify.go diff --git a/libnotify/libnotify.go b/libnotify/libnotify.go deleted file mode 100644 index 60d91fd..0000000 --- a/libnotify/libnotify.go +++ /dev/null @@ -1,47 +0,0 @@ -/* -libnotify is a lightweight client for libnotify https://developer.gnome.org/notification-spec/. -For now this just shells out to "notify-send". -TODO: Move this into it's own repository as time permits. -*/ -package libnotify - -import ( - "fmt" - "os/exec" - "time" -) - -type Notification struct { - Urgency string - Expire time.Duration - Title string - Body string - Icon string -} - -type Client struct { - Path string -} - -func NewClient() *Client { - return &Client{ - Path: "/bin/notify-send", - } -} - -func (c Client) Notify(n Notification) error { - var args []string - if n.Urgency != "" { - args = append(args, fmt.Sprintf("--urgency=%s", n.Urgency)) - } - if n.Icon != "" { - args = append(args, fmt.Sprintf("--icon=%s", n.Icon)) - } - if n.Expire > 0 { - args = append(args, fmt.Sprintf("--expire=%s", n.Expire.Truncate(time.Millisecond))) - } - args = append(args, n.Title) - args = append(args, n.Body) - _, err := exec.Command(c.Path, args...).Output() - return err -} diff --git a/main.go b/main.go index 15d4bf3..a3fcce6 100644 --- a/main.go +++ b/main.go @@ -3,25 +3,12 @@ package main import ( "encoding/json" "os" - "runtime" "sort" "time" "github.com/jawher/mow.cli" ) -func notifier(iconPath string) Notifier { - switch runtime.GOOS { - case "linux": - return NewLibNotifier(iconPath) - case "darwin": - return NewDarwinNotifier(iconPath) - case "windows": - return NewWindowsNotifier(iconPath) - } - return NoopNotifier{} -} - func start(path *string) func(*cli.Cmd) { return func(cmd *cli.Cmd) { cmd.Spec = "[OPTIONS] MESSAGE" @@ -43,7 +30,7 @@ func start(path *string) func(*cli.Cmd) { NPomodoros: *pomodoros, Duration: parsed, } - runner, err := NewTaskRunner(task, db, notifier(*path+"/icon.png")) + runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png")) maybe(err) runner.Start() startUI(runner) diff --git a/types.go b/types.go index b4123f2..3088f8c 100644 --- a/types.go +++ b/types.go @@ -9,8 +9,6 @@ import ( "github.com/0xAX/notificator" "github.com/fatih/color" - - "github.com/kevinschoon/pomo/libnotify" ) type State int @@ -142,8 +140,7 @@ func (p Pomodoro) Duration() time.Duration { return (p.End.Sub(p.Start)) } -// Notifier implements a system specific -// notification. On Linux this libnotify. +// Notifier sends a system notification type Notifier interface { Notify(string, string) error } @@ -153,45 +150,13 @@ type NoopNotifier struct{} func (n NoopNotifier) Notify(string, string) error { return nil } -// LibNotifier implements a Linux -// notifier client. -type LibNotifier struct { - client *libnotify.Client - iconPath string -} - -func NewLibNotifier(iconPath string) Notifier { - ln := &LibNotifier{ - client: libnotify.NewClient(), - } - // Write the built-in tomato icon if it - // doesn't already exist. - _, err := os.Stat(iconPath) - if os.IsNotExist(err) { - raw := MustAsset("tomato-icon.png") - ioutil.WriteFile(iconPath, raw, 0644) - } - ln.iconPath = iconPath - return ln -} - -func (ln LibNotifier) Notify(title, body string) error { - return ln.client.Notify( - libnotify.Notification{ - Title: title, - Body: body, - Icon: ln.iconPath, - }, - ) -} - -// xnotifier can push notifications to mac, linux and windows. -type xnotifier struct { +// Xnotifier can push notifications to mac, linux and windows. +type Xnotifier struct { *notificator.Notificator iconPath string } -func newXnotifier(iconPath string) xnotifier { +func NewXnotifier(iconPath string) Notifier { // Write the built-in tomato icon if it // doesn't already exist. _, err := os.Stat(iconPath) @@ -199,25 +164,13 @@ func newXnotifier(iconPath string) xnotifier { raw := MustAsset("tomato-icon.png") _ = ioutil.WriteFile(iconPath, raw, 0644) } - return xnotifier{ + return Xnotifier{ Notificator: notificator.New(notificator.Options{}), iconPath: iconPath, } } // Notify sends a notification to the OS. -func (n xnotifier) Notify(title, body string) error { +func (n Xnotifier) Notify(title, body string) error { return n.Push(title, body, n.iconPath, notificator.UR_NORMAL) } - -type DarwinNotifier = xnotifier - -func NewDarwinNotifier(iconPath string) DarwinNotifier { - return newXnotifier(iconPath) -} - -type WindowsNotifier = xnotifier - -func NewWindowsNotifier(iconPath string) WindowsNotifier { - return newXnotifier(iconPath) -}