From 44ba26c6f5181f6b2fee9a719304c18fa7358249 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 29 Jul 2022 00:16:15 +0000 Subject: [PATCH] Check and radio lists, see #34. --- list.go | 22 ++++++++++++++++++++-- list_unix.go | 24 ++++++++++++++++++++---- zenity.go | 1 + zenity_darwin.go | 6 +++--- zenity_unix.go | 6 +++--- zenity_windows.go | 6 +++--- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/list.go b/list.go index a2d14c8..fd099d2 100644 --- a/list.go +++ b/list.go @@ -3,7 +3,7 @@ package zenity // List displays the list dialog. // // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, -// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. +// WindowIcon, Attach, Modal, RadioList, DefaultItems, DisallowEmpty. // // May return: ErrCanceled, ErrExtraButton, ErrUnsupported. func List(text string, items []string, options ...Option) (string, error) { @@ -20,7 +20,7 @@ func ListItems(text string, items ...string) (string, error) { // ListMultiple displays the list dialog, allowing multiple items to be selected. // // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, -// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. +// WindowIcon, Attach, Modal, CheckList, DefaultItems, DisallowEmpty. // // May return: ErrCanceled, ErrExtraButton, ErrUnsupported. func ListMultiple(text string, items []string, options ...Option) ([]string, error) { @@ -34,6 +34,24 @@ func ListMultipleItems(text string, items ...string) ([]string, error) { return ListMultiple(text, items) } +// CheckList returns an Option to show check boxes (Unix only). +func CheckList() Option { + return funcOption(func(o *options) { o.listKind = checkListKind }) +} + +// RadioList returns an Option to show radio boxes (Unix only). +func RadioList() Option { + return funcOption(func(o *options) { o.listKind = radioListKind }) +} + +type listKind int + +const ( + basicListKind listKind = iota + checkListKind + radioListKind +) + // DefaultItems returns an Option to set the items to initially select (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 7a7e0ae..41845df 100644 --- a/list_unix.go +++ b/list_unix.go @@ -5,24 +5,40 @@ package zenity import "github.com/ncruces/zenity/internal/zenutil" func list(text string, items []string, opts options) (string, error) { - args := []string{"--list", "--column=", "--hide-header", "--text", text} + args := []string{"--list", "--hide-header", "--text", text} args = appendGeneral(args, opts) args = appendButtons(args, opts) args = appendWidthHeight(args, opts) args = appendWindowIcon(args, opts) - args = append(args, items...) + if opts.listKind == radioListKind { + args = append(args, "--radiolist", "--column=", "--column=") + for _, i := range items { + args = append(args, i, i) + } + } else { + args = append(args, "--column=") + args = append(args, items...) + } out, err := zenutil.Run(opts.ctx, args) return strResult(opts, out, err) } func listMultiple(text string, items []string, opts options) ([]string, error) { - args := []string{"--list", "--column=", "--hide-header", "--text", text, "--multiple", "--separator", zenutil.Separator} + args := []string{"--list", "--hide-header", "--text", text, "--multiple", "--separator", zenutil.Separator} args = appendGeneral(args, opts) args = appendButtons(args, opts) args = appendWidthHeight(args, opts) args = appendWindowIcon(args, opts) - args = append(args, items...) + if opts.listKind == checkListKind { + args = append(args, "--checklist", "--column=", "--column=") + for _, i := range items { + args = append(args, i, i) + } + } else { + args = append(args, "--column=") + args = append(args, items...) + } out, err := zenutil.Run(opts.ctx, args) return lstResult(opts, out, err) diff --git a/zenity.go b/zenity.go index ed9c3fc..3c3c808 100644 --- a/zenity.go +++ b/zenity.go @@ -57,6 +57,7 @@ type options struct { username bool // List options + listKind listKind disallowEmpty bool defaultItems []string diff --git a/zenity_darwin.go b/zenity_darwin.go index e4b40c7..7feae2a 100644 --- a/zenity_darwin.go +++ b/zenity_darwin.go @@ -3,9 +3,9 @@ package zenity // Attach returns an Option to set the parent window to attach to. // // Attach accepts: -// - a window id (int) on Unix -// - a window handle (~uintptr) on Windows -// - an application name (string) or process id (int) on macOS +// - a window id (int) on Unix +// - a window handle (~uintptr) on Windows +// - an application name (string) or process id (int) on macOS func Attach(id any) Option { switch id.(type) { case int, string: diff --git a/zenity_unix.go b/zenity_unix.go index 45fd72b..677fc04 100644 --- a/zenity_unix.go +++ b/zenity_unix.go @@ -5,9 +5,9 @@ package zenity // Attach returns an Option to set the parent window to attach to. // // Attach accepts: -// - a window id (int) on Unix -// - a window handle (~uintptr) on Windows -// - an application name (string) or process id (int) on macOS +// - a window id (int) on Unix +// - a window handle (~uintptr) on Windows +// - an application name (string) or process id (int) on macOS func Attach(id int) Option { return funcOption(func(o *options) { o.attach = id }) } diff --git a/zenity_windows.go b/zenity_windows.go index 0344381..4598ff7 100644 --- a/zenity_windows.go +++ b/zenity_windows.go @@ -9,9 +9,9 @@ import ( // Attach returns an Option to set the parent window to attach to. // // Attach accepts: -// - a window id (int) on Unix -// - a window handle (~uintptr) on Windows -// - an application name (string) or process id (int) on macOS +// - a window id (int) on Unix +// - a window handle (~uintptr) on Windows +// - an application name (string) or process id (int) on macOS func Attach(id any) Option { if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr { id = win.HWND(uintptr(v.Uint()))