simplify notifications

This commit is contained in:
Kevin Schoon 2018-01-31 07:43:41 -05:00
parent 51aaa9c19f
commit 7bffb5fe7e
3 changed files with 7 additions and 114 deletions

View File

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

15
main.go
View File

@ -3,25 +3,12 @@ package main
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"runtime"
"sort" "sort"
"time" "time"
"github.com/jawher/mow.cli" "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) { func start(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" cmd.Spec = "[OPTIONS] MESSAGE"
@ -43,7 +30,7 @@ func start(path *string) func(*cli.Cmd) {
NPomodoros: *pomodoros, NPomodoros: *pomodoros,
Duration: parsed, Duration: parsed,
} }
runner, err := NewTaskRunner(task, db, notifier(*path+"/icon.png")) runner, err := NewTaskRunner(task, db, NewXnotifier(*path+"/icon.png"))
maybe(err) maybe(err)
runner.Start() runner.Start()
startUI(runner) startUI(runner)

View File

@ -9,8 +9,6 @@ import (
"github.com/0xAX/notificator" "github.com/0xAX/notificator"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/kevinschoon/pomo/libnotify"
) )
type State int type State int
@ -142,8 +140,7 @@ func (p Pomodoro) Duration() time.Duration {
return (p.End.Sub(p.Start)) return (p.End.Sub(p.Start))
} }
// Notifier implements a system specific // Notifier sends a system notification
// notification. On Linux this libnotify.
type Notifier interface { type Notifier interface {
Notify(string, string) error Notify(string, string) error
} }
@ -153,45 +150,13 @@ type NoopNotifier struct{}
func (n NoopNotifier) Notify(string, string) error { return nil } func (n NoopNotifier) Notify(string, string) error { return nil }
// LibNotifier implements a Linux // Xnotifier can push notifications to mac, linux and windows.
// notifier client. type Xnotifier struct {
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 {
*notificator.Notificator *notificator.Notificator
iconPath string iconPath string
} }
func newXnotifier(iconPath string) xnotifier { func NewXnotifier(iconPath string) Notifier {
// Write the built-in tomato icon if it // Write the built-in tomato icon if it
// doesn't already exist. // doesn't already exist.
_, err := os.Stat(iconPath) _, err := os.Stat(iconPath)
@ -199,25 +164,13 @@ func newXnotifier(iconPath string) xnotifier {
raw := MustAsset("tomato-icon.png") raw := MustAsset("tomato-icon.png")
_ = ioutil.WriteFile(iconPath, raw, 0644) _ = ioutil.WriteFile(iconPath, raw, 0644)
} }
return xnotifier{ return Xnotifier{
Notificator: notificator.New(notificator.Options{}), Notificator: notificator.New(notificator.Options{}),
iconPath: iconPath, iconPath: iconPath,
} }
} }
// Notify sends a notification to the OS. // 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) 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)
}