From 106174c833e799ac89261a56d3a44f2dbb145353 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 12 Apr 2017 11:28:08 +0200 Subject: Runtime OS switcher for notifications --- main.go | 29 +++++++++++++++++++++++++++++ notify_osx.go | 13 ------------- notify_x11.go | 9 --------- 3 files changed, 29 insertions(+), 22 deletions(-) delete mode 100644 notify_osx.go delete mode 100644 notify_x11.go diff --git a/main.go b/main.go index a03a67d..592f770 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,9 @@ import ( "flag" "fmt" "os" + "os/exec" "os/signal" + "runtime" "strings" "time" ) @@ -128,3 +130,30 @@ func main() { for s := o.doWork; s != nil; s = s(ctx) { } } + +const title = "Pomodoro timer" + +func notifyX11(s string) error { + msg := fmt.Sprintf("%s\n\n%s", title, s) + return exec.Command("xmessage", "-center", "-timeout", "5", msg).Run() +} + +func notifyOSX(s string) error { + msg := fmt.Sprintf("display notification %q with title %q", s, title) + return exec.Command("osascript", "-e", msg).Run() +} + +func notifyLnx(s string) error { + return exec.Command("notify-send", title, s).Run() +} + +func notify(s string) error { + switch runtime.GOOS { + case "darwin": + return notifyOSX(s) + case "linux": + return notifyLnx(s) + default: // *BSD + return notifyX11(s) + } +} diff --git a/notify_osx.go b/notify_osx.go deleted file mode 100644 index 2e427e9..0000000 --- a/notify_osx.go +++ /dev/null @@ -1,13 +0,0 @@ -// +build darwin - -package main - -import ( - "fmt" - "os/exec" -) - -func notify(s string) error { - cmd := fmt.Sprintf("display notification %q with title %q", s, "Pomodoro") - return exec.Command("osascript", "-e", cmd).Run() -} diff --git a/notify_x11.go b/notify_x11.go deleted file mode 100644 index f698be1..0000000 --- a/notify_x11.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build !darwin - -package main - -import "os/exec" - -func notify(s string) error { - return exec.Command("xmessage", "-center", "-timeout", "5", s).Run() -} -- cgit v1.2.3