From 3d8fdb534e6240c32e2738be52ba5d72e342531c Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 27 Jan 2020 15:42:43 +0000 Subject: [PATCH] Notifications. --- README.md | 1 + cmd/zenity/.gitignore | 3 ++- cmd/zenity/main.go | 46 ++++++++++++++++++++++++++++++------------- msg.go | 16 --------------- notify.go | 2 ++ zenity.go | 18 ++++++++++++++++- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 9a6d7df..52cef10 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ For now, these are the only implemented dialogs: * [message](https://github.com/ncruces/zenity/wiki/Message-dialog) (error, info, question, warning) * [file selection](https://github.com/ncruces/zenity/wiki/File-Selection-dialog) * [color selection](https://github.com/ncruces/zenity/wiki/Color-Selection-dialog) +* [notification](https://github.com/ncruces/zenity/wiki/Notification) Behavior on Windows, macOS and other Unixes might differ slightly. Some of that is intended (reflecting platform differences), diff --git a/cmd/zenity/.gitignore b/cmd/zenity/.gitignore index afc690a..dc9ab05 100644 --- a/cmd/zenity/.gitignore +++ b/cmd/zenity/.gitignore @@ -1,2 +1,3 @@ zenity -*.syso \ No newline at end of file +*.syso +*.zip \ No newline at end of file diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index a0290b8..2dc04d3 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -17,6 +17,7 @@ import ( var ( // Application Options + notification bool errorDlg bool infoDlg bool warningDlg bool @@ -29,7 +30,7 @@ var ( // Message options text string - iconName string + icon string okLabel string cancelLabel string extraButton string @@ -65,6 +66,9 @@ func main() { zenutil.Command = true switch { + case notification: + errResult(zenity.Notify(text, opts...)) + case errorDlg: msgResult(zenity.Error(text, opts...)) case infoDlg: @@ -81,11 +85,11 @@ func main() { case save: strResult(egestPath(zenity.SelectFileSave(opts...))) case multiple: - lstResult(egestPaths(zenity.SelectFileMutiple(opts...))) + listResult(egestPaths(zenity.SelectFileMutiple(opts...))) } case colorSelectionDlg: - clrResult(zenity.SelectColor(opts...)) + colorResult(zenity.SelectColor(opts...)) } flag.Usage() @@ -95,6 +99,7 @@ func main() { func setupFlags() { // Application Options + flag.BoolVar(¬ification, "notification", false, "Display notification") flag.BoolVar(&errorDlg, "error", false, "Display error dialog") flag.BoolVar(&infoDlg, "info", false, "Display info dialog") flag.BoolVar(&warningDlg, "warning", false, "Display warning dialog") @@ -105,11 +110,12 @@ func setupFlags() { // General options flag.StringVar(&title, "title", "", "Set the dialog title") + flag.StringVar(&icon, "window-icon", "", "Set the window icon (error, info, question, warning)") // Message options flag.StringVar(&text, "text", "", "Set the dialog text") - flag.StringVar(&iconName, "icon-name", "", "Set the dialog icon (error, info, question, warning)") + flag.StringVar(&icon, "icon-name", "", "Set the dialog icon (error, info, question, warning)") flag.StringVar(&okLabel, "ok-label", "", "Set the label of the OK button") flag.StringVar(&cancelLabel, "cancel-label", "", "Set the label of the Cancel button") flag.StringVar(&extraButton, "extra-button", "", "Add an extra button") @@ -142,6 +148,9 @@ func setupFlags() { func validateFlags() { var n int + if notification { + n++ + } if errorDlg { n++ } @@ -175,19 +184,19 @@ func loadFlags() []zenity.Option { // Message options - var icon zenity.MessageIcon - switch iconName { + var ico zenity.DialogIcon + switch icon { case "error", "dialog-error": - icon = zenity.ErrorIcon + ico = zenity.ErrorIcon case "info", "dialog-information": - icon = zenity.InfoIcon + ico = zenity.InfoIcon case "question", "dialog-question": - icon = zenity.QuestionIcon - case "warning", "dialog-warning": - icon = zenity.WarningIcon + ico = zenity.QuestionIcon + case "important", "warning", "dialog-warning": + ico = zenity.WarningIcon } - options = append(options, zenity.Icon(icon)) + options = append(options, zenity.Icon(ico)) options = append(options, zenity.OKLabel(okLabel)) options = append(options, zenity.CancelLabel(cancelLabel)) options = append(options, zenity.ExtraButton(extraButton)) @@ -234,6 +243,15 @@ func loadFlags() []zenity.Option { return options } +func errResult(err error) { + if err != nil { + os.Stderr.WriteString(err.Error()) + os.Stderr.WriteString(zenutil.LineBreak) + os.Exit(-1) + } + os.Exit(0) +} + func msgResult(ok bool, err error) { if err == zenity.ErrExtraButton { os.Stdout.WriteString(extraButton) @@ -265,7 +283,7 @@ func strResult(s string, err error) { os.Exit(0) } -func lstResult(l []string, err error) { +func listResult(l []string, err error) { if err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(zenutil.LineBreak) @@ -279,7 +297,7 @@ func lstResult(l []string, err error) { os.Exit(0) } -func clrResult(c color.Color, err error) { +func colorResult(c color.Color, err error) { if err != nil { os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(zenutil.LineBreak) diff --git a/msg.go b/msg.go index 5c71aa0..2b7b899 100644 --- a/msg.go +++ b/msg.go @@ -50,22 +50,6 @@ const ( errorKind ) -// MessageIcon is the enumeration for message dialog icons. -type MessageIcon int - -// Icons for -const ( - ErrorIcon MessageIcon = iota + 1 - WarningIcon - InfoIcon - QuestionIcon -) - -// Icon returns an Option to set the dialog icon. -func Icon(icon MessageIcon) Option { - return funcOption(func(o *options) { o.icon = icon }) -} - // OKLabel returns an Option to set the label of the OK button. func OKLabel(ok string) Option { return funcOption(func(o *options) { o.okLabel = ok }) diff --git a/notify.go b/notify.go index 031f4b3..0049c71 100644 --- a/notify.go +++ b/notify.go @@ -1,6 +1,8 @@ package zenity // Notify displays a notification. +// +// Valid options: Title, Icon. func Notify(text string, options ...Option) error { return notify(text, options) } diff --git a/zenity.go b/zenity.go index e44c82d..31a400a 100644 --- a/zenity.go +++ b/zenity.go @@ -33,7 +33,7 @@ type options struct { showPalette bool // Message options - icon MessageIcon + icon DialogIcon okLabel string cancelLabel string extraButton string @@ -63,3 +63,19 @@ func applyOptions(options []Option) (res options) { func Title(title string) Option { return funcOption(func(o *options) { o.title = title }) } + +// DialogIcon is the enumeration for dialog icons. +type DialogIcon int + +// Icons for +const ( + ErrorIcon DialogIcon = iota + 1 + WarningIcon + InfoIcon + QuestionIcon +) + +// Icon returns an Option to set the dialog icon. +func Icon(icon DialogIcon) Option { + return funcOption(func(o *options) { o.icon = icon }) +}