From 9c21245767ffa54d5d434ac8f047d3a705f834e7 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 31 Jan 2018 00:54:03 +0100 Subject: [PATCH 1/3] [+] Implemented cross platform notifications. --- main.go | 10 ++++++++-- types.go | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index b3d7597..d0476fe 100644 --- a/main.go +++ b/main.go @@ -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{} } diff --git a/types.go b/types.go index 5293e5f..0dbc1e0 100644 --- a/types.go +++ b/types.go @@ -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) +} From 18d97db12ab5600ef570c6985d041daa302e50e9 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 31 Jan 2018 01:05:40 +0100 Subject: [PATCH 2/3] [*] Added wrappers around type notificator to indicate which OS are supported by it. --- main.go | 4 ++-- types.go | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index d0476fe..15d4bf3 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,9 @@ func notifier(iconPath string) Notifier { case "linux": return NewLibNotifier(iconPath) case "darwin": - return NewAllNotifier(iconPath) + return NewDarwinNotifier(iconPath) case "windows": - return NewAllNotifier(iconPath) + return NewWindowsNotifier(iconPath) } return NoopNotifier{} } diff --git a/types.go b/types.go index 0dbc1e0..50d85e9 100644 --- a/types.go +++ b/types.go @@ -7,7 +7,6 @@ import ( "os" "time" - "github.com/0xAX/notificator" "github.com/fatih/color" "github.com/kevinschoon/pomo/libnotify" @@ -185,15 +184,13 @@ 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 can push notifications to mac, linux and windows. +type notificator struct { *notificator.Notificator iconPath string } -// NewAllNotifier constructs an AllNotifier object. -func NewAllNotifier(iconPath string) AllNotifier { +func newNotificator(iconPath string) notificator { // Write the built-in tomato icon if it // doesn't already exist. _, err := os.Stat(iconPath) @@ -201,13 +198,25 @@ func NewAllNotifier(iconPath string) AllNotifier { raw := MustAsset("tomato-icon.png") _ = ioutil.WriteFile(iconPath, raw, 0644) } - return AllNotifier{ + return notificator{ Notificator: notificator.New(notificator.Options{}), iconPath: iconPath, } } // Notify sends a notification to the OS. -func (n AllNotifier) Notify(title, body string) error { +func (n notificator) Notify(title, body string) error { return n.Push(title, body, n.iconPath, notificator.UR_NORMAL) } + +type DarwinNotifier = notificator + +func NewDarwinNotifier(iconPath string) DarwinNotifier { + return newNotificator(iconPath) +} + +type WindowsNotifier = notificator + +func NewWindowsNotifier(iconPath string) WindowsNotifier { + return newNotificator(iconPath) +} From 99667b95aee2cfa41870e48e6446c6d553695827 Mon Sep 17 00:00:00 2001 From: Jack Mordaunt Date: Wed, 31 Jan 2018 01:10:03 +0100 Subject: [PATCH 3/3] [*] Renamed implementation type due to naming conflict with package. --- types.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/types.go b/types.go index 50d85e9..b4123f2 100644 --- a/types.go +++ b/types.go @@ -7,6 +7,7 @@ import ( "os" "time" + "github.com/0xAX/notificator" "github.com/fatih/color" "github.com/kevinschoon/pomo/libnotify" @@ -184,13 +185,13 @@ func (ln LibNotifier) Notify(title, body string) error { ) } -// notificator can push notifications to mac, linux and windows. -type notificator struct { +// xnotifier can push notifications to mac, linux and windows. +type xnotifier struct { *notificator.Notificator iconPath string } -func newNotificator(iconPath string) notificator { +func newXnotifier(iconPath string) xnotifier { // Write the built-in tomato icon if it // doesn't already exist. _, err := os.Stat(iconPath) @@ -198,25 +199,25 @@ func newNotificator(iconPath string) notificator { raw := MustAsset("tomato-icon.png") _ = ioutil.WriteFile(iconPath, raw, 0644) } - return notificator{ + return xnotifier{ Notificator: notificator.New(notificator.Options{}), iconPath: iconPath, } } // Notify sends a notification to the OS. -func (n notificator) 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 = notificator +type DarwinNotifier = xnotifier func NewDarwinNotifier(iconPath string) DarwinNotifier { - return newNotificator(iconPath) + return newXnotifier(iconPath) } -type WindowsNotifier = notificator +type WindowsNotifier = xnotifier func NewWindowsNotifier(iconPath string) WindowsNotifier { - return newNotificator(iconPath) + return newXnotifier(iconPath) }