Documentation
¶
Overview ¶
Package ui defines interactive component models built on top of a backend.
Example (ReadlineFallback) ¶
package main
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/gookit/cliui/interact/backend/readline"
"github.com/gookit/cliui/interact/ui"
)
func main() {
in := bytes.NewBufferString("\n")
out := new(bytes.Buffer)
be := readline.New()
input := ui.NewInput("Your name")
input.Default = "guest"
name, err := input.RunWithIO(context.Background(), be, in, out)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(name)
fmt.Println(strings.Contains(out.String(), "Your name"))
}
Output: guest true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAborted reports that the user aborted the current interaction. ErrAborted = errors.New("interact/ui: aborted") // ErrNoTTY reports that the backend requires a TTY but none is available. ErrNoTTY = errors.New("interact/ui: tty required") // ErrInvalidState reports invalid component configuration or state. ErrInvalidState = errors.New("interact/ui: invalid state") // ErrNotImplemented reports that a concrete backend loop is not implemented yet. ErrNotImplemented = errors.New("interact/ui: not implemented") )
Functions ¶
This section is empty.
Types ¶
type Confirm ¶
Confirm collects a boolean choice from the user.
func NewConfirm ¶
NewConfirm creates a Confirm component.
func (*Confirm) RunWithIO ¶
func (c *Confirm) RunWithIO(ctx context.Context, be backend.Backend, in io.Reader, out io.Writer) (bool, error)
RunWithIO runs the component with explicit IO streams.
func (*Confirm) ValidateConfig ¶
ValidateConfig checks whether the component has enough data to run.
type Input ¶
Input collects a text value from the user.
func (*Input) RunWithIO ¶
func (c *Input) RunWithIO(ctx context.Context, be backend.Backend, in io.Reader, out io.Writer) (string, error)
RunWithIO runs the component with explicit IO streams.
func (*Input) ValidateConfig ¶
ValidateConfig checks whether the component has enough data to run.
type MultiSelect ¶
type MultiSelect struct {
Prompt string
Items []Item
DefaultKeys []string
MinSelected int
Filterable bool
FilterPrompt string
PageSize int
}
MultiSelect collects multiple selected items from the user.
func NewMultiSelect ¶
func NewMultiSelect(prompt string, items []Item) *MultiSelect
NewMultiSelect creates a MultiSelect component.
func (*MultiSelect) RunWithIO ¶
func (c *MultiSelect) RunWithIO(ctx context.Context, be backend.Backend, in io.Reader, out io.Writer) (*Result, error)
RunWithIO runs the component with explicit IO streams.
func (*MultiSelect) ValidateConfig ¶
func (c *MultiSelect) ValidateConfig() error
ValidateConfig checks whether the component has enough data to run.
type Select ¶
type Select struct {
Prompt string
Items []Item
DefaultKey string
Filterable bool
FilterPrompt string
PageSize int
}
Select collects one selected item from the user.
func (*Select) RunWithIO ¶
func (c *Select) RunWithIO(ctx context.Context, be backend.Backend, in io.Reader, out io.Writer) (*Result, error)
RunWithIO runs the component with explicit IO streams.
func (*Select) ValidateConfig ¶
ValidateConfig checks whether the component has enough data to run.