Check and radio lists, see #34.

This commit is contained in:
Nuno Cruces 2022-07-29 00:16:15 +00:00 committed by Nuno Cruces
parent 60a828af0a
commit 44ba26c6f5
6 changed files with 50 additions and 15 deletions

22
list.go
View file

@ -3,7 +3,7 @@ package zenity
// List displays the list dialog. // List displays the list dialog.
// //
// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. // WindowIcon, Attach, Modal, RadioList, DefaultItems, DisallowEmpty.
// //
// May return: ErrCanceled, ErrExtraButton, ErrUnsupported. // May return: ErrCanceled, ErrExtraButton, ErrUnsupported.
func List(text string, items []string, options ...Option) (string, error) { 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. // ListMultiple displays the list dialog, allowing multiple items to be selected.
// //
// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. // WindowIcon, Attach, Modal, CheckList, DefaultItems, DisallowEmpty.
// //
// May return: ErrCanceled, ErrExtraButton, ErrUnsupported. // May return: ErrCanceled, ErrExtraButton, ErrUnsupported.
func ListMultiple(text string, items []string, options ...Option) ([]string, error) { 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) 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). // DefaultItems returns an Option to set the items to initially select (macOS only).
func DefaultItems(items ...string) Option { func DefaultItems(items ...string) Option {
return funcOption(func(o *options) { o.defaultItems = items }) return funcOption(func(o *options) { o.defaultItems = items })

View file

@ -5,24 +5,40 @@ package zenity
import "github.com/ncruces/zenity/internal/zenutil" import "github.com/ncruces/zenity/internal/zenutil"
func list(text string, items []string, opts options) (string, error) { 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 = appendGeneral(args, opts)
args = appendButtons(args, opts) args = appendButtons(args, opts)
args = appendWidthHeight(args, opts) args = appendWidthHeight(args, opts)
args = appendWindowIcon(args, opts) args = appendWindowIcon(args, opts)
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...) args = append(args, items...)
}
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
return strResult(opts, out, err) return strResult(opts, out, err)
} }
func listMultiple(text string, items []string, opts options) ([]string, error) { 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 = appendGeneral(args, opts)
args = appendButtons(args, opts) args = appendButtons(args, opts)
args = appendWidthHeight(args, opts) args = appendWidthHeight(args, opts)
args = appendWindowIcon(args, opts) args = appendWindowIcon(args, opts)
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...) args = append(args, items...)
}
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
return lstResult(opts, out, err) return lstResult(opts, out, err)

View file

@ -57,6 +57,7 @@ type options struct {
username bool username bool
// List options // List options
listKind listKind
disallowEmpty bool disallowEmpty bool
defaultItems []string defaultItems []string