From c2c09ddd32d87b2134d3850a188f6a2ea67272d8 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 26 Jan 2020 16:04:49 +0000 Subject: [PATCH] Notifications (macos). --- README.md | 2 +- color.go | 2 +- color_darwin.go | 2 +- color_unix.go | 2 +- color_windows.go | 2 +- file.go | 6 +++--- file_darwin.go | 6 +++--- file_unix.go | 6 +++--- file_windows.go | 6 +++--- internal/zenutil/osa_generated.go | 12 ++++++++++++ internal/zenutil/osascripts/notify.js | 14 ++++++++++++++ internal/zenutil/run_darwin.go | 6 ++++++ notify.go | 6 ++++++ notify_darwin.go | 24 ++++++++++++++++++++++++ notify_test.go | 10 ++++++++++ notify_unix.go | 7 +++++++ notify_windows.go | 5 +++++ zenity.go | 2 +- 18 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 internal/zenutil/osascripts/notify.js create mode 100644 notify.go create mode 100644 notify_darwin.go create mode 100644 notify_test.go create mode 100644 notify_unix.go create mode 100644 notify_windows.go diff --git a/README.md b/README.md index b5bdc6d..9a6d7df 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![GoDoc](https://godoc.org/github.com/ncruces/zenity?status.svg)](https://godoc.org/github.com/ncruces/zenity) This repo includes both a cross-platform Go package providing -[Zenity](https://help.gnome.org/users/zenity/)-like dialogs +[Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs (simple dialogs that interact graphically with the user), as well as a *“port”* of the `zenity` command to both Windows and macOS based on that library. diff --git a/color.go b/color.go index 5b40397..6efb616 100644 --- a/color.go +++ b/color.go @@ -8,7 +8,7 @@ import "image/color" // // Valid options: Title, Color, ShowPalette. func SelectColor(options ...Option) (color.Color, error) { - return selectColor(options...) + return selectColor(options) } // Color returns an Option to set the color. diff --git a/color_darwin.go b/color_darwin.go index 64fd225..9ae3273 100644 --- a/color_darwin.go +++ b/color_darwin.go @@ -7,7 +7,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectColor(options ...Option) (color.Color, error) { +func selectColor(options []Option) (color.Color, error) { opts := applyOptions(options) var data zenutil.Color diff --git a/color_unix.go b/color_unix.go index 6354b4f..8ca9ed9 100644 --- a/color_unix.go +++ b/color_unix.go @@ -9,7 +9,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectColor(options ...Option) (color.Color, error) { +func selectColor(options []Option) (color.Color, error) { opts := applyOptions(options) args := []string{"--color-selection"} diff --git a/color_windows.go b/color_windows.go index e24336a..43d454b 100644 --- a/color_windows.go +++ b/color_windows.go @@ -20,7 +20,7 @@ func init() { } } -func selectColor(options ...Option) (color.Color, error) { +func selectColor(options []Option) (color.Color, error) { opts := applyOptions(options) // load custom colors diff --git a/file.go b/file.go index a03a9b1..e7739e5 100644 --- a/file.go +++ b/file.go @@ -11,7 +11,7 @@ import ( // // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s). func SelectFile(options ...Option) (string, error) { - return selectFile(options...) + return selectFile(options) } // SelectFileMutiple displays the multiple file selection dialog. @@ -20,7 +20,7 @@ func SelectFile(options ...Option) (string, error) { // // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s). func SelectFileMutiple(options ...Option) ([]string, error) { - return selectFileMutiple(options...) + return selectFileMutiple(options) } // SelectFileSave displays the save file selection dialog. @@ -30,7 +30,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { // Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden, // FileFilter(s). func SelectFileSave(options ...Option) (string, error) { - return selectFileSave(options...) + return selectFileSave(options) } // Filename returns an Option to set the filename. diff --git a/file_darwin.go b/file_darwin.go index fb774ff..8ae43d9 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -7,7 +7,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectFile(options ...Option) (string, error) { +func selectFile(options []Option) (string, error) { opts := applyOptions(options) data := zenutil.File{ @@ -35,7 +35,7 @@ func selectFile(options ...Option) (string, error) { return string(out), nil } -func selectFileMutiple(options ...Option) ([]string, error) { +func selectFileMutiple(options []Option) ([]string, error) { opts := applyOptions(options) data := zenutil.File{ @@ -68,7 +68,7 @@ func selectFileMutiple(options ...Option) ([]string, error) { return strings.Split(string(out), zenutil.Separator), nil } -func selectFileSave(options ...Option) (string, error) { +func selectFileSave(options []Option) (string, error) { opts := applyOptions(options) data := zenutil.File{ diff --git a/file_unix.go b/file_unix.go index 4556337..a52c607 100644 --- a/file_unix.go +++ b/file_unix.go @@ -9,7 +9,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectFile(options ...Option) (string, error) { +func selectFile(options []Option) (string, error) { opts := applyOptions(options) args := []string{"--file-selection"} @@ -37,7 +37,7 @@ func selectFile(options ...Option) (string, error) { return string(out), nil } -func selectFileMutiple(options ...Option) ([]string, error) { +func selectFileMutiple(options []Option) ([]string, error) { opts := applyOptions(options) args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} @@ -65,7 +65,7 @@ func selectFileMutiple(options ...Option) ([]string, error) { return strings.Split(string(out), zenutil.Separator), nil } -func selectFileSave(options ...Option) (string, error) { +func selectFileSave(options []Option) (string, error) { opts := applyOptions(options) args := []string{"--file-selection", "--save"} diff --git a/file_windows.go b/file_windows.go index d91c4df..0660ee8 100644 --- a/file_windows.go +++ b/file_windows.go @@ -17,7 +17,7 @@ var ( shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName") ) -func selectFile(options ...Option) (string, error) { +func selectFile(options []Option) (string, error) { opts := applyOptions(options) if opts.directory { res, _, err := pickFolders(opts, false) @@ -53,7 +53,7 @@ func selectFile(options ...Option) (string, error) { return syscall.UTF16ToString(res[:]), nil } -func selectFileMutiple(options ...Option) ([]string, error) { +func selectFileMutiple(options []Option) ([]string, error) { opts := applyOptions(options) if opts.directory { _, res, err := pickFolders(opts, true) @@ -114,7 +114,7 @@ func selectFileMutiple(options ...Option) ([]string, error) { return split, nil } -func selectFileSave(options ...Option) (string, error) { +func selectFileSave(options []Option) (string, error) { opts := applyOptions(options) if opts.directory { res, _, err := pickFolders(opts, false) diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index 22368fe..95e6365 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -76,4 +76,16 @@ opts.cancelButton = {{json .Cancel}} {{end -}} var res = app[{{json .Operation}}]({{json .Text}}, opts).buttonReturned res === {{json .Extra}} ? res : void 0 +{{- end}} +{{define "notify" -}}var app = Application.currentApplication() +app.includeStandardAdditions = true +app.activate() +var opts = {} +{{if .Title -}} +opts.withTitle = {{json .Title}} +{{end -}} +{{if .Subtitle -}} +opts.subtitle = {{json .Subtitle}} +{{end -}} +void app.displayNotification({{json .Text}}, opts) {{- end}}`)) diff --git a/internal/zenutil/osascripts/notify.js b/internal/zenutil/osascripts/notify.js new file mode 100644 index 0000000..8018ab8 --- /dev/null +++ b/internal/zenutil/osascripts/notify.js @@ -0,0 +1,14 @@ +var app = Application.currentApplication() +app.includeStandardAdditions = true +app.activate() + +var opts = {} + +{{if .Title -}} + opts.withTitle = {{json .Title}} +{{end -}} +{{if .Subtitle -}} + opts.subtitle = {{json .Subtitle}} +{{end -}} + +void app.displayNotification({{json .Text}}, opts) \ No newline at end of file diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index e5fe7ac..88564e5 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -61,3 +61,9 @@ type Msg struct { Cancel int Default int } + +type Notify struct { + Text string + Title string + Subtitle string +} diff --git a/notify.go b/notify.go new file mode 100644 index 0000000..031f4b3 --- /dev/null +++ b/notify.go @@ -0,0 +1,6 @@ +package zenity + +// Notify displays a notification. +func Notify(text string, options ...Option) error { + return notify(text, options) +} diff --git a/notify_darwin.go b/notify_darwin.go new file mode 100644 index 0000000..adf52d3 --- /dev/null +++ b/notify_darwin.go @@ -0,0 +1,24 @@ +package zenity + +import ( + "strings" + + "github.com/ncruces/zenity/internal/zenutil" +) + +func notify(text string, options []Option) error { + opts := applyOptions(options) + data := zenutil.Notify{ + Text: text, + Title: opts.title, + } + if i := strings.IndexByte(text, '\n'); i >= 0 && i < len(text) { + data.Subtitle = text[:i] + data.Text = text[i+1:] + } + _, err := zenutil.Run("notify", data) + if err != nil { + return err + } + return nil +} diff --git a/notify_test.go b/notify_test.go new file mode 100644 index 0000000..64a3478 --- /dev/null +++ b/notify_test.go @@ -0,0 +1,10 @@ +package zenity_test + +import "github.com/ncruces/zenity" + +func ExampleNotify() { + zenity.Notify("An error has occurred.", + zenity.Title("Error"), + zenity.Icon(zenity.ErrorIcon)) + // Output: +} diff --git a/notify_unix.go b/notify_unix.go new file mode 100644 index 0000000..cf73b97 --- /dev/null +++ b/notify_unix.go @@ -0,0 +1,7 @@ +// +build !windows,!darwin + +package zenity + +func notify(text string, options []Option) error { + panic("not implemented") +} diff --git a/notify_windows.go b/notify_windows.go new file mode 100644 index 0000000..d89ae3f --- /dev/null +++ b/notify_windows.go @@ -0,0 +1,5 @@ +package zenity + +func notify(text string, options []Option) error { + panic("not implemented") +} diff --git a/zenity.go b/zenity.go index c1c3245..e44c82d 100644 --- a/zenity.go +++ b/zenity.go @@ -4,7 +4,7 @@ // It is inspired by, and closely follows the API of, the zenity program, which // it uses to provide the functionality on various Unixes. See: // -// https://help.gnome.org/users/zenity/ +// https://help.gnome.org/users/zenity/stable/ // // This package does not require cgo, and it does not impose any threading or // initialization requirements.