Notifications.

This commit is contained in:
Nuno Cruces 2020-01-27 15:42:43 +00:00
parent 7b7d915e3a
commit 3d8fdb534e
6 changed files with 54 additions and 32 deletions

View file

@ -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) * [message](https://github.com/ncruces/zenity/wiki/Message-dialog) (error, info, question, warning)
* [file selection](https://github.com/ncruces/zenity/wiki/File-Selection-dialog) * [file selection](https://github.com/ncruces/zenity/wiki/File-Selection-dialog)
* [color selection](https://github.com/ncruces/zenity/wiki/Color-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. Behavior on Windows, macOS and other Unixes might differ slightly.
Some of that is intended (reflecting platform differences), Some of that is intended (reflecting platform differences),

View file

@ -1,2 +1,3 @@
zenity zenity
*.syso *.syso
*.zip

View file

@ -17,6 +17,7 @@ import (
var ( var (
// Application Options // Application Options
notification bool
errorDlg bool errorDlg bool
infoDlg bool infoDlg bool
warningDlg bool warningDlg bool
@ -29,7 +30,7 @@ var (
// Message options // Message options
text string text string
iconName string icon string
okLabel string okLabel string
cancelLabel string cancelLabel string
extraButton string extraButton string
@ -65,6 +66,9 @@ func main() {
zenutil.Command = true zenutil.Command = true
switch { switch {
case notification:
errResult(zenity.Notify(text, opts...))
case errorDlg: case errorDlg:
msgResult(zenity.Error(text, opts...)) msgResult(zenity.Error(text, opts...))
case infoDlg: case infoDlg:
@ -81,11 +85,11 @@ func main() {
case save: case save:
strResult(egestPath(zenity.SelectFileSave(opts...))) strResult(egestPath(zenity.SelectFileSave(opts...)))
case multiple: case multiple:
lstResult(egestPaths(zenity.SelectFileMutiple(opts...))) listResult(egestPaths(zenity.SelectFileMutiple(opts...)))
} }
case colorSelectionDlg: case colorSelectionDlg:
clrResult(zenity.SelectColor(opts...)) colorResult(zenity.SelectColor(opts...))
} }
flag.Usage() flag.Usage()
@ -95,6 +99,7 @@ func main() {
func setupFlags() { func setupFlags() {
// Application Options // Application Options
flag.BoolVar(&notification, "notification", false, "Display notification")
flag.BoolVar(&errorDlg, "error", false, "Display error dialog") flag.BoolVar(&errorDlg, "error", false, "Display error dialog")
flag.BoolVar(&infoDlg, "info", false, "Display info dialog") flag.BoolVar(&infoDlg, "info", false, "Display info dialog")
flag.BoolVar(&warningDlg, "warning", false, "Display warning dialog") flag.BoolVar(&warningDlg, "warning", false, "Display warning dialog")
@ -105,11 +110,12 @@ func setupFlags() {
// General options // General options
flag.StringVar(&title, "title", "", "Set the dialog title") flag.StringVar(&title, "title", "", "Set the dialog title")
flag.StringVar(&icon, "window-icon", "", "Set the window icon (error, info, question, warning)")
// Message options // Message options
flag.StringVar(&text, "text", "", "Set the dialog text") 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(&okLabel, "ok-label", "", "Set the label of the OK button")
flag.StringVar(&cancelLabel, "cancel-label", "", "Set the label of the Cancel button") flag.StringVar(&cancelLabel, "cancel-label", "", "Set the label of the Cancel button")
flag.StringVar(&extraButton, "extra-button", "", "Add an extra button") flag.StringVar(&extraButton, "extra-button", "", "Add an extra button")
@ -142,6 +148,9 @@ func setupFlags() {
func validateFlags() { func validateFlags() {
var n int var n int
if notification {
n++
}
if errorDlg { if errorDlg {
n++ n++
} }
@ -175,19 +184,19 @@ func loadFlags() []zenity.Option {
// Message options // Message options
var icon zenity.MessageIcon var ico zenity.DialogIcon
switch iconName { switch icon {
case "error", "dialog-error": case "error", "dialog-error":
icon = zenity.ErrorIcon ico = zenity.ErrorIcon
case "info", "dialog-information": case "info", "dialog-information":
icon = zenity.InfoIcon ico = zenity.InfoIcon
case "question", "dialog-question": case "question", "dialog-question":
icon = zenity.QuestionIcon ico = zenity.QuestionIcon
case "warning", "dialog-warning": case "important", "warning", "dialog-warning":
icon = zenity.WarningIcon 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.OKLabel(okLabel))
options = append(options, zenity.CancelLabel(cancelLabel)) options = append(options, zenity.CancelLabel(cancelLabel))
options = append(options, zenity.ExtraButton(extraButton)) options = append(options, zenity.ExtraButton(extraButton))
@ -234,6 +243,15 @@ func loadFlags() []zenity.Option {
return options 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) { func msgResult(ok bool, err error) {
if err == zenity.ErrExtraButton { if err == zenity.ErrExtraButton {
os.Stdout.WriteString(extraButton) os.Stdout.WriteString(extraButton)
@ -265,7 +283,7 @@ func strResult(s string, err error) {
os.Exit(0) os.Exit(0)
} }
func lstResult(l []string, err error) { func listResult(l []string, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
os.Stderr.WriteString(zenutil.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
@ -279,7 +297,7 @@ func lstResult(l []string, err error) {
os.Exit(0) os.Exit(0)
} }
func clrResult(c color.Color, err error) { func colorResult(c color.Color, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
os.Stderr.WriteString(zenutil.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)

16
msg.go
View file

@ -50,22 +50,6 @@ const (
errorKind 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. // OKLabel returns an Option to set the label of the OK button.
func OKLabel(ok string) Option { func OKLabel(ok string) Option {
return funcOption(func(o *options) { o.okLabel = ok }) return funcOption(func(o *options) { o.okLabel = ok })

View file

@ -1,6 +1,8 @@
package zenity package zenity
// Notify displays a notification. // Notify displays a notification.
//
// Valid options: Title, Icon.
func Notify(text string, options ...Option) error { func Notify(text string, options ...Option) error {
return notify(text, options) return notify(text, options)
} }

View file

@ -33,7 +33,7 @@ type options struct {
showPalette bool showPalette bool
// Message options // Message options
icon MessageIcon icon DialogIcon
okLabel string okLabel string
cancelLabel string cancelLabel string
extraButton string extraButton string
@ -63,3 +63,19 @@ func applyOptions(options []Option) (res options) {
func Title(title string) Option { func Title(title string) Option {
return funcOption(func(o *options) { o.title = title }) 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 })
}