From 9816e864ae6ac0e035628aabcb6ea51392f47524 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 19 Dec 2022 19:49:05 +0000 Subject: [PATCH] Improve compatibility with zenity. --- cmd/zenity/main.go | 13 +++++++++++++ list.go | 6 ++++++ list_unix.go | 3 +++ util.go | 9 +++++++++ util_test.go | 18 ++++++++++++++---- zenity.go | 22 ++++++++++++++++++++++ zenity_test.go | 3 +++ 7 files changed, 70 insertions(+), 4 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 725cd41..5ae76b3 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -54,6 +54,9 @@ var ( windowIcon string attach string modal bool + display string + class string + name string multiple bool defaultCancel bool @@ -73,6 +76,7 @@ var ( columns int checklist bool radiolist bool + midSearch bool disallowEmpty bool // Calendar options @@ -221,6 +225,9 @@ func parseFlags() []string { fset.StringVar(&windowIcon, "window-icon", "", "Set the window `icon` (error, info, question, warning)") fset.StringVar(&attach, "attach", "", "Set the parent `window` to attach to") fset.BoolVar(&modal, "modal", runtime.GOOS == "darwin", "Set the modal hint") + fset.StringVar(&display, "display", "", "X `display` to use (Unix only)") + fset.StringVar(&class, "class", "", "Program `class` as used by the window manager (Unix only)") + fset.StringVar(&name, "name", "", "Program `name` as used by the window manager (Unix only)") fset.BoolVar(&multiple, "multiple", false, "Allow multiple items to be selected") fset.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default") @@ -242,6 +249,7 @@ func parseFlags() []string { fset.Bool("hide-header", true, "Hide the column headers") fset.BoolVar(&checklist, "checklist", false, "Use check boxes for the first column (Unix only)") fset.BoolVar(&radiolist, "radiolist", false, "Use radio buttons for the first column (Unix only)") + fset.BoolVar(&midSearch, "mid-search", false, "Change list search to find text in the middle, not on the beginning (Unix only)") fset.BoolVar(&disallowEmpty, "disallow-empty", false, "Disallow empty selection (Windows and macOS only)") // Calendar options @@ -501,6 +509,8 @@ func loadFlags() []zenity.Option { if modal { opts = append(opts, zenity.Modal()) } + opts = append(opts, zenity.Display(display)) + opts = append(opts, zenity.ClassHint(name, class)) // Message options @@ -538,6 +548,9 @@ func loadFlags() []zenity.Option { if radiolist { opts = append(opts, zenity.RadioList()) } + if midSearch { + opts = append(opts, zenity.MidSearch()) + } if disallowEmpty { opts = append(opts, zenity.DisallowEmpty()) } diff --git a/list.go b/list.go index dd06cd0..25a5756 100644 --- a/list.go +++ b/list.go @@ -52,6 +52,12 @@ const ( radioListKind ) +// MidSearch returns an Option to change list search to find text in the middle, +// not on the beginning (Unix only). +func MidSearch() Option { + return funcOption(func(o *options) { o.midSearch = true }) +} + // DefaultItems returns an Option to set the items to initially select (Windows and macOS only). func DefaultItems(items ...string) Option { return funcOption(func(o *options) { o.defaultItems = items }) diff --git a/list_unix.go b/list_unix.go index 20c9194..544a7a8 100644 --- a/list_unix.go +++ b/list_unix.go @@ -19,6 +19,9 @@ func list(text string, items []string, opts options) (string, error) { args = append(args, "--column=") args = append(args, items...) } + if opts.midSearch { + args = append(args, "--mid-search") + } out, err := zenutil.Run(opts.ctx, args) return strResult(opts, out, err) diff --git a/util.go b/util.go index 4084bf7..efb5a89 100644 --- a/util.go +++ b/util.go @@ -24,6 +24,15 @@ func appendGeneral(args []string, opts options) []string { if opts.modal { args = append(args, "--modal") } + if opts.display != "" { + args = append(args, "--display", opts.display) + } + if opts.class != "" { + args = append(args, "--class", opts.class) + } + if opts.name != "" { + args = append(args, "--name", opts.name) + } return args } diff --git a/util_test.go b/util_test.go index 0fea195..65ddb64 100644 --- a/util_test.go +++ b/util_test.go @@ -34,11 +34,21 @@ func Test_quoteAccelerators(t *testing.T) { func Test_appendGeneral(t *testing.T) { t.Parallel() got := appendGeneral(nil, options{ - title: ptr("Title"), - attach: 12345, - modal: true, + title: ptr("Title"), + attach: 12345, + modal: true, + display: ":1", + class: "Class", + name: "Name", }) - want := []string{"--title", "Title", "--attach", "12345", "--modal"} + want := []string{ + "--title", "Title", + "--attach", "12345", + "--modal", + "--display", ":1", + "--class", "Class", + "--name", "Name", + } if !reflect.DeepEqual(got, want) { t.Errorf("appendTitle() = %v; want %v", got, want) } diff --git a/zenity.go b/zenity.go index ed506fa..6ab618e 100644 --- a/zenity.go +++ b/zenity.go @@ -49,6 +49,9 @@ type options struct { windowIcon any attach any modal bool + display string + class string + name string // Message options noWrap bool @@ -61,6 +64,7 @@ type options struct { // List options listKind listKind + midSearch bool disallowEmpty bool defaultItems []string @@ -203,6 +207,24 @@ func Modal() Option { return funcOption(func(o *options) { o.modal = true }) } +// Display returns an Option to set the X display to use (Unix only). +func Display(display string) Option { + return funcOption(func(o *options) { o.display = display }) +} + +// ClassHint returns an Option to set the program name and class +// as used by the window manager (Unix only). +func ClassHint(name, class string) Option { + return funcOption(func(o *options) { + if name != "" { + o.name = name + } + if class != "" { + o.class = class + } + }) +} + // Context returns an Option to set a Context that can dismiss the dialog. // // Dialogs dismissed by ctx return ctx.Err(). diff --git a/zenity_test.go b/zenity_test.go index 5daec16..610a869 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -32,6 +32,8 @@ func Test_applyOptions(t *testing.T) { {name: "Icon", args: Icon("error"), want: options{icon: "error"}}, {name: "Modal", args: Modal(), want: options{modal: true}}, {name: "Attach", args: Attach(zencmd.ParseWindowId("12345")), want: options{attach: zencmd.ParseWindowId("12345")}}, + {name: "Display", args: Display(":1"), want: options{display: ":1"}}, + {name: "ClassHint", args: ClassHint("Name", "Class"), want: options{name: "Name", class: "Class"}}, // Message options {name: "NoWrap", args: NoWrap(), want: options{noWrap: true}}, @@ -45,6 +47,7 @@ func Test_applyOptions(t *testing.T) { // List options {name: "CheckList", args: CheckList(), want: options{listKind: checkListKind}}, {name: "RadioList", args: RadioList(), want: options{listKind: radioListKind}}, + {name: "MidSearch", args: MidSearch(), want: options{midSearch: true}}, {name: "DisallowEmpty", args: DisallowEmpty(), want: options{disallowEmpty: true}}, {name: "DefaultItems", args: DefaultItems("a", "b"), want: options{defaultItems: []string{"a", "b"}}},