Reshuffle.

This commit is contained in:
Nuno Cruces 2021-03-05 15:14:30 +00:00
parent 79ade2cd7e
commit 4c753a3c39
9 changed files with 117 additions and 105 deletions

View File

@ -33,20 +33,20 @@ var (
colorSelectionDlg bool colorSelectionDlg bool
// General options // General options
title string title string
width uint width uint
height uint height uint
text string okLabel string
cancelLabel string
extraButton string
text string
icon string
// Entry options // Entry options
entryText string entryText string
hideText bool hideText bool
// Message options // Message options
icon string
okLabel string
cancelLabel string
extraButton string
noWrap bool noWrap bool
ellipsize bool ellipsize bool
defaultCancel bool defaultCancel bool
@ -136,10 +136,13 @@ 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)")
flag.UintVar(&width, "width", 0, "Set the `width`") flag.UintVar(&width, "width", 0, "Set the `width`")
flag.UintVar(&height, "height", 0, "Set the `height`") 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(&text, "text", "", "Set the dialog `text`")
flag.StringVar(&icon, "window-icon", "", "Set the window `icon` (error, info, question, warning)")
// Entry options // Entry options
flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`") flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`")
@ -147,9 +150,6 @@ func setupFlags() {
// Message options // Message options
flag.StringVar(&icon, "icon-name", "", "Set the dialog `icon` (dialog-error, dialog-information, dialog-question, dialog-warning)") 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(&noWrap, "no-wrap", false, "Do not enable text wrapping")
flag.BoolVar(&ellipsize, "ellipsize", false, "Enable ellipsizing in the dialog text") flag.BoolVar(&ellipsize, "ellipsize", false, "Enable ellipsizing in the dialog text")
flag.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default") flag.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default")
@ -180,11 +180,11 @@ func setupFlags() {
// Detect unspecified values // Detect unspecified values
title = unspecified title = unspecified
icon = unspecified
text = unspecified
okLabel = unspecified okLabel = unspecified
cancelLabel = unspecified cancelLabel = unspecified
extraButton = unspecified extraButton = unspecified
text = unspecified
icon = unspecified
} }
func validateFlags() { func validateFlags() {
@ -266,30 +266,6 @@ func loadFlags() []zenity.Option {
} }
opts = append(opts, zenity.Width(width)) opts = append(opts, zenity.Width(width))
opts = append(opts, zenity.Height(height)) 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 { if okLabel != unspecified {
opts = append(opts, zenity.OKLabel(okLabel)) opts = append(opts, zenity.OKLabel(okLabel))
} }
@ -299,6 +275,33 @@ func loadFlags() []zenity.Option {
if extraButton != unspecified { if extraButton != unspecified {
opts = append(opts, zenity.ExtraButton(extraButton)) 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 { if noWrap {
opts = append(opts, zenity.NoWrap()) opts = append(opts, zenity.NoWrap())
} }
@ -311,10 +314,6 @@ func loadFlags() []zenity.Option {
// File selection options // File selection options
opts = append(opts, fileFilters)
if filename != "" {
opts = append(opts, zenity.Filename(ingestPath(filename)))
}
if directory { if directory {
opts = append(opts, zenity.Directory()) opts = append(opts, zenity.Directory())
} }
@ -327,6 +326,10 @@ func loadFlags() []zenity.Option {
if showHidden { if showHidden {
opts = append(opts, zenity.ShowHidden()) opts = append(opts, zenity.ShowHidden())
} }
if filename != "" {
opts = append(opts, zenity.Filename(ingestPath(filename)))
}
opts = append(opts, fileFilters)
// Color selection options // Color selection options

View File

@ -4,7 +4,8 @@ package zenity
// //
// Returns nil on cancel. // 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) { func Entry(text string, options ...Option) (string, error) {
return entry(text, applyOptions(options)) return entry(text, applyOptions(options))
} }

View File

@ -10,7 +10,7 @@ import (
) )
func entry(text string, opts options) (string, error) { 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 { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }
@ -29,6 +29,9 @@ func entry(text string, opts options) (string, error) {
if opts.extraButton != nil { if opts.extraButton != nil {
args = append(args, "--extra-button", *opts.extraButton) args = append(args, "--extra-button", *opts.extraButton)
} }
if opts.hideText {
args = append(args, "--hide-text")
}
switch opts.icon { switch opts.icon {
case ErrorIcon: case ErrorIcon:
args = append(args, "--window-icon=error") args = append(args, "--window-icon=error")

18
file.go
View File

@ -33,15 +33,6 @@ func SelectFileSave(options ...Option) (string, error) {
return selectFileSave(applyOptions(options)) 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. // Directory returns an Option to activate directory-only selection.
func Directory() Option { func Directory() Option {
return funcOption(func(o *options) { o.directory = true }) return funcOption(func(o *options) { o.directory = true })
@ -64,6 +55,15 @@ func ShowHidden() Option {
return funcOption(func(o *options) { o.showHidden = true }) 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. // FileFilter is an Option that sets a filename filter.
// //
// macOS hides filename filters from the user, // macOS hides filename filters from the user,

View File

@ -11,12 +11,12 @@ import (
func selectFile(opts options) (string, error) { func selectFile(opts options) (string, error) {
args := []string{"--file-selection"} args := []string{"--file-selection"}
if opts.directory {
args = append(args, "--directory")
}
if opts.title != nil { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }
if opts.directory {
args = append(args, "--directory")
}
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", opts.filename) args = append(args, "--filename", opts.filename)
} }
@ -37,12 +37,12 @@ func selectFile(opts options) (string, error) {
func selectFileMutiple(opts options) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
if opts.directory {
args = append(args, "--directory")
}
if opts.title != nil { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }
if opts.directory {
args = append(args, "--directory")
}
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", opts.filename) args = append(args, "--filename", opts.filename)
} }
@ -63,18 +63,18 @@ func selectFileMutiple(opts options) ([]string, error) {
func selectFileSave(opts options) (string, error) { func selectFileSave(opts options) (string, error) {
args := []string{"--file-selection", "--save"} args := []string{"--file-selection", "--save"}
if opts.directory {
args = append(args, "--directory")
}
if opts.title != nil { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }
if opts.filename != "" { if opts.directory {
args = append(args, "--filename", opts.filename) args = append(args, "--directory")
} }
if opts.confirmOverwrite { if opts.confirmOverwrite {
args = append(args, "--confirm-overwrite") args = append(args, "--confirm-overwrite")
} }
if opts.filename != "" {
args = append(args, "--filename", opts.filename)
}
args = append(args, initFilters(opts.fileFilters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)

25
msg.go
View File

@ -8,8 +8,8 @@ const ErrExtraButton = stringErr("Extra button pressed")
// //
// Returns true on OK, false on Cancel, or ErrExtraButton. // Returns true on OK, false on Cancel, or ErrExtraButton.
// //
// Valid options: Title, Width, Height, Icon, OKLabel, CancelLabel, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// ExtraButton, NoWrap, Ellipsize, DefaultCancel. // Icon, NoWrap, Ellipsize, DefaultCancel.
func Question(text string, options ...Option) (bool, error) { func Question(text string, options ...Option) (bool, error) {
return message(questionKind, text, applyOptions(options)) 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. // 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. // NoWrap, Ellipsize.
func Info(text string, options ...Option) (bool, error) { func Info(text string, options ...Option) (bool, error) {
return message(infoKind, text, applyOptions(options)) 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. // 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. // NoWrap, Ellipsize.
func Warning(text string, options ...Option) (bool, error) { func Warning(text string, options ...Option) (bool, error) {
return message(warningKind, text, applyOptions(options)) 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. // 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. // NoWrap, Ellipsize.
func Error(text string, options ...Option) (bool, error) { func Error(text string, options ...Option) (bool, error) {
return message(errorKind, text, applyOptions(options)) return message(errorKind, text, applyOptions(options))
@ -53,21 +53,6 @@ const (
errorKind 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). // NoWrap returns an Option to disable enable text wrapping (Unix only).
func NoWrap() Option { func NoWrap() Option {
return funcOption(func(o *options) { o.noWrap = true }) return funcOption(func(o *options) { o.noWrap = true })

View File

@ -49,8 +49,6 @@ func message(kind messageKind, text string, opts options) (bool, error) {
args = append(args, "--default-cancel") args = append(args, "--default-cancel")
} }
switch opts.icon { switch opts.icon {
case NoIcon:
args = append(args, "--icon-name=")
case ErrorIcon: case ErrorIcon:
args = append(args, "--window-icon=error", "--icon-name=dialog-error") args = append(args, "--window-icon=error", "--icon-name=dialog-error")
case WarningIcon: 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") args = append(args, "--window-icon=info", "--icon-name=dialog-information")
case QuestionIcon: case QuestionIcon:
args = append(args, "--window-icon=question", "--icon-name=dialog-question") 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) out, err := zenutil.Run(opts.ctx, args)

View File

@ -12,8 +12,6 @@ func notify(text string, opts options) error {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }
switch opts.icon { switch opts.icon {
case NoIcon:
args = append(args, "--window-icon=dialog")
case ErrorIcon: case ErrorIcon:
args = append(args, "--window-icon=dialog-error") args = append(args, "--window-icon=dialog-error")
case WarningIcon: case WarningIcon:
@ -22,6 +20,10 @@ func notify(text string, opts options) error {
args = append(args, "--window-icon=dialog-information") args = append(args, "--window-icon=dialog-information")
case QuestionIcon: case QuestionIcon:
args = append(args, "--window-icon=dialog-question") 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) _, err := zenutil.Run(opts.ctx, args)

View File

@ -23,35 +23,35 @@ func stringPtr(s string) *string { return &s }
type options struct { type options struct {
// General options // General options
title *string title *string
width uint width uint
height 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 // File selection options
filename string
directory bool directory bool
confirmOverwrite bool confirmOverwrite bool
confirmCreate bool confirmCreate bool
showHidden bool showHidden bool
filename string
fileFilters []FileFilter fileFilters []FileFilter
// Color selection options // Color selection options
color color.Color color color.Color
showPalette bool 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 // Context for timeout
ctx context.Context 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. // DialogIcon is the enumeration for dialog icons.
type DialogIcon int type DialogIcon int
@ -101,6 +116,7 @@ const (
WarningIcon WarningIcon
InfoIcon InfoIcon
QuestionIcon QuestionIcon
PasswordIcon
NoIcon NoIcon
) )