simplify notifications
This commit is contained in:
parent
51aaa9c19f
commit
7bffb5fe7e
|
@ -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
15
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)
|
||||
|
|
59
types.go
59
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue