@@ -0,0 +1,302 @@
1+package modal
2+
3+import (
4+ "strings"
5+ "time"
6+
7+ tea "charm.land/bubbletea/v2"
8+ "charm.land/lipgloss/v2"
9+)
10+
11+// Action is the result of handling a message. nil means no action.
12+type Action any
13+
14+// ActionClose signals the modal should be dismissed.
15+type ActionClose struct{}
16+
17+// ActionSelect signals an item was chosen.
18+type ActionSelect struct{ Item Item }
19+
20+// Item is a single entry in the list.
21+type Item struct {
22+ Name string
23+ Desc string
24+}
25+
26+// SearchFunc is called with the current query to produce results.
27+type SearchFunc func(query string) []Item
28+
29+const (
30+ debounceDelay = 250 * time.Millisecond
31+
32+ colorDim = "#666666"
33+ colorSelFg = "#ffffff"
34+ colorSelBg = "#444444"
35+ colorBorder = "#555555"
36+
37+ keyDown = "down"
38+ keyBackspace = "backspace"
39+)
40+
41+// debounceMsg is sent after the debounce timer fires.
42+type debounceMsg struct{ seq int }
43+
44+// Model is a generic input+list modal.
45+type Model struct {
46+ input []rune
47+ cursor int
48+ items []Item
49+ selected int
50+ inList bool // true = list focused, false = input focused
51+
52+ searchFn SearchFunc
53+ seq int // debounce sequence counter
54+
55+ // Prompt shown before the input text.
56+ Prompt string
57+}
58+
59+// New creates a modal with the given search function.
60+func New(searchFn SearchFunc, prompt string) Model {
61+ m := Model{
62+ searchFn: searchFn,
63+ Prompt: prompt,
64+ }
65+ // Initial search with empty query.
66+ m.items = searchFn("")
67+ return m
68+}
69+
70+// DebounceCmd returns a tea.Cmd that fires after the debounce delay.
71+func (m *Model) DebounceCmd() tea.Cmd {
72+ seq := m.seq
73+ return tea.Tick(debounceDelay, func(time.Time) tea.Msg {
74+ return debounceMsg{seq: seq}
75+ })
76+}
77+
78+// HandleMsg processes a tea.Msg and returns an optional Action.
79+// The second return is a tea.Cmd the parent must dispatch (for debounce).
80+func (m *Model) HandleMsg(msg tea.Msg) (Action, tea.Cmd) {
81+ switch msg := msg.(type) {
82+ case tea.KeyPressMsg:
83+ return m.handleKey(msg)
84+ case debounceMsg:
85+ if msg.seq == m.seq {
86+ m.items = m.searchFn(string(m.input))
87+ m.selected = 0
88+ m.inList = false
89+ }
90+ return nil, nil
91+ }
92+ return nil, nil
93+}
94+
95+func (m *Model) handleKey(msg tea.KeyPressMsg) (Action, tea.Cmd) {
96+ switch msg.Keystroke() {
97+ case "esc":
98+ return ActionClose{}, nil
99+ case "enter":
100+ return m.handleEnter()
101+ case "up", keyDown:
102+ m.handleNav(msg.Keystroke())
103+ return nil, nil
104+ case keyBackspace, "delete", "ctrl+u", "ctrl+k":
105+ return nil, m.handleDelete(msg.Keystroke())
106+ case "left", "right", "home", "end", "ctrl+a", "ctrl+e":
107+ m.handleCursor(msg.Keystroke())
108+ return nil, nil
109+ }
110+
111+ // Rune input.
112+ if !m.inList && msg.Text != "" {
113+ for _, r := range msg.Text {
114+ m.input = append(m.input[:m.cursor], append([]rune{r}, m.input[m.cursor:]...)...)
115+ m.cursor++
116+ }
117+ m.seq++
118+ return nil, m.DebounceCmd()
119+ }
120+
121+ return nil, nil
122+}
123+
124+func (m *Model) handleEnter() (Action, tea.Cmd) {
125+ if len(m.items) == 0 {
126+ return nil, nil
127+ }
128+ idx := m.selected
129+ if idx >= len(m.items) {
130+ idx = 0
131+ }
132+ return ActionSelect{Item: m.items[idx]}, nil
133+}
134+
135+func (m *Model) handleNav(key string) {
136+ switch key {
137+ case "up":
138+ if m.inList {
139+ if m.selected > 0 {
140+ m.selected--
141+ } else {
142+ m.inList = false
143+ }
144+ }
145+ case keyDown:
146+ if !m.inList {
147+ if len(m.items) > 0 {
148+ m.inList = true
149+ m.selected = 0
150+ }
151+ } else if m.selected < len(m.items)-1 {
152+ m.selected++
153+ }
154+ }
155+}
156+
157+func (m *Model) handleDelete(key string) tea.Cmd {
158+ switch key {
159+ case keyBackspace:
160+ if m.cursor > 0 {
161+ m.input = append(m.input[:m.cursor-1], m.input[m.cursor:]...)
162+ m.cursor--
163+ m.seq++
164+ return m.DebounceCmd()
165+ }
166+ case "delete":
167+ if m.cursor < len(m.input) {
168+ m.input = append(m.input[:m.cursor], m.input[m.cursor+1:]...)
169+ m.seq++
170+ return m.DebounceCmd()
171+ }
172+ case "ctrl+u":
173+ m.input = m.input[m.cursor:]
174+ m.cursor = 0
175+ m.seq++
176+ return m.DebounceCmd()
177+ case "ctrl+k":
178+ m.input = m.input[:m.cursor]
179+ m.seq++
180+ return m.DebounceCmd()
181+ }
182+ return nil
183+}
184+
185+func (m *Model) handleCursor(key string) {
186+ if m.inList {
187+ return
188+ }
189+ switch key {
190+ case "left":
191+ if m.cursor > 0 {
192+ m.cursor--
193+ }
194+ case "right":
195+ if m.cursor < len(m.input) {
196+ m.cursor++
197+ }
198+ case "home", "ctrl+a":
199+ m.cursor = 0
200+ case "end", "ctrl+e":
201+ m.cursor = len(m.input)
202+ }
203+}
204+
205+// View renders the modal at the given app width and height.
206+func (m *Model) View(appWidth, appHeight int) string {
207+ w := appWidth * 80 / 100
208+ h := appHeight * 80 / 100
209+ if w < 30 {
210+ w = 30
211+ }
212+ if h < 8 {
213+ h = 8
214+ }
215+
216+ innerW := w - 6 // border + padding
217+ if innerW < 10 {
218+ innerW = 10
219+ }
220+
221+ prompt := m.Prompt
222+ if prompt == "" {
223+ prompt = "> "
224+ }
225+
226+ inputLine := m.renderInput(prompt)
227+ listHeight := h - 4 // border top/bottom + input + divider
228+ if listHeight < 1 {
229+ listHeight = 1
230+ }
231+
232+ dimStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(colorDim))
233+ divider := dimStyle.Render(strings.Repeat("\u2500", innerW))
234+ listLines := m.renderList(innerW, listHeight)
235+
236+ content := inputLine + "\n" + divider
237+ for _, l := range listLines {
238+ content += "\n" + l
239+ }
240+
241+ box := lipgloss.NewStyle().
242+ Border(lipgloss.RoundedBorder()).
243+ BorderForeground(lipgloss.Color(colorBorder)).
244+ Padding(0, 1).
245+ Width(w - 2).
246+ Render(content)
247+
248+ return lipgloss.Place(appWidth, appHeight, lipgloss.Center, lipgloss.Center, box)
249+}
250+
251+func (m *Model) renderInput(prompt string) string {
252+ if m.inList {
253+ return prompt + string(m.input)
254+ }
255+ before := string(m.input[:m.cursor])
256+ cursorStyle := lipgloss.NewStyle().Reverse(true)
257+ cursorChar := " "
258+ after := ""
259+ if m.cursor < len(m.input) {
260+ cursorChar = string(m.input[m.cursor])
261+ after = string(m.input[m.cursor+1:])
262+ }
263+ return prompt + before + cursorStyle.Render(cursorChar) + after
264+}
265+
266+func (m *Model) renderList(innerW, listHeight int) []string {
267+ scrollOff := 0
268+ if m.selected >= listHeight {
269+ scrollOff = m.selected - listHeight + 1
270+ }
271+
272+ dimStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(colorDim))
273+ selStyle := lipgloss.NewStyle().
274+ Foreground(lipgloss.Color(colorSelFg)).
275+ Background(lipgloss.Color(colorSelBg))
276+
277+ var lines []string
278+ for i := scrollOff; i < len(m.items) && len(lines) < listHeight; i++ {
279+ item := m.items[i]
280+ if i == m.selected && m.inList {
281+ lines = append(lines, selStyle.Render(padRight(item.Name, innerW)))
282+ } else {
283+ line := item.Name
284+ if item.Desc != "" {
285+ line += dimStyle.Render(" " + item.Desc)
286+ }
287+ lines = append(lines, padRight(line, innerW))
288+ }
289+ }
290+
291+ for len(lines) < listHeight {
292+ lines = append(lines, strings.Repeat(" ", innerW))
293+ }
294+ return lines
295+}
296+
297+func padRight(s string, w int) string {
298+ if len(s) >= w {
299+ return s
300+ }
301+ return s + strings.Repeat(" ", w-len(s))
302+}