From 4c753a3c3959c85c71c22dcc4b445783487af8b6 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 5 Mar 2021 15:14:30 +0000 Subject: [PATCH] Reshuffle. --- cmd/zenity/main.go | 87 ++++++++++++++++++++++++---------------------- entry.go | 3 +- entry_unix.go | 5 ++- file.go | 18 +++++----- file_unix.go | 22 ++++++------ msg.go | 25 +++---------- msg_unix.go | 6 ++-- notify_unix.go | 6 ++-- zenity.go | 50 +++++++++++++++++--------- 9 files changed, 117 insertions(+), 105 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 9b4ff2a..7584780 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -33,20 +33,20 @@ var ( colorSelectionDlg bool // General options - title string - width uint - height uint - text string + title string + width uint + height uint + okLabel string + cancelLabel string + extraButton string + text string + icon string // Entry options entryText string hideText bool // Message options - icon string - okLabel string - cancelLabel string - extraButton string noWrap bool ellipsize bool defaultCancel bool @@ -136,10 +136,13 @@ 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)") flag.UintVar(&width, "width", 0, "Set the `width`") flag.UintVar(&height, "height", 0, "Set the `height`") + 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") flag.StringVar(&text, "text", "", "Set the dialog `text`") + flag.StringVar(&icon, "window-icon", "", "Set the window `icon` (error, info, question, warning)") // Entry options flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`") @@ -147,9 +150,6 @@ func setupFlags() { // Message options flag.StringVar(&icon, "icon-name", "", "Set the dialog `icon` (dialog-error, dialog-information, dialog-question, dialog-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") flag.BoolVar(&noWrap, "no-wrap", false, "Do not enable text wrapping") flag.BoolVar(&ellipsize, "ellipsize", false, "Enable ellipsizing in the dialog text") flag.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default") @@ -180,11 +180,11 @@ func setupFlags() { // Detect unspecified values title = unspecified - icon = unspecified - text = unspecified okLabel = unspecified cancelLabel = unspecified extraButton = unspecified + text = unspecified + icon = unspecified } func validateFlags() { @@ -266,30 +266,6 @@ func loadFlags() []zenity.Option { } opts = append(opts, zenity.Width(width)) opts = append(opts, zenity.Height(height)) - - // Entry options - opts = append(opts, zenity.EntryText(entryText)) - if hideText { - opts = append(opts, zenity.HideText()) - } - - // Message options - - var ico zenity.DialogIcon - switch icon { - case "": - ico = zenity.NoIcon - case "error", "dialog-error": - ico = zenity.ErrorIcon - case "info", "dialog-information": - ico = zenity.InfoIcon - case "question", "dialog-question": - ico = zenity.QuestionIcon - case "important", "warning", "dialog-warning": - ico = zenity.WarningIcon - } - - opts = append(opts, zenity.Icon(ico)) if okLabel != unspecified { opts = append(opts, zenity.OKLabel(okLabel)) } @@ -299,6 +275,33 @@ func loadFlags() []zenity.Option { if extraButton != unspecified { opts = append(opts, zenity.ExtraButton(extraButton)) } + + var ico zenity.DialogIcon + switch icon { + case "error", "dialog-error": + ico = zenity.ErrorIcon + case "info", "dialog-information": + ico = zenity.InfoIcon + case "question", "dialog-question": + ico = zenity.QuestionIcon + case "important", "warning", "dialog-warning": + ico = zenity.WarningIcon + case "dialog-password": + ico = zenity.PasswordIcon + case "": + ico = zenity.NoIcon + } + opts = append(opts, zenity.Icon(ico)) + + // Entry options + + opts = append(opts, zenity.EntryText(entryText)) + if hideText { + opts = append(opts, zenity.HideText()) + } + + // Message options + if noWrap { opts = append(opts, zenity.NoWrap()) } @@ -311,10 +314,6 @@ func loadFlags() []zenity.Option { // File selection options - opts = append(opts, fileFilters) - if filename != "" { - opts = append(opts, zenity.Filename(ingestPath(filename))) - } if directory { opts = append(opts, zenity.Directory()) } @@ -327,6 +326,10 @@ func loadFlags() []zenity.Option { if showHidden { opts = append(opts, zenity.ShowHidden()) } + if filename != "" { + opts = append(opts, zenity.Filename(ingestPath(filename))) + } + opts = append(opts, fileFilters) // Color selection options diff --git a/entry.go b/entry.go index 06fdbd2..b19e01c 100644 --- a/entry.go +++ b/entry.go @@ -4,7 +4,8 @@ package zenity // // Returns nil on cancel. // -// Valid options: Title, Text, EntryText, HideText. +// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, +// Icon, EntryText, HideText. func Entry(text string, options ...Option) (string, error) { return entry(text, applyOptions(options)) } diff --git a/entry_unix.go b/entry_unix.go index f6f3d09..bc1009b 100644 --- a/entry_unix.go +++ b/entry_unix.go @@ -10,7 +10,7 @@ import ( ) func entry(text string, opts options) (string, error) { - args := []string{"--entry", "--text", text} + args := []string{"--entry", "--text", text, "--entry-text", opts.entryText} if opts.title != nil { args = append(args, "--title", *opts.title) } @@ -29,6 +29,9 @@ func entry(text string, opts options) (string, error) { if opts.extraButton != nil { args = append(args, "--extra-button", *opts.extraButton) } + if opts.hideText { + args = append(args, "--hide-text") + } switch opts.icon { case ErrorIcon: args = append(args, "--window-icon=error") diff --git a/file.go b/file.go index 9201b86..a1fcce1 100644 --- a/file.go +++ b/file.go @@ -33,15 +33,6 @@ func SelectFileSave(options ...Option) (string, error) { return selectFileSave(applyOptions(options)) } -// Filename returns an Option to set the filename. -// -// You can specify a file name, a directory path, or both. -// Specifying a file name, makes it the default selected file. -// Specifying a directory path, makes it the default dialog location. -func Filename(filename string) Option { - return funcOption(func(o *options) { o.filename = filename }) -} - // Directory returns an Option to activate directory-only selection. func Directory() Option { return funcOption(func(o *options) { o.directory = true }) @@ -64,6 +55,15 @@ func ShowHidden() Option { return funcOption(func(o *options) { o.showHidden = true }) } +// Filename returns an Option to set the filename. +// +// You can specify a file name, a directory path, or both. +// Specifying a file name, makes it the default selected file. +// Specifying a directory path, makes it the default dialog location. +func Filename(filename string) Option { + return funcOption(func(o *options) { o.filename = filename }) +} + // FileFilter is an Option that sets a filename filter. // // macOS hides filename filters from the user, diff --git a/file_unix.go b/file_unix.go index 0d828ba..0f81127 100644 --- a/file_unix.go +++ b/file_unix.go @@ -11,12 +11,12 @@ import ( func selectFile(opts options) (string, error) { args := []string{"--file-selection"} - if opts.directory { - args = append(args, "--directory") - } if opts.title != nil { args = append(args, "--title", *opts.title) } + if opts.directory { + args = append(args, "--directory") + } if opts.filename != "" { args = append(args, "--filename", opts.filename) } @@ -37,12 +37,12 @@ func selectFile(opts options) (string, error) { func selectFileMutiple(opts options) ([]string, error) { args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} - if opts.directory { - args = append(args, "--directory") - } if opts.title != nil { args = append(args, "--title", *opts.title) } + if opts.directory { + args = append(args, "--directory") + } if opts.filename != "" { args = append(args, "--filename", opts.filename) } @@ -63,18 +63,18 @@ func selectFileMutiple(opts options) ([]string, error) { func selectFileSave(opts options) (string, error) { args := []string{"--file-selection", "--save"} - if opts.directory { - args = append(args, "--directory") - } if opts.title != nil { args = append(args, "--title", *opts.title) } - if opts.filename != "" { - args = append(args, "--filename", opts.filename) + if opts.directory { + args = append(args, "--directory") } if opts.confirmOverwrite { args = append(args, "--confirm-overwrite") } + if opts.filename != "" { + args = append(args, "--filename", opts.filename) + } args = append(args, initFilters(opts.fileFilters)...) out, err := zenutil.Run(opts.ctx, args) diff --git a/msg.go b/msg.go index a9ab5c4..7401db8 100644 --- a/msg.go +++ b/msg.go @@ -8,8 +8,8 @@ const ErrExtraButton = stringErr("Extra button pressed") // // Returns true on OK, false on Cancel, or ErrExtraButton. // -// Valid options: Title, Width, Height, Icon, OKLabel, CancelLabel, -// ExtraButton, NoWrap, Ellipsize, DefaultCancel. +// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, +// Icon, NoWrap, Ellipsize, DefaultCancel. func Question(text string, options ...Option) (bool, error) { return message(questionKind, text, applyOptions(options)) } @@ -18,7 +18,7 @@ func Question(text string, options ...Option) (bool, error) { // // Returns true on OK, false on dismiss, or ErrExtraButton. // -// Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, +// Valid options: Title, Width, Height, OKLabel, ExtraButton, Icon, // NoWrap, Ellipsize. func Info(text string, options ...Option) (bool, error) { return message(infoKind, text, applyOptions(options)) @@ -28,7 +28,7 @@ func Info(text string, options ...Option) (bool, error) { // // Returns true on OK, false on dismiss, or ErrExtraButton. // -// Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, +// Valid options: Title, Width, Height, OKLabel, ExtraButton, Icon, // NoWrap, Ellipsize. func Warning(text string, options ...Option) (bool, error) { return message(warningKind, text, applyOptions(options)) @@ -38,7 +38,7 @@ func Warning(text string, options ...Option) (bool, error) { // // Returns true on OK, false on dismiss, or ErrExtraButton. // -// Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, +// Valid options: Title, Width, Height, OKLabel, ExtraButton, Icon, // NoWrap, Ellipsize. func Error(text string, options ...Option) (bool, error) { return message(errorKind, text, applyOptions(options)) @@ -53,21 +53,6 @@ const ( errorKind ) -// 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 }) -} - -// CancelLabel returns an Option to set the label of the Cancel button. -func CancelLabel(cancel string) Option { - return funcOption(func(o *options) { o.cancelLabel = &cancel }) -} - -// ExtraButton returns an Option to add an extra button. -func ExtraButton(extra string) Option { - return funcOption(func(o *options) { o.extraButton = &extra }) -} - // NoWrap returns an Option to disable enable text wrapping (Unix only). func NoWrap() Option { return funcOption(func(o *options) { o.noWrap = true }) diff --git a/msg_unix.go b/msg_unix.go index 7d8ed2a..2ded76c 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -49,8 +49,6 @@ func message(kind messageKind, text string, opts options) (bool, error) { args = append(args, "--default-cancel") } switch opts.icon { - case NoIcon: - args = append(args, "--icon-name=") case ErrorIcon: args = append(args, "--window-icon=error", "--icon-name=dialog-error") case WarningIcon: @@ -59,6 +57,10 @@ func message(kind messageKind, text string, opts options) (bool, error) { args = append(args, "--window-icon=info", "--icon-name=dialog-information") case QuestionIcon: args = append(args, "--window-icon=question", "--icon-name=dialog-question") + case PasswordIcon: + args = append(args, "--icon-name=dialog-password") + case NoIcon: + args = append(args, "--icon-name=") } out, err := zenutil.Run(opts.ctx, args) diff --git a/notify_unix.go b/notify_unix.go index 1f54121..f8204f4 100644 --- a/notify_unix.go +++ b/notify_unix.go @@ -12,8 +12,6 @@ func notify(text string, opts options) error { args = append(args, "--title", *opts.title) } switch opts.icon { - case NoIcon: - args = append(args, "--window-icon=dialog") case ErrorIcon: args = append(args, "--window-icon=dialog-error") case WarningIcon: @@ -22,6 +20,10 @@ func notify(text string, opts options) error { args = append(args, "--window-icon=dialog-information") case QuestionIcon: args = append(args, "--window-icon=dialog-question") + case PasswordIcon: + args = append(args, "--window-icon=dialog-password") + case NoIcon: + args = append(args, "--window-icon=dialog") } _, err := zenutil.Run(opts.ctx, args) diff --git a/zenity.go b/zenity.go index 23e3264..b0b4afd 100644 --- a/zenity.go +++ b/zenity.go @@ -23,35 +23,35 @@ func stringPtr(s string) *string { return &s } type options struct { // General options - title *string - width uint - height uint + title *string + width uint + height uint + okLabel *string + cancelLabel *string + extraButton *string + icon DialogIcon + + // Entry options + entryText string + hideText bool + + // Message options + noWrap bool + ellipsize bool + defaultCancel bool // File selection options - filename string directory bool confirmOverwrite bool confirmCreate bool showHidden bool + filename string fileFilters []FileFilter // Color selection options color color.Color showPalette bool - // Message options - icon DialogIcon - okLabel *string - cancelLabel *string - extraButton *string - noWrap bool - ellipsize bool - defaultCancel bool - - // Entry options - entryText string - hideText bool - // Context for timeout ctx context.Context } @@ -92,6 +92,21 @@ func Height(height uint) Option { }) } +// 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 }) +} + +// CancelLabel returns an Option to set the label of the Cancel button. +func CancelLabel(cancel string) Option { + return funcOption(func(o *options) { o.cancelLabel = &cancel }) +} + +// ExtraButton returns an Option to add an extra button. +func ExtraButton(extra string) Option { + return funcOption(func(o *options) { o.extraButton = &extra }) +} + // DialogIcon is the enumeration for dialog icons. type DialogIcon int @@ -101,6 +116,7 @@ const ( WarningIcon InfoIcon QuestionIcon + PasswordIcon NoIcon )