taginput

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 10 Imported by: ? 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 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

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

func (m Model) Bindings() []keymap.Binding

Bindings returns the tag actions followed by the editing actions of Input, with descriptions, for help text.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) SetTheme(t theme.Theme) Model

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

func (m Model) Tokens() theme.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

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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.

func (Model) View

func (m Model) View() string

View renders each committed tag via widgets.Tag, space-separated, followed by the live Input field.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

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