ui

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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

type Confirm struct {
	Prompt  string
	Default bool
}

Confirm collects a boolean choice from the user.

func NewConfirm

func NewConfirm(prompt string, def bool) *Confirm

NewConfirm creates a Confirm component.

func (*Confirm) Run

func (c *Confirm) Run(ctx context.Context, be backend.Backend) (bool, error)

Run uses the default process stdin/stdout streams.

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

func (c *Confirm) ValidateConfig() error

ValidateConfig checks whether the component has enough data to run.

type Input

type Input struct {
	Prompt   string
	Default  string
	Validate func(string) error
}

Input collects a text value from the user.

func NewInput

func NewInput(prompt string) *Input

NewInput creates an Input component.

func (*Input) Run

func (c *Input) Run(ctx context.Context, be backend.Backend) (string, error)

Run uses the default process stdin/stdout streams.

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

func (c *Input) ValidateConfig() error

ValidateConfig checks whether the component has enough data to run.

type Item

type Item struct {
	Key      string
	Label    string
	Value    any
	Disabled bool
}

Item is a selectable option.

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) Run

func (c *MultiSelect) Run(ctx context.Context, be backend.Backend) (*Result, error)

Run uses the default process stdin/stdout streams.

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 Result

type Result struct {
	Key    string
	Keys   []string
	Value  any
	Values []any
}

Result is a common selection result.

type Runner

type Runner[T any] interface {
	Run(ctx context.Context, be backend.Backend) (T, error)
}

Runner is implemented by interactive components that return a value.

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 NewSelect

func NewSelect(prompt string, items []Item) *Select

NewSelect creates a Select component.

func (*Select) Run

func (c *Select) Run(ctx context.Context, be backend.Backend) (*Result, error)

Run uses the default process stdin/stdout streams.

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

func (c *Select) ValidateConfig() error

ValidateConfig checks whether the component has enough data to run.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL