Check and radio lists, see #34.
This commit is contained in:
parent
60a828af0a
commit
44ba26c6f5
6 changed files with 50 additions and 15 deletions
22
list.go
22
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 })
|
||||
|
|
20
list_unix.go
20
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)
|
||||
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)
|
||||
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)
|
||||
|
|
|
@ -57,6 +57,7 @@ type options struct {
|
|||
username bool
|
||||
|
||||
// List options
|
||||
listKind listKind
|
||||
disallowEmpty bool
|
||||
defaultItems []string
|
||||
|
||||
|
|
Loading…
Reference in a new issue