repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / tui
Eric Bower  ·  2026-01-16

input.go

 1package tui
 2
 3import (
 4	"git.sr.ht/~rockorager/vaxis"
 5	"git.sr.ht/~rockorager/vaxis/vxfw"
 6	"git.sr.ht/~rockorager/vaxis/vxfw/text"
 7	"git.sr.ht/~rockorager/vaxis/vxfw/textfield"
 8)
 9
10type TextInput struct {
11	input *textfield.TextField
12	label string
13	focus bool
14}
15
16func (m *TextInput) GetValue() string {
17	return m.input.Value
18}
19
20func (m *TextInput) Reset() {
21	m.input.Reset()
22}
23
24func NewTextInput(label string) *TextInput {
25	input := textfield.New()
26	return &TextInput{
27		label: label,
28		input: input,
29	}
30}
31
32func (m *TextInput) FocusIn() (vxfw.Command, error) {
33	m.focus = true
34	return vxfw.FocusWidgetCmd(m.input), nil
35}
36
37func (m *TextInput) FocusOut() (vxfw.Command, error) {
38	m.focus = false
39	return vxfw.RedrawCmd{}, nil
40}
41
42func (m *TextInput) Focused() bool {
43	return m.focus
44}
45
46func (m *TextInput) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) {
47	return nil, nil
48}
49
50func (m *TextInput) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
51	txt := text.New("> ")
52	if m.focus {
53		txt.Style = vaxis.Style{Foreground: oj}
54	} else {
55		txt.Style = vaxis.Style{Foreground: purp}
56	}
57	txtSurf, _ := txt.Draw(ctx)
58	inputSurf, _ := m.input.Draw(ctx)
59	stack := NewGroupStack([]vxfw.Surface{
60		txtSurf,
61		inputSurf,
62	})
63	stack.Direction = "horizontal"
64	brd := NewBorder(stack)
65	brd.Label = m.label
66	if m.focus {
67		brd.Style = vaxis.Style{Foreground: oj}
68	} else {
69		brd.Style = vaxis.Style{Foreground: purp}
70	}
71	surf, _ := brd.Draw(ctx)
72	return surf, nil
73}