otpinput

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: 9 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 otpinput is a field for a short code typed one character to a cell, such as a one-time password: each character fills a cell and moves to the next, Backspace steps back, and a paste fills as many cells as it has characters.

Stability: experimental. Its API may change in any minor release.

Example

A paste fills as many cells as it has digits.

package main

import (
	"fmt"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/otpinput"
)

func main() {
	code := otpinput.New(6)
	code.Group = 3
	code.Focus()
	code, _ = code.Update(tui.PasteEvent{Text: "4821"})
	fmt.Println(ansi.StripANSI(code.View()))
	fmt.Println(code.Value(), code.Complete())
}
Output:
[4][8][2] — [1]< >[ ]
4821 false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangedMsg

type ChangedMsg struct {
	// ID is the field's ID.
	ID string
	// Value is the code so far: the filled cells in order, without the
	// empty ones.
	Value string
	// Complete reports whether every cell is filled.
	Complete bool
}

ChangedMsg is delivered (via the Cmd Update returns) when the code changes.

type KeyMap

type KeyMap struct {
	Back  keymap.Binding // empty the cell under the cursor, or the one before it
	Prev  keymap.Binding // move to the cell before
	Next  keymap.Binding // move to the cell after
	First keymap.Binding // move to the first cell
	Last  keymap.Binding // move to the last cell
	Clear keymap.Binding // empty every cell
}

KeyMap names the keys of each action of a Model. A character the field accepts always fills the cell under the cursor and is not in the KeyMap.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Backspace, Left and Right, Home and End, and Ctrl+U.

type Model

type Model struct {
	// ID is carried by ChangedMsg, to tell one field's change from
	// another's.
	ID string
	// Length is the number of cells. A Length under 1 is read as 6.
	Length int
	// Group, when above 0, draws a dash between every Group cells:
	// "[1][2][3] - [4][5][6]" for a Length of 6 and a Group of 3.
	Group int
	// Accept decides which characters a cell takes. Nil takes the digits 0
	// to 9. Use unicode.IsLetter, or a function of your own, for other
	// codes.
	Accept func(rune) bool
	// Upper turns a typed letter into its capital.
	Upper bool
	// Mask draws a filled cell with the theme's mask glyph, not its
	// character.
	Mask bool
	// Disabled stops the code being changed; it is drawn faint.
	Disabled bool
	Theme    theme.Theme

	// KeyMap holds the keys for each action. New fills it with DefaultKeyMap;
	// a Model built as a struct literal with a zero KeyMap behaves as if it
	// held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
	// a cell moves the cursor to it. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the field.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is one code field. The zero value is not ready to use; build one with New.

func New

func New(length int) Model

New returns a field of length cells that takes digits.

func (Model) Bindings

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

Bindings returns the actions the field honours, with descriptions, for help text. A disabled field has none.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (Model) Complete

func (m Model) Complete() bool

Complete reports whether every cell is filled.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the cell the cursor is on.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

Focus gives the field keyboard focus. It returns no Cmd; the result is there so a field can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the field has keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the field to a layout.Node of its own width on one row. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the field as plain text for accessible output (see tui.Linearizer): "code field, 3 of 6 entered", then the characters unless Mask is set, and ", unavailable" when disabled.

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

func (m *Model) SetValue(s string)

SetValue empties the field and fills it from the start with the characters of s that it accepts, as a paste does, without a key press: no ChangedMsg is delivered. The cursor is left after the last one filled.

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.ComponentOTPInput, ...) and then by WithTokens.

func (Model) Update

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

Update fills the cell under the cursor with a typed character the field accepts and moves to the next; a pasted text fills as many cells as it has accepted characters. Backspace empties the cell under the cursor, or the one before it when that one is already empty. Left, Right, Home and End move the cursor, and Ctrl+U empties the field. With Mouse on, a left press on a cell moves the cursor to it. Only a field that has focus acts on keys, and a disabled one acts on nothing.

func (Model) Value

func (m Model) Value() string

Value returns the code so far: the filled cells in order, without the empty ones.

func (Model) View

func (m Model) View() string

View renders each cell as its character in square brackets, "[4]", and an empty one as "[ ]", so what has been typed reads without colour. The cell under the cursor of a field that has focus is drawn in angle brackets, "<4>" or "< >", as the other controls show focus. A disabled field is faint.

func (Model) Width

func (m Model) Width() int

Width is the number of cells of the screen View takes: three for each cell of the code, and three for each separator.

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