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 ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Complete() bool
- func (m Model) Cursor() int
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) SetValue(s string)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) Value() string
- func (m Model) View() string
- func (m Model) Width() int
- 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 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 (Model) Bindings ¶
Bindings returns the actions the field honours, with descriptions, for help text. A disabled field has none.
func (*Model) Focus ¶
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) LayoutNode ¶
LayoutNode adapts the field to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Value returns the code so far: the filled cells in order, without the empty ones.
func (Model) View ¶
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.