repos / pico

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

pico / pkg / tui
Eric Bower  ·  2025-12-18

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) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) {
43	return nil, nil
44}
45
46func (m *TextInput) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
47	txt := text.New("> ")
48	if m.focus {
49		txt.Style = vaxis.Style{Foreground: oj}
50	} else {
51		txt.Style = vaxis.Style{Foreground: purp}
52	}
53	txtSurf, _ := txt.Draw(ctx)
54	inputSurf, _ := m.input.Draw(ctx)
55	stack := NewGroupStack([]vxfw.Surface{
56		txtSurf,
57		inputSurf,
58	})
59	stack.Direction = "horizontal"
60	brd := NewBorder(stack)
61	brd.Label = m.label
62	if m.focus {
63		brd.Style = vaxis.Style{Foreground: oj}
64	} else {
65		brd.Style = vaxis.Style{Foreground: purp}
66	}
67	surf, _ := brd.Draw(ctx)
68	return surf, nil
69}