Documentation
¶
Overview ¶
Package taginput is a small composite widget for entering a list of short tags: a textinput.Model for typing plus a []string of committed tags, rendered as widgets.Tag chips followed by the live input field.
Example ¶
Typing then Enter commits a tag.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/taginput"
)
func main() {
m := taginput.New()
m.Input.Focus()
for _, word := range []string{"go", "tui"} {
for _, r := range word {
m, _ = m.Update(tui.Key{Type: tui.KeyRunes, Text: string([]rune{r})})
}
m, _ = m.Update(tui.Key{Type: tui.KeyEnter})
}
fmt.Println(m.Tags)
}
Output: [go tui]
Index ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyMap ¶
type KeyMap struct {
Commit keymap.Binding // add the typed text as a tag
RemoveLast keymap.Binding // with empty input, remove the last tag
}
KeyMap names the keys of the tag actions of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the keys a Model used before KeyMap existed.
type Model ¶
type Model struct {
Input textinput.Model
Tags []string
MaxTags int
Theme theme.Theme
Variant widgets.Variant
// Mouse, when true, makes Update handle tui.MouseEvent: a left click on the
// input part of the row moves the cursor and a drag selects, as in
// textinput. Clicks on the tag chips do nothing. Off (the default) ignores
// the mouse.
Mouse bool
// Bounds is the screen rectangle of the row the app draws the Model on (tag
// chips first, then the input). Update derives Input's own Bounds from it.
Bounds hittest.Rect
// KeyMap holds the keys for committing and removing tags. New fills it
// with DefaultKeyMap; a Model built as a struct literal with a zero
// KeyMap behaves as if it held DefaultKeyMap. Keys it does not name go
// to Input, whose own KeyMap governs editing.
KeyMap KeyMap
// contains filtered or unexported fields
}
Model holds an embedded textinput.Model for the in-progress value and the already-committed Tags. MaxTags, when positive, caps how many tags Enter will add (0 means unlimited).
func New ¶
func New() Model
New returns a Model with textinput's default styling and theme.DarkTheme().
func (Model) Bindings ¶
Bindings returns the tag actions followed by the editing actions of Input, with descriptions, for help text.
func (Model) LayoutNode ¶
LayoutNode adapts the field to a layout.Node. Measure reports its natural size (the tags and the input on one row). Render fits the allotted Size: the tags keep their room and the input scrolls its value around the cursor in whatever width is left, all cut to the width. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the tag list and the input as plain text for accessible output (see tui.Linearizer): a first line naming the tags and how many there are ("Tags: go, tui (2 tags)", or "2 of 5 tags" with MaxTags, or "No tags"), then the embedded input's own line.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentTagInput, ...) and then by WithTokens.
func (Model) Update ¶
Update handles Enter (commit the current input value as a new tag, unless MaxTags is already reached) and Backspace on an empty input (pop the last tag). With Mouse on, a tui.MouseEvent goes to Input (see Bounds). Every other Msg is forwarded to Input.Update.