Refactor.

This commit is contained in:
Nuno Cruces 2021-03-04 12:42:30 +00:00
parent abdae34d4e
commit 7f1f5d1aa3
16 changed files with 27 additions and 60 deletions

View file

@ -8,7 +8,7 @@ import "image/color"
// //
// Valid options: Title, Color, ShowPalette. // Valid options: Title, Color, ShowPalette.
func SelectColor(options ...Option) (color.Color, error) { func SelectColor(options ...Option) (color.Color, error) {
return selectColor(options) return selectColor(applyOptions(options))
} }
// Color returns an Option to set the color. // Color returns an Option to set the color.

View file

@ -7,9 +7,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectColor(options []Option) (color.Color, error) { func selectColor(opts options) (color.Color, error) {
opts := applyOptions(options)
var col color.Color var col color.Color
if opts.color != nil { if opts.color != nil {
col = opts.color col = opts.color

View file

@ -9,9 +9,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectColor(options []Option) (color.Color, error) { func selectColor(opts options) (color.Color, error) {
opts := applyOptions(options)
args := []string{"--color-selection"} args := []string{"--color-selection"}
if opts.title != nil { if opts.title != nil {

View file

@ -20,9 +20,7 @@ func init() {
} }
} }
func selectColor(options []Option) (color.Color, error) { func selectColor(opts options) (color.Color, error) {
opts := applyOptions(options)
// load custom colors // load custom colors
colorsMutex.Lock() colorsMutex.Lock()
customColors := savedColors customColors := savedColors

View file

@ -11,7 +11,7 @@ import (
// //
// Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s). // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFile(options ...Option) (string, error) { func SelectFile(options ...Option) (string, error) {
return selectFile(options) return selectFile(applyOptions(options))
} }
// SelectFileMutiple displays the multiple file selection dialog. // 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). // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFileMutiple(options ...Option) ([]string, error) { func SelectFileMutiple(options ...Option) ([]string, error) {
return selectFileMutiple(options) return selectFileMutiple(applyOptions(options))
} }
// SelectFileSave displays the save file selection dialog. // SelectFileSave displays the save file selection dialog.
@ -30,7 +30,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
// Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden, // Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden,
// FileFilter(s). // FileFilter(s).
func SelectFileSave(options ...Option) (string, error) { func SelectFileSave(options ...Option) (string, error) {
return selectFileSave(options) return selectFileSave(applyOptions(options))
} }
// Filename returns an Option to set the filename. // Filename returns an Option to set the filename.

View file

@ -7,9 +7,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectFile(options []Option) (string, error) { func selectFile(opts options) (string, error) {
opts := applyOptions(options)
var data zenutil.File var data zenutil.File
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden data.Options.Invisibles = opts.showHidden
@ -35,9 +33,7 @@ func selectFile(options []Option) (string, error) {
return string(out), nil return string(out), nil
} }
func selectFileMutiple(options []Option) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
opts := applyOptions(options)
var data zenutil.File var data zenutil.File
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden data.Options.Invisibles = opts.showHidden
@ -68,9 +64,7 @@ func selectFileMutiple(options []Option) ([]string, error) {
return strings.Split(string(out), zenutil.Separator), nil return strings.Split(string(out), zenutil.Separator), nil
} }
func selectFileSave(options []Option) (string, error) { func selectFileSave(opts options) (string, error) {
opts := applyOptions(options)
var data zenutil.File var data zenutil.File
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden data.Options.Invisibles = opts.showHidden

View file

@ -9,9 +9,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectFile(options []Option) (string, error) { func selectFile(opts options) (string, error) {
opts := applyOptions(options)
args := []string{"--file-selection"} args := []string{"--file-selection"}
if opts.directory { if opts.directory {
args = append(args, "--directory") args = append(args, "--directory")
@ -37,9 +35,7 @@ func selectFile(options []Option) (string, error) {
return string(out), nil return string(out), nil
} }
func selectFileMutiple(options []Option) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
opts := applyOptions(options)
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
if opts.directory { if opts.directory {
args = append(args, "--directory") args = append(args, "--directory")
@ -65,9 +61,7 @@ func selectFileMutiple(options []Option) ([]string, error) {
return strings.Split(string(out), zenutil.Separator), nil return strings.Split(string(out), zenutil.Separator), nil
} }
func selectFileSave(options []Option) (string, error) { func selectFileSave(opts options) (string, error) {
opts := applyOptions(options)
args := []string{"--file-selection", "--save"} args := []string{"--file-selection", "--save"}
if opts.directory { if opts.directory {
args = append(args, "--directory") args = append(args, "--directory")

View file

@ -17,8 +17,7 @@ var (
shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName") shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName")
) )
func selectFile(options []Option) (string, error) { func selectFile(opts options) (string, error) {
opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)
return res, err return res, err
@ -65,8 +64,7 @@ func selectFile(options []Option) (string, error) {
return syscall.UTF16ToString(res[:]), nil return syscall.UTF16ToString(res[:]), nil
} }
func selectFileMutiple(options []Option) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
opts := applyOptions(options)
if opts.directory { if opts.directory {
_, res, err := pickFolders(opts, true) _, res, err := pickFolders(opts, true)
return res, err return res, err
@ -138,8 +136,7 @@ func selectFileMutiple(options []Option) ([]string, error) {
return split, nil return split, nil
} }
func selectFileSave(options []Option) (string, error) { func selectFileSave(opts options) (string, error) {
opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)
return res, err return res, err

8
msg.go
View file

@ -11,7 +11,7 @@ const ErrExtraButton = stringErr("Extra button pressed")
// Valid options: Title, Width, Height, Icon, OKLabel, CancelLabel, // Valid options: Title, Width, Height, Icon, OKLabel, CancelLabel,
// ExtraButton, NoWrap, Ellipsize, DefaultCancel. // ExtraButton, NoWrap, Ellipsize, DefaultCancel.
func Question(text string, options ...Option) (bool, error) { func Question(text string, options ...Option) (bool, error) {
return message(questionKind, text, options) return message(questionKind, text, applyOptions(options))
} }
// Info displays the info dialog. // 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, // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton,
// NoWrap, Ellipsize. // NoWrap, Ellipsize.
func Info(text string, options ...Option) (bool, error) { func Info(text string, options ...Option) (bool, error) {
return message(infoKind, text, options) return message(infoKind, text, applyOptions(options))
} }
// Warning displays the warning dialog. // 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, // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton,
// NoWrap, Ellipsize. // NoWrap, Ellipsize.
func Warning(text string, options ...Option) (bool, error) { func Warning(text string, options ...Option) (bool, error) {
return message(warningKind, text, options) return message(warningKind, text, applyOptions(options))
} }
// Error displays the error dialog. // 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, // Valid options: Title, Width, Height, Icon, OKLabel, ExtraButton,
// NoWrap, Ellipsize. // NoWrap, Ellipsize.
func Error(text string, options ...Option) (bool, error) { func Error(text string, options ...Option) (bool, error) {
return message(errorKind, text, options) return message(errorKind, text, applyOptions(options))
} }
type messageKind int type messageKind int

View file

@ -6,9 +6,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, opts options) (bool, error) {
opts := applyOptions(options)
var data zenutil.Msg var data zenutil.Msg
data.Text = text data.Text = text
data.Options.Timeout = zenutil.Timeout data.Options.Timeout = zenutil.Timeout

View file

@ -9,9 +9,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, opts options) (bool, error) {
opts := applyOptions(options)
var args []string var args []string
switch kind { switch kind {
case questionKind: case questionKind:

View file

@ -11,9 +11,7 @@ var (
messageBox = user32.NewProc("MessageBoxW") messageBox = user32.NewProc("MessageBoxW")
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, opts options) (bool, error) {
opts := applyOptions(options)
var flags uintptr var flags uintptr
switch { switch {

View file

@ -4,5 +4,5 @@ package zenity
// //
// Valid options: Title, Icon. // 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, applyOptions(options))
} }

View file

@ -6,9 +6,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func notify(text string, options []Option) error { func notify(text string, opts options) error {
opts := applyOptions(options)
var data zenutil.Notify var data zenutil.Notify
data.Text = text data.Text = text
data.Options.Title = opts.title data.Options.Title = opts.title

View file

@ -6,9 +6,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func notify(text string, options []Option) error { func notify(text string, opts options) error {
opts := applyOptions(options)
args := []string{"--notification"} args := []string{"--notification"}
if text != "" { if text != "" {

View file

@ -13,9 +13,7 @@ var (
wtsSendMessage = wtsapi32.NewProc("WTSSendMessageW") wtsSendMessage = wtsapi32.NewProc("WTSSendMessageW")
) )
func notify(text string, options []Option) error { func notify(text string, opts options) error {
opts := applyOptions(options)
if opts.ctx != nil && opts.ctx.Err() != nil { if opts.ctx != nil && opts.ctx.Err() != nil {
return opts.ctx.Err() return opts.ctx.Err()
} }