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.
func DefaultDate(year int, month time.Month, day int) Option {
return funcOption(func(o *options) {
t := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
o.time = &t
o.time = ptr(time.Date(year, month, day, 0, 0, 0, 0, time.Local))
})
}

View File

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

View File

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

View File

@ -9,13 +9,13 @@ import (
func entry(text string, opts options) (string, error) {
if opts.title == nil {
opts.title = stringPtr("")
opts.title = ptr("")
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
opts.okLabel = ptr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
opts.cancelLabel = ptr("Cancel")
}
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) {
if opts.title == nil {
opts.title = stringPtr("")
opts.title = ptr("")
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
opts.okLabel = ptr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
opts.cancelLabel = ptr("Cancel")
}
dlg := &listDialog{

View File

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

View File

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

View File

@ -16,13 +16,13 @@ func password(opts options) (string, string, error) {
}
if opts.title == nil {
opts.title = stringPtr("")
opts.title = ptr("")
}
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
opts.okLabel = ptr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
opts.cancelLabel = ptr("Cancel")
}
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.okLabel = stringPtr("OK")
opts.okLabel = ptr("OK")
}
if okcancel {
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
opts.cancelLabel = ptr("Cancel")
}
if opts.extraButton == nil {
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) {
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
opts.okLabel = ptr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
opts.cancelLabel = ptr("Cancel")
}
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) {
got := appendGeneral(nil, options{
title: stringPtr("Title"),
title: ptr("Title"),
attach: 12345,
modal: true,
})
@ -48,9 +48,9 @@ func Test_appendButtons(t *testing.T) {
opts options
want []string
}{
{name: "OK", opts: options{okLabel: stringPtr("OK")}, want: []string{"--ok-label", "OK"}},
{name: "Cancel", opts: options{cancelLabel: stringPtr("Cancel")}, want: []string{"--cancel-label", "Cancel"}},
{name: "Extra", opts: options{extraButton: stringPtr("Extra")}, want: []string{"--extra-button", "Extra"}},
{name: "OK", opts: options{okLabel: ptr("OK")}, want: []string{"--ok-label", "OK"}},
{name: "Cancel", opts: options{cancelLabel: ptr("Cancel")}, want: []string{"--cancel-label", "Cancel"}},
{name: "Extra", opts: options{extraButton: ptr("Extra")}, want: []string{"--extra-button", "Extra"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -18,7 +18,7 @@ import (
"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,
// or window functions are used to close the dialog.

View File

@ -18,12 +18,12 @@ func Test_applyOptions(t *testing.T) {
want 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: "Height", args: Height(100), want: options{height: 100}},
{name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: stringPtr("OK")}},
{name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: stringPtr("Cancel")}},
{name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: stringPtr("Extra")}},
{name: "OKLabel", args: OKLabel("OK"), want: options{okLabel: ptr("OK")}},
{name: "CancelLabel", args: CancelLabel("Cancel"), want: options{cancelLabel: ptr("Cancel")}},
{name: "ExtraButton", args: ExtraButton("Extra"), want: options{extraButton: ptr("Extra")}},
{name: "DefaultCancel", args: DefaultCancel(), want: options{defaultCancel: true}},
{name: "WindowIcon", args: WindowIcon(ErrorIcon), want: options{windowIcon: ErrorIcon}},
{name: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}},