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.
func SelectColor(options ...Option) (color.Color, error) {
return selectColor(options)
return selectColor(applyOptions(options))
}
// Color returns an Option to set the color.

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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")

View File

@ -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

8
msg.go
View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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 {

View File

@ -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))
}

View File

@ -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

View File

@ -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 != "" {

View File

@ -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()
}