[+] Implemented cross platform notifications.

This commit is contained in:
Jack Mordaunt 2018-01-31 00:54:03 +01:00
parent e06d8dd4ec
commit 9c21245767
2 changed files with 38 additions and 4 deletions

10
main.go
View File

@ -2,16 +2,22 @@ package main
import (
"encoding/json"
"github.com/jawher/mow.cli"
"os"
"runtime"
"sort"
"time"
"github.com/jawher/mow.cli"
)
func notifier(iconPath string) Notifier {
if runtime.GOOS == "linux" {
switch runtime.GOOS {
case "linux":
return NewLibNotifier(iconPath)
case "darwin":
return NewAllNotifier(iconPath)
case "windows":
return NewAllNotifier(iconPath)
}
return NoopNotifier{}
}

View File

@ -3,11 +3,13 @@ package main
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os"
"time"
"github.com/0xAX/notificator"
"github.com/fatih/color"
"github.com/kevinschoon/pomo/libnotify"
)
@ -142,7 +144,6 @@ func (p Pomodoro) Duration() time.Duration {
// Notifier implements a system specific
// notification. On Linux this libnotify.
// TODO: OSX, Windows(?)
type Notifier interface {
Notify(string, string) error
}
@ -183,3 +184,30 @@ func (ln LibNotifier) Notify(title, body string) error {
},
)
}
// AllNotifier can push notifications to mac, linux and windows.
// Icon can be specified via file path.
type AllNotifier struct {
*notificator.Notificator
iconPath string
}
// NewAllNotifier constructs an AllNotifier object.
func NewAllNotifier(iconPath string) AllNotifier {
// 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)
}
return AllNotifier{
Notificator: notificator.New(notificator.Options{}),
iconPath: iconPath,
}
}
// Notify sends a notification to the OS.
func (n AllNotifier) Notify(title, body string) error {
return n.Push(title, body, n.iconPath, notificator.UR_NORMAL)
}