From df14a314e43ab9421544c410f2e276ca69a41d84 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 15 Dec 2022 00:26:34 +0000 Subject: [PATCH] Use ptr. --- date.go | 3 +-- date_darwin.go | 3 +-- date_windows.go | 6 +++--- entry_windows.go | 6 +++--- list_windows.go | 6 +++--- notify_windows.go | 2 +- progress_windows.go | 6 +++--- pwd_windows.go | 6 +++--- util_darwin.go | 8 ++++---- util_test.go | 8 ++++---- zenity.go | 2 +- zenity_test.go | 8 ++++---- 12 files changed, 31 insertions(+), 33 deletions(-) diff --git a/date.go b/date.go index 04ca536..1cf99ba 100644 --- a/date.go +++ b/date.go @@ -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)) }) } diff --git a/date_darwin.go b/date_darwin.go index 2c9954e..9ec1aa6 100644 --- a/date_darwin.go +++ b/date_darwin.go @@ -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 { diff --git a/date_windows.go b/date_windows.go index 9ecc876..40fdf06 100644 --- a/date_windows.go +++ b/date_windows.go @@ -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{} diff --git a/entry_windows.go b/entry_windows.go index a21bc98..6b10dc2 100644 --- a/entry_windows.go +++ b/entry_windows.go @@ -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{} diff --git a/list_windows.go b/list_windows.go index b2f0be4..605b426 100644 --- a/list_windows.go +++ b/list_windows.go @@ -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{ diff --git a/notify_windows.go b/notify_windows.go index 2dde8e1..fc1d766 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -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 diff --git a/progress_windows.go b/progress_windows.go index 61ff704..3e967d3 100644 --- a/progress_windows.go +++ b/progress_windows.go @@ -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 diff --git a/pwd_windows.go b/pwd_windows.go index ac003b9..58b7d6c 100644 --- a/pwd_windows.go +++ b/pwd_windows.go @@ -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{} diff --git a/util_darwin.go b/util_darwin.go index f0318d2..89dfc99 100644 --- a/util_darwin.go +++ b/util_darwin.go @@ -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 } diff --git a/util_test.go b/util_test.go index 861dd58..2a8f7ab 100644 --- a/util_test.go +++ b/util_test.go @@ -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) { diff --git a/zenity.go b/zenity.go index 63ad290..ed506fa 100644 --- a/zenity.go +++ b/zenity.go @@ -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. diff --git a/zenity_test.go b/zenity_test.go index 8eb5d14..20802d1 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -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"}},