This commit is contained in:
Nuno Cruces 2022-12-15 00:26:34 +00:00
parent 02564b6232
commit df14a314e4
12 changed files with 31 additions and 33 deletions

View file

@ -15,7 +15,6 @@ func Calendar(text string, options ...Option) (time.Time, error) {
// DefaultDate returns an Option to set the date. // DefaultDate returns an Option to set the date.
func DefaultDate(year int, month time.Month, day int) Option { func DefaultDate(year int, month time.Month, day int) Option {
return funcOption(func(o *options) { return funcOption(func(o *options) {
t := time.Date(year, month, day, 0, 0, 0, 0, time.Local) o.time = ptr(time.Date(year, month, day, 0, 0, 0, 0, time.Local))
o.time = &t
}) })
} }

View file

@ -15,8 +15,7 @@ func calendar(text string, opts options) (t time.Time, err error) {
return return
} }
if opts.time != nil { if opts.time != nil {
unix := opts.time.Unix() data.Date = ptr(opts.time.Unix())
data.Date = &unix
} }
if opts.title != nil { if opts.title != nil {

View file

@ -10,13 +10,13 @@ import (
func calendar(text string, opts options) (time.Time, error) { func calendar(text string, opts options) (time.Time, error) {
if opts.title == nil { if opts.title == nil {
opts.title = stringPtr("") opts.title = ptr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
dlg := &calendarDialog{} dlg := &calendarDialog{}

View file

@ -9,13 +9,13 @@ import (
func entry(text string, opts options) (string, error) { func entry(text string, opts options) (string, error) {
if opts.title == nil { if opts.title == nil {
opts.title = stringPtr("") opts.title = ptr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
dlg := &entryDialog{} dlg := &entryDialog{}

View file

@ -21,13 +21,13 @@ func listMultiple(text string, items []string, opts options) ([]string, error) {
func listDlg(text string, items []string, multiple bool, opts options) ([]string, error) { func listDlg(text string, items []string, multiple bool, opts options) ([]string, error) {
if opts.title == nil { if opts.title == nil {
opts.title = stringPtr("") opts.title = ptr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
dlg := &listDialog{ dlg := &listDialog{

View file

@ -87,7 +87,7 @@ func wtsMessage(text string, opts options) error {
title := opts.title title := opts.title
if title == nil { if title == nil {
title = stringPtr("Notification") title = ptr("Notification")
} }
timeout := zenutil.Timeout timeout := zenutil.Timeout

View file

@ -11,13 +11,13 @@ import (
func progress(opts options) (ProgressDialog, error) { func progress(opts options) (ProgressDialog, error) {
if opts.title == nil { if opts.title == nil {
opts.title = stringPtr("") opts.title = ptr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
if opts.maxValue == 0 { if opts.maxValue == 0 {
opts.maxValue = 100 opts.maxValue = 100

View file

@ -16,13 +16,13 @@ func password(opts options) (string, string, error) {
} }
if opts.title == nil { if opts.title == nil {
opts.title = stringPtr("") opts.title = ptr("")
} }
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
dlg := &passwordDialog{} dlg := &passwordDialog{}

View file

@ -10,11 +10,11 @@ func getButtons(dialog, okcancel bool, opts options) (btns zenutil.DialogButtons
if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil || dialog != okcancel { if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil || dialog != okcancel {
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if okcancel { if okcancel {
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
if opts.extraButton == nil { if opts.extraButton == nil {
btns.Buttons = []string{*opts.cancelLabel, *opts.okLabel} btns.Buttons = []string{*opts.cancelLabel, *opts.okLabel}
@ -50,10 +50,10 @@ func getButtons(dialog, okcancel bool, opts options) (btns zenutil.DialogButtons
func getAlertButtons(opts options) (ok, cancel string, extra *string) { func getAlertButtons(opts options) (ok, cancel string, extra *string) {
if opts.okLabel == nil { if opts.okLabel == nil {
opts.okLabel = stringPtr("OK") opts.okLabel = ptr("OK")
} }
if opts.cancelLabel == nil { if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel") opts.cancelLabel = ptr("Cancel")
} }
return *opts.okLabel, *opts.cancelLabel, opts.extraButton return *opts.okLabel, *opts.cancelLabel, opts.extraButton
} }

View file

@ -32,7 +32,7 @@ func Test_quoteAccelerators(t *testing.T) {
func Test_appendGeneral(t *testing.T) { func Test_appendGeneral(t *testing.T) {
got := appendGeneral(nil, options{ got := appendGeneral(nil, options{
title: stringPtr("Title"), title: ptr("Title"),
attach: 12345, attach: 12345,
modal: true, modal: true,
}) })
@ -48,9 +48,9 @@ func Test_appendButtons(t *testing.T) {
opts options opts options
want []string want []string
}{ }{
{name: "OK", opts: options{okLabel: stringPtr("OK")}, want: []string{"--ok-label", "OK"}}, {name: "OK", opts: options{okLabel: ptr("OK")}, want: []string{"--ok-label", "OK"}},
{name: "Cancel", opts: options{cancelLabel: stringPtr("Cancel")}, want: []string{"--cancel-label", "Cancel"}}, {name: "Cancel", opts: options{cancelLabel: ptr("Cancel")}, want: []string{"--cancel-label", "Cancel"}},
{name: "Extra", opts: options{extraButton: stringPtr("Extra")}, want: []string{"--extra-button", "Extra"}}, {name: "Extra", opts: options{extraButton: ptr("Extra")}, want: []string{"--extra-button", "Extra"}},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View file

@ -18,7 +18,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func stringPtr(s string) *string { return &s } func ptr[T any](v T) *T { return &v }
// ErrCanceled is returned when the cancel button is pressed, // ErrCanceled is returned when the cancel button is pressed,
// or window functions are used to close the dialog. // or window functions are used to close the dialog.

View file

@ -18,12 +18,12 @@ func Test_applyOptions(t *testing.T) {
want options want options
}{ }{
// General options // General options
{name: "Title", args: Title("Title"), want: options{title: stringPtr("Title")}}, {name: "Title", args: Title("Title"), want: options{title: ptr("Title")}},
{name: "Width", args: Width(100), want: options{width: 100}}, {name: "Width", args: Width(100), want: options{width: 100}},
{name: "Height", args: Height(100), want: options{height: 100}}, {name: "Height", args: Height(100), want: options{height: 100}},
{name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: stringPtr("OK")}}, {name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: ptr("OK")}},
{name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: stringPtr("Cancel")}}, {name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: ptr("Cancel")}},
{name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: stringPtr("Extra")}}, {name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: ptr("Extra")}},
{name: "DefaultCancel", args: DefaultCancel(), want: options{defaultCancel: true}}, {name: "DefaultCancel", args: DefaultCancel(), want: options{defaultCancel: true}},
{name: "WindowIcon", args: WindowIcon(ErrorIcon), want: options{windowIcon: ErrorIcon}}, {name: "WindowIcon", args: WindowIcon(ErrorIcon), want: options{windowIcon: ErrorIcon}},
{name: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}}, {name: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}},