textinput

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 5 Imported by: 0

README

Text Input

Multi-line text input with placeholder, focus, 2D cursor, Ctrl-U kill-to-cursor, Alt+Left/Right word jumps, and Home/End.

text-input preview

Install

glyph add text-input

This copies text-input.go (and its test file) into your repo at the path your glyph.json aliases declare. After install, the file is yours: edit it, refactor it, rename it. There is no text-input library to keep in sync.

Hello, world

package main

import (
	"fmt"

	textinput "github.com/truffle-dev/glyph/components/text-input"
	"github.com/truffle-dev/glyph/components/theme"
)

func main() {
	t := textinput.New(theme.Default).
		WithPlaceholder("Commit message…").
		WithWidth(72).
		WithHeight(6).
		Focus()
	fmt.Println(t.View())
}

Key bindings

  • Enter insert a newline
  • Ctrl-D emit textinput.SubmitMsg{Value}
  • Esc emit textinput.CancelMsg{}
  • Backspace delete the rune before the cursor; join lines when at column 0
  • Ctrl-U kill from start of line to the cursor
  • Ctrl-K kill from the cursor to end of line
  • Alt+Left / Alt+Right jump one word at a time
  • Left / Right / Up / Down 2D cursor movement
  • Home / End snap to line start / end

API surface

Package: textinput

Types

  • SubmitMsg
  • CancelMsg
  • Input

Functions and methods

  • New
  • WithPlaceholder
  • WithWidth
  • WithHeight
  • WithValue
  • Focus
  • Blur
  • Focused
  • Value
  • Cursor
  • Reset
  • Init
  • Update
  • View

Dependencies

  • glyph component theme (installed automatically)
  • github.com/charmbracelet/bubbletea@v1.3.10
  • github.com/charmbracelet/lipgloss@v1.1.0

Notes

Enter inserts a newline, so Ctrl-D is the accept binding. The input emits textinput.SubmitMsg on accept and textinput.CancelMsg on Esc. Pair with panel for a labeled commit-message surface, or with key-hints to advertise the bindings.

See also

License

MIT, same as the rest of glyph.

Documentation

Overview

Package textinput renders a multi-line text input with a 2D cursor, placeholder, focus, word-jumps on Alt+Left/Right, and Ctrl-U to kill the current line up to the cursor. It is a thin Bubble Tea model — the consumer owns the code and is expected to extend it (history, paste handlers, line wrapping, syntax styling) by editing the file directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelMsg

type CancelMsg struct{}

CancelMsg is emitted when the user presses Esc.

type Input

type Input struct {
	// contains filtered or unexported fields
}

Input is a Bubble Tea model for a multi-line text input.

func New

func New(t theme.Theme) Input

New constructs an Input using the given theme. Focused by default, with a single empty line, width 60 cells, and four visible rows.

func (Input) Blur

func (i Input) Blur() Input

Blur disables key input.

func (Input) Cursor

func (i Input) Cursor() (int, int)

Cursor returns the current 0-based (row, col) cursor position.

func (Input) Focus

func (i Input) Focus() Input

Focus enables key input.

func (Input) Focused

func (i Input) Focused() bool

Focused reports whether the input is currently accepting keys.

func (Input) Init

func (i Input) Init() tea.Cmd

Init implements tea.Model.

func (Input) Reset

func (i Input) Reset() Input

Reset clears the value back to a single empty line and homes the cursor.

func (Input) Update

func (i Input) Update(msg tea.Msg) (Input, tea.Cmd)

Update implements tea.Model.

func (Input) Value

func (i Input) Value() string

Value returns the current text with rows joined by "\n".

func (Input) View

func (i Input) View() string

View renders the input. Safe to call repeatedly.

func (Input) WithHeight

func (i Input) WithHeight(h int) Input

WithHeight sets the number of visible rows. Clamped to >= 1.

func (Input) WithPlaceholder

func (i Input) WithPlaceholder(s string) Input

WithPlaceholder sets the placeholder shown when the value is empty.

func (Input) WithValue

func (i Input) WithValue(s string) Input

WithValue presets the value. Newlines are honored; the cursor lands at the end of the last line.

func (Input) WithWidth

func (i Input) WithWidth(w int) Input

WithWidth sets the overall input width in cells. Clamped to >= 8.

type SubmitMsg

type SubmitMsg struct {
	Value string
}

SubmitMsg is emitted when the user presses Ctrl-D on a non-empty value.

Jump to

Keyboard shortcuts

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