-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-input.go
More file actions
181 lines (155 loc) · 4.18 KB
/
Copy pathchat-input.go
File metadata and controls
181 lines (155 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package chatinput renders a single-line chat prompt with a placeholder,
// cursor, and submit/escape key bindings. It's a thin Bubble Tea model — the
// consumer owns the code and is expected to extend it (history, multi-line,
// slash commands, completions) by editing the file directly.
package chatinput
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/truffle-dev/glyph/components/theme"
)
// SubmitMsg is emitted when the user presses Enter on a non-empty value.
type SubmitMsg struct {
Value string
}
// CancelMsg is emitted when the user presses Esc.
type CancelMsg struct{}
// Input is a Bubble Tea model for a chat-style single-line text input.
type Input struct {
theme theme.Theme
value string
placeholder string
prompt string
width int
focused bool
}
// New constructs an Input using the given theme. Focused by default.
func New(t theme.Theme) Input {
return Input{
theme: t,
prompt: "> ",
width: 60,
focused: true,
}
}
// WithPlaceholder sets the placeholder shown when the value is empty.
func (i Input) WithPlaceholder(s string) Input { i.placeholder = s; return i }
// WithPrompt sets the prompt prefix (default "> "). Use "" for no prompt.
func (i Input) WithPrompt(s string) Input { i.prompt = s; return i }
// WithWidth sets the overall input width in cells. Clamped to >= 8.
func (i Input) WithWidth(w int) Input {
if w < 8 {
w = 8
}
i.width = w
return i
}
// WithValue presets the value. Useful for restored sessions.
func (i Input) WithValue(s string) Input { i.value = s; return i }
// Focus enables key input.
func (i Input) Focus() Input { i.focused = true; return i }
// Blur disables key input.
func (i Input) Blur() Input { i.focused = false; return i }
// Focused reports whether the input is currently accepting keys.
func (i Input) Focused() bool { return i.focused }
// Value returns the current text.
func (i Input) Value() string { return i.value }
// Reset clears the value to "".
func (i Input) Reset() Input { i.value = ""; return i }
// Init implements tea.Model.
func (i Input) Init() tea.Cmd { return nil }
// Update implements tea.Model.
func (i Input) Update(msg tea.Msg) (Input, tea.Cmd) {
if !i.focused {
return i, nil
}
key, ok := msg.(tea.KeyMsg)
if !ok {
return i, nil
}
switch key.Type {
case tea.KeyEnter:
v := strings.TrimRight(i.value, " ")
if v == "" {
return i, nil
}
out := i.value
i.value = ""
return i, func() tea.Msg { return SubmitMsg{Value: out} }
case tea.KeyEsc:
return i, func() tea.Msg { return CancelMsg{} }
case tea.KeyBackspace:
if len(i.value) > 0 {
r := []rune(i.value)
i.value = string(r[:len(r)-1])
}
return i, nil
case tea.KeyCtrlU:
i.value = ""
return i, nil
case tea.KeySpace:
i.value += " "
return i, nil
case tea.KeyRunes:
i.value += string(key.Runes)
return i, nil
}
return i, nil
}
// View renders the input. Safe to call repeatedly.
func (i Input) View() string {
prompt := lipgloss.NewStyle().Foreground(i.theme.Primary).Render(i.prompt)
contentWidth := i.width - lipgloss.Width(prompt) - 2 // 2 for border padding
if contentWidth < 4 {
contentWidth = 4
}
var body string
if i.value == "" {
body = lipgloss.NewStyle().
Foreground(i.theme.TextMuted).
Render(truncate(i.placeholder, contentWidth))
} else {
visible := i.value
if lipgloss.Width(visible) > contentWidth-1 {
r := []rune(visible)
start := len(r) - (contentWidth - 1)
if start < 0 {
start = 0
}
visible = string(r[start:])
}
body = lipgloss.NewStyle().Foreground(i.theme.Text).Render(visible)
}
cursor := ""
if i.focused {
cursor = lipgloss.NewStyle().
Background(i.theme.Text).
Foreground(i.theme.Bg).
Render(" ")
}
line := prompt + body + cursor
border := i.theme.Border
if i.focused {
border = i.theme.PrimaryStrong
}
return lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(border).
Padding(0, 1).
Width(i.width).
Render(line)
}
func truncate(s string, max int) string {
if max <= 0 {
return ""
}
r := []rune(s)
if len(r) <= max {
return s
}
if max < 2 {
return string(r[:max])
}
return string(r[:max-1]) + "…"
}