From 7f1f5d1aa3d7adc86b84f2381aa925df1f23cc38 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 4 Mar 2021 12:42:30 +0000 Subject: [PATCH] Refactor. --- color.go | 2 +- color_darwin.go | 4 +--- color_unix.go | 4 +--- color_windows.go | 4 +--- file.go | 6 +++--- file_darwin.go | 12 +++--------- file_unix.go | 12 +++--------- file_windows.go | 9 +++------ msg.go | 8 ++++---- msg_darwin.go | 4 +--- msg_unix.go | 4 +--- msg_windows.go | 4 +--- notify.go | 2 +- notify_darwin.go | 4 +--- notify_unix.go | 4 +--- notify_windows.go | 4 +--- 16 files changed, 27 insertions(+), 60 deletions(-) diff --git a/color.go b/color.go index 6efb616..f9978de 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(applyOptions(options)) } // Color returns an Option to set the color. diff --git a/color_darwin.go b/color_darwin.go index f724dff..138c305 100644 --- a/color_darwin.go +++ b/color_darwin.go @@ -7,9 +7,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectColor(options []Option) (color.Color, error) { - opts := applyOptions(options) - +func selectColor(opts options) (color.Color, error) { var col color.Color if opts.color != nil { col = opts.color diff --git a/color_unix.go b/color_unix.go index 3feeba8..1df04e4 100644 --- a/color_unix.go +++ b/color_unix.go @@ -9,9 +9,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectColor(options []Option) (color.Color, error) { - opts := applyOptions(options) - +func selectColor(opts options) (color.Color, error) { args := []string{"--color-selection"} if opts.title != nil { diff --git a/color_windows.go b/color_windows.go index 2504d11..2641afd 100644 --- a/color_windows.go +++ b/color_windows.go @@ -20,9 +20,7 @@ func init() { } } -func selectColor(options []Option) (color.Color, error) { - opts := applyOptions(options) - +func selectColor(opts options) (color.Color, error) { // load custom colors colorsMutex.Lock() customColors := savedColors diff --git a/file.go b/file.go index eeafa15..9201b86 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(applyOptions(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(applyOptions(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(applyOptions(options)) } // Filename returns an Option to set the filename. diff --git a/file_darwin.go b/file_darwin.go index 999d9f6..7e1b92c 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -7,9 +7,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectFile(options []Option) (string, error) { - opts := applyOptions(options) - +func selectFile(opts options) (string, error) { var data zenutil.File data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden @@ -35,9 +33,7 @@ func selectFile(options []Option) (string, error) { return string(out), nil } -func selectFileMutiple(options []Option) ([]string, error) { - opts := applyOptions(options) - +func selectFileMutiple(opts options) ([]string, error) { var data zenutil.File data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden @@ -68,9 +64,7 @@ func selectFileMutiple(options []Option) ([]string, error) { return strings.Split(string(out), zenutil.Separator), nil } -func selectFileSave(options []Option) (string, error) { - opts := applyOptions(options) - +func selectFileSave(opts options) (string, error) { var data zenutil.File data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden diff --git a/file_unix.go b/file_unix.go index a62b418..0d828ba 100644 --- a/file_unix.go +++ b/file_unix.go @@ -9,9 +9,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func selectFile(options []Option) (string, error) { - opts := applyOptions(options) - +func selectFile(opts options) (string, error) { args := []string{"--file-selection"} if opts.directory { args = append(args, "--directory") @@ -37,9 +35,7 @@ func selectFile(options []Option) (string, error) { return string(out), nil } -func selectFileMutiple(options []Option) ([]string, error) { - opts := applyOptions(options) - +func selectFileMutiple(opts options) ([]string, error) { args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} if opts.directory { args = append(args, "--directory") @@ -65,9 +61,7 @@ func selectFileMutiple(options []Option) ([]string, error) { return strings.Split(string(out), zenutil.Separator), nil } -func selectFileSave(options []Option) (string, error) { - opts := applyOptions(options) - +func selectFileSave(opts options) (string, error) { args := []string{"--file-selection", "--save"} if opts.directory { args = append(args, "--directory") diff --git a/file_windows.go b/file_windows.go index 8f06eab..5a7d70a 100644 --- a/file_windows.go +++ b/file_windows.go @@ -17,8 +17,7 @@ var ( shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName") ) -func selectFile(options []Option) (string, error) { - opts := applyOptions(options) +func selectFile(opts options) (string, error) { if opts.directory { res, _, err := pickFolders(opts, false) return res, err @@ -65,8 +64,7 @@ func selectFile(options []Option) (string, error) { return syscall.UTF16ToString(res[:]), nil } -func selectFileMutiple(options []Option) ([]string, error) { - opts := applyOptions(options) +func selectFileMutiple(opts options) ([]string, error) { if opts.directory { _, res, err := pickFolders(opts, true) return res, err @@ -138,8 +136,7 @@ func selectFileMutiple(options []Option) ([]string, error) { return split, nil } -func selectFileSave(options []Option) (string, error) { - opts := applyOptions(options) +func selectFileSave(opts options) (string, error) { if opts.directory { res, _, err := pickFolders(opts, false) return res, err diff --git a/msg.go b/msg.go index 188d1a5..a9ab5c4 100644 --- a/msg.go +++ b/msg.go @@ -11,7 +11,7 @@ const ErrExtraButton = stringErr("Extra button pressed") // Valid options: Title, Width, Height, Icon, OKLabel, CancelLabel, // ExtraButton, NoWrap, Ellipsize, DefaultCancel. func Question(text string, options ...Option) (bool, error) { - return message(questionKind, text, options) + return message(questionKind, text, applyOptions(options)) } // Info displays the info dialog. @@ -21,7 +21,7 @@ func Question(text string, options ...Option) (bool, error) { // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, // NoWrap, Ellipsize. func Info(text string, options ...Option) (bool, error) { - return message(infoKind, text, options) + return message(infoKind, text, applyOptions(options)) } // Warning displays the warning dialog. @@ -31,7 +31,7 @@ func Info(text string, options ...Option) (bool, error) { // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, // NoWrap, Ellipsize. func Warning(text string, options ...Option) (bool, error) { - return message(warningKind, text, options) + return message(warningKind, text, applyOptions(options)) } // Error displays the error dialog. @@ -41,7 +41,7 @@ func Warning(text string, options ...Option) (bool, error) { // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton, // NoWrap, Ellipsize. func Error(text string, options ...Option) (bool, error) { - return message(errorKind, text, options) + return message(errorKind, text, applyOptions(options)) } type messageKind int diff --git a/msg_darwin.go b/msg_darwin.go index dd9177f..1e7642e 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -6,9 +6,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func message(kind messageKind, text string, options []Option) (bool, error) { - opts := applyOptions(options) - +func message(kind messageKind, text string, opts options) (bool, error) { var data zenutil.Msg data.Text = text data.Options.Timeout = zenutil.Timeout diff --git a/msg_unix.go b/msg_unix.go index 7912b6c..3d432e5 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -9,9 +9,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func message(kind messageKind, text string, options []Option) (bool, error) { - opts := applyOptions(options) - +func message(kind messageKind, text string, opts options) (bool, error) { var args []string switch kind { case questionKind: diff --git a/msg_windows.go b/msg_windows.go index d0bc1f6..8342b3e 100644 --- a/msg_windows.go +++ b/msg_windows.go @@ -11,9 +11,7 @@ var ( messageBox = user32.NewProc("MessageBoxW") ) -func message(kind messageKind, text string, options []Option) (bool, error) { - opts := applyOptions(options) - +func message(kind messageKind, text string, opts options) (bool, error) { var flags uintptr switch { diff --git a/notify.go b/notify.go index 0049c71..ca4695a 100644 --- a/notify.go +++ b/notify.go @@ -4,5 +4,5 @@ package zenity // // Valid options: Title, Icon. func Notify(text string, options ...Option) error { - return notify(text, options) + return notify(text, applyOptions(options)) } diff --git a/notify_darwin.go b/notify_darwin.go index 268fdbc..e15ee0e 100644 --- a/notify_darwin.go +++ b/notify_darwin.go @@ -6,9 +6,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func notify(text string, options []Option) error { - opts := applyOptions(options) - +func notify(text string, opts options) error { var data zenutil.Notify data.Text = text data.Options.Title = opts.title diff --git a/notify_unix.go b/notify_unix.go index e471a51..b65bcc7 100644 --- a/notify_unix.go +++ b/notify_unix.go @@ -6,9 +6,7 @@ import ( "github.com/ncruces/zenity/internal/zenutil" ) -func notify(text string, options []Option) error { - opts := applyOptions(options) - +func notify(text string, opts options) error { args := []string{"--notification"} if text != "" { diff --git a/notify_windows.go b/notify_windows.go index 61c93de..fbf8460 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -13,9 +13,7 @@ var ( wtsSendMessage = wtsapi32.NewProc("WTSSendMessageW") ) -func notify(text string, options []Option) error { - opts := applyOptions(options) - +func notify(text string, opts options) error { if opts.ctx != nil && opts.ctx.Err() != nil { return opts.ctx.Err() }