form

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package form is the TUI's dialog kit: text fields, text areas, selects, checkboxes, pick lists, read-only notes and buttons. Tab / shift+Tab move between them, ctrl+s or Enter on a button submits, Esc cancels. Every field, list item and button is a hit rectangle, so the root model routes clicks to it through its pure layout.

A form never runs anything: Key and Click return an Event, and the dialog that owns the form builds the CLI arguments from its values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Button

type Button struct {
	ID, Label string
	Cancel    bool
}

Button is a button; Cancel buttons cancel without checks.

type Editor

type Editor struct {
	Value []rune
	Pos   int  // cursor: an index into Value
	Off   int  // first visual row shown
	Multi bool // newlines allowed (Insert keeps them)
}

Editor is a small text editor: runes and a cursor, soft-wrapped at the width it is drawn with. A single-line editor shows the visual row that holds the cursor.

func (*Editor) Click

func (e *Editor) Click(x, y, w int)

Click moves the cursor to the cell (x, y) of a view w cells wide.

func (*Editor) Height

func (e *Editor) Height(w int) int

Height is the number of visual rows at width w.

func (*Editor) Insert

func (e *Editor) Insert(s string)

Insert inserts s at the cursor. A single-line editor turns newlines into spaces; other control characters are dropped.

func (*Editor) Key

func (e *Editor) Key(k tea.KeyPressMsg, w int) bool

Key edits with a key press and reports whether it used it. Newlines are the caller's choice (Insert("\n")); up and down move between visual rows of width w.

func (*Editor) Scroll

func (e *Editor) Scroll(n, w, h int)

Scroll moves the view n visual rows (the cursor stays).

func (*Editor) Set

func (e *Editor) Set(s string)

Set replaces the text and puts the cursor at its end.

func (*Editor) String

func (e *Editor) String() string

String returns the text.

func (*Editor) View

func (e *Editor) View(w, h int, cursor func(string) string, placeholder string, dim func(...string) string) []string

View renders h rows of w cells, scrolled so the cursor shows; cursor draws the cell under it (nil: no cursor). placeholder shows, through dim, when the text is empty.

type Event

type Event int

Event is what a key or click asks of the form's owner.

const (
	None   Event = iota
	Submit       // with the button's ID
	Cancel
)

Events.

type Field

type Field struct {
	Kind     Kind
	ID       string
	Label    string
	Required bool
	Ed       Editor // Text, Area
	Height   int    // Area, List, Note: rows shown
	Opts     []Option
	Sel      int    // Select, List
	On       bool   // Check
	Text     string // Note
	Off      int    // List, Note: first row shown
	Keep     bool   // List: keep a disabled preselection (submitting it says why)
	// Placeholder shows, dim, in an empty Text or Area ("" = a blank line).
	Placeholder string
}

Field is one row (or block of rows) of a form.

func (*Field) Value

func (f *Field) Value() string

Value is the field's value: the text, the chosen option, or "true" / "".

type Form

type Form struct {
	Title   string
	Fields  []*Field
	Buttons []Button
	Focus   int    // index into the focusables: the fields that can focus, then the buttons
	Err     string // a failed check or the CLI's error, shown above the buttons
	Status  string // "working…" while the action runs
}

Form is a dialog's content.

func New

func New(title string, buttons []Button, fields ...*Field) *Form

New returns a form with the focus on its first field.

func (*Form) Click

func (f *Form) Click(h Hit, x, y int, double bool, width int) (Event, string)

Click handles a left click on hit h at the cell (x, y) relative to the form; double is a second click on the same spot.

func (*Form) Field

func (f *Form) Field(id string) *Field

Field returns the field with id (nil: none).

func (*Form) FocusButton

func (f *Form) FocusButton(id string)

FocusButton moves the focus to button id.

func (*Form) FocusField

func (f *Form) FocusField(id string)

FocusField moves the focus to field id.

func (*Form) Get

func (f *Form) Get(id string) string

Get returns the value of field id.

func (*Form) Key

func (f *Form) Key(k tea.KeyPressMsg, width int) (Event, string)

Key handles a key press. Width is the form's content width (Area rows wrap there).

func (*Form) Paste

func (f *Form) Paste(s string)

Paste inserts pasted text into the focused text field.

func (*Form) View

func (f *Form) View(width int, th style.Theme) ([]string, []Hit)

View renders the form width cells wide and returns its lines and hit rectangles.

func (*Form) Wheel

func (f *Form) Wheel(h Hit, n, width int)

Wheel scrolls the List, Note or Area of hit h by n rows.

type Hit

type Hit struct {
	X, Y, W, H int
	Field      int // field index, -1 for a button
	Button     int // button index, -1 for a field
	Opt        int // List: option index; -1 otherwise
}

Hit is a clickable rectangle of the form's view, relative to its top-left cell.

type Kind

type Kind int

Kind is a field kind.

const (
	Text   Kind = iota // one line
	Area               // several lines (Enter is a newline)
	Select             // one of Opts, cycled with ← → or space
	Check              // a checkbox
	List               // a pick list of Opts, Height rows
	Note               // read-only text, Height rows at most
)

Field kinds.

type Option

type Option struct {
	Value, Label, Detail, Disabled string
}

Option is an item of a Select or a List. A disabled one says why and cannot be chosen.

Jump to

Keyboard shortcuts

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