xonecas.io
62159
Sy

symb / commit 1d45246

feat: basic mouse controls

xonecas2026-02-11 1d45246c3fa63ba3041521f5827f1d08ff00d146 parent 1519037

6 files changed

docs/DESIGN.md+13−1
@@ -154,9 +154,21 @@ Confidence Level: High for a prototype. The ecosystem is ready.
154154
155155 # TODO:
156156
157+Potential Mouse Improvements:
158+
159+1. Click to focus between editor and input panes
160+2. Click in conversation log to select/copy text
161+3. Scroll conversation with mouse wheel (currently auto-scrolls to bottom only)
162+4. Click to position cursor in editor and input areas
163+5. Text selection with mouse drag
164+6. Resize panes by dragging the divider
165+
166+- Copy text selection to clipboard (ctrl c)
167+
168+- Scroll the coversation log with mouse.
157169 - `stub.go` is bleeding into prod runs, the llm sees those tools.
158170 - Credentials tools need to be deleted
159171 - OpenForUser(file, start, end) tool for llm -> sends file contents to editor in a new buffer with correct filetype syntax
160-- file opened keybind and opened buffers (no fzf yet)
172+- file searching and opening, buffer switching.
161173 - sendUserEditor(content) tool to add a "buffer"
162174 - streamed assistant response
internal/tui/testdata/TestLayout/120x40/ANSI.golden+5−5
@@ -1,8 +1,4 @@
1- 1 │┌ Input ───────────────────────────────────────────────────
2- ││ 1 Type a message... │
3- ││ │
4- ││ │
5- │└──────────────────────────────────────────────────────────
1+ 1 │
62 │
73 │
84 │
@@ -36,5 +32,9 @@
3632 │
3733 │
3834 │
35+ ├───────────────────────────────────────────────────────────
36+ │Type a message...
37+ │
38+ │
3939 ────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────
4040 gitbranch/working dir ⣾
internal/tui/testdata/TestLayout/120x40/Stripped.golden+5−5
@@ -1,8 +1,4 @@
1- 1 │┌ Input ───────────────────────────────────────────────────
2- ││ 1 Type a message... │
3- ││ │
4- ││ │
5- │└──────────────────────────────────────────────────────────
1+ 1 │
62 │
73 │
84 │
@@ -36,5 +32,9 @@
3632 │
3733 │
3834 │
35+ ├───────────────────────────────────────────────────────────
36+ │Type a message...
37+ │
38+ │
3939 ────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────
4040 gitbranch/working dir ⣾
internal/tui/testdata/TestLayout/80x24/ANSI.golden+5−5
@@ -1,8 +1,4 @@
1- 1 │┌ Input ───────────────────────────────
2- ││ 1 Type a message... │
3- ││ │
4- ││ │
5- │└──────────────────────────────────────
1+ 1 │
62 │
73 │
84 │
@@ -20,5 +16,9 @@
2016 │
2117 │
2218 │
19+ ├───────────────────────────────────────
20+ │Type a message...
21+ │
22+ │
2323 ────────────────────────────────────────┴───────────────────────────────────────
2424 gitbranch/working dir ⣾
internal/tui/testdata/TestLayout/80x24/Stripped.golden+5−5
@@ -1,8 +1,4 @@
1- 1 │┌ Input ───────────────────────────────
2- ││ 1 Type a message... │
3- ││ │
4- ││ │
5- │└──────────────────────────────────────
1+ 1 │
62 │
73 │
84 │
@@ -20,5 +16,9 @@
2016 │
2117 │
2218 │
19+ ├───────────────────────────────────────
20+ │Type a message...
21+ │
22+ │
2323 ────────────────────────────────────────┴───────────────────────────────────────
2424 gitbranch/working dir ⣾
internal/tui/tui.go+219−12
@@ -5,6 +5,7 @@ import (
55 "strings"
66 "time"
77
8+ "github.com/atotto/clipboard"
89 "github.com/charmbracelet/bubbles/spinner"
910 "github.com/charmbracelet/bubbles/textarea"
1011 tea "github.com/charmbracelet/bubbletea"
@@ -34,6 +35,18 @@ type Model struct {
3435 updateChan chan tea.Msg // Channel for streaming LLM updates
3536 ctx context.Context
3637 cancel context.CancelFunc
38+
39+ // Conversation scroll state
40+ conversationScrollOffset int // Lines scrolled from bottom (0 = stick to bottom)
41+
42+ // Conversation selection state
43+ conversationSelecting bool // True when dragging to select
44+ conversationSelectStart int // Line index where selection started
45+ conversationSelectEnd int // Line index where selection ends
46+
47+ // Pane resize state
48+ resizingPane bool // True when dragging divider
49+ customHalfWidth int // Custom half width (0 = use default calculated value)
3750 }
3851
3952 // New creates a new TUI model
@@ -217,6 +230,148 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
217230 var cmds []tea.Cmd
218231
219232 switch msg := msg.(type) {
233+ case tea.MouseMsg:
234+ // Handle mouse events
235+ halfWidth := m.width / 2
236+ if m.customHalfWidth > 0 {
237+ halfWidth = m.customHalfWidth
238+ }
239+ contentHeight := m.height - 2
240+ inputStartRow := contentHeight - 3
241+ separatorRow := contentHeight - 4
242+
243+ // Handle pane resizing
244+ if msg.X == halfWidth && msg.Button == tea.MouseButtonLeft {
245+ if msg.Action == tea.MouseActionPress {
246+ m.resizingPane = true
247+ } else if msg.Action == tea.MouseActionRelease {
248+ m.resizingPane = false
249+ }
250+ }
251+
252+ if m.resizingPane && msg.Action == tea.MouseActionMotion {
253+ // Update pane width based on mouse position
254+ newHalfWidth := msg.X
255+ minWidth := 20
256+ maxWidth := m.width - 20
257+ if newHalfWidth >= minWidth && newHalfWidth <= maxWidth {
258+ m.customHalfWidth = newHalfWidth
259+ m.rightPaneWidth = m.width - newHalfWidth - 1
260+
261+ // Update component sizes
262+ m.editor.SetWidth(newHalfWidth - 1)
263+ m.agentInput.SetWidth(m.width - newHalfWidth - 4)
264+ }
265+ return m, nil
266+ }
267+
268+ // Determine which pane the mouse is in and translate coordinates
269+ if msg.X < halfWidth {
270+ // Left pane (editor)
271+ if msg.Action == tea.MouseActionPress && msg.Button == tea.MouseButtonLeft {
272+ m.agentInput.Blur()
273+ m.editor.Focus()
274+ }
275+ // Forward mouse event to editor (coordinates are already correct for left pane)
276+ var cmd tea.Cmd
277+ m.editor, cmd = m.editor.Update(msg)
278+ cmds = append(cmds, cmd)
279+
280+ } else if msg.X > halfWidth && msg.Y >= inputStartRow && msg.Y < contentHeight {
281+ // Right pane input area
282+ if msg.Action == tea.MouseActionPress && msg.Button == tea.MouseButtonLeft {
283+ m.editor.Blur()
284+ m.agentInput.Focus()
285+ }
286+ // Translate coordinates for input area (remove left offset and input start offset)
287+ translatedMsg := msg
288+ translatedMsg.X = msg.X - halfWidth - 1
289+ translatedMsg.Y = msg.Y - inputStartRow
290+ var cmd tea.Cmd
291+ m.agentInput, cmd = m.agentInput.Update(translatedMsg)
292+ cmds = append(cmds, cmd)
293+
294+ } else if msg.X > halfWidth && msg.Y < separatorRow {
295+ // Right pane conversation area
296+ switch msg.Button {
297+ case tea.MouseButtonLeft:
298+ // Calculate which line was clicked
299+ totalLines := len(m.conversation)
300+ visibleLines := separatorRow
301+ startLine := 0
302+ if totalLines > visibleLines {
303+ startLine = totalLines - visibleLines - m.conversationScrollOffset
304+ if startLine < 0 {
305+ startLine = 0
306+ }
307+ }
308+ clickedLine := startLine + msg.Y
309+
310+ if msg.Action == tea.MouseActionPress {
311+ // Start selection
312+ m.conversationSelecting = true
313+ m.conversationSelectStart = clickedLine
314+ m.conversationSelectEnd = clickedLine
315+ } else if msg.Action == tea.MouseActionMotion && m.conversationSelecting {
316+ // Update selection end
317+ m.conversationSelectEnd = clickedLine
318+ } else if msg.Action == tea.MouseActionRelease {
319+ // End selection and copy to clipboard
320+ m.conversationSelecting = false
321+ if m.conversationSelectStart != m.conversationSelectEnd {
322+ // Copy selected lines to clipboard
323+ start := m.conversationSelectStart
324+ end := m.conversationSelectEnd
325+ if start > end {
326+ start, end = end, start
327+ }
328+ if start < 0 {
329+ start = 0
330+ }
331+ if end >= totalLines {
332+ end = totalLines - 1
333+ }
334+
335+ var selectedText strings.Builder
336+ for i := start; i <= end && i < totalLines; i++ {
337+ // Strip ANSI codes for clipboard
338+ line := ansi.Strip(m.conversation[i])
339+ selectedText.WriteString(line)
340+ if i < end {
341+ selectedText.WriteString("\n")
342+ }
343+ }
344+
345+ // Copy to clipboard (best effort, ignore errors)
346+ _ = clipboard.WriteAll(selectedText.String())
347+ }
348+ // Clear selection
349+ m.conversationSelectStart = 0
350+ m.conversationSelectEnd = 0
351+ }
352+
353+ case tea.MouseButtonWheelUp:
354+ totalLines := len(m.conversation)
355+ visibleLines := separatorRow
356+ maxScroll := totalLines - visibleLines
357+ if maxScroll < 0 {
358+ maxScroll = 0
359+ }
360+ m.conversationScrollOffset += 3 // Scroll 3 lines at a time
361+ if m.conversationScrollOffset > maxScroll {
362+ m.conversationScrollOffset = maxScroll
363+ }
364+
365+ case tea.MouseButtonWheelDown:
366+ m.conversationScrollOffset -= 3 // Scroll 3 lines at a time
367+ if m.conversationScrollOffset < 0 {
368+ m.conversationScrollOffset = 0
369+ }
370+ }
371+ }
372+ // Don't forward mouse events to components below since we handled them above
373+ return m, tea.Batch(cmds...)
374+
220375 case tea.KeyMsg:
221376 switch msg.String() {
222377 case "ctrl+c":
@@ -225,15 +380,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
225380 case "esc":
226381 if m.agentInput.Focused() {
227382 m.agentInput.Blur()
383+ } else if m.editor.Focused() {
384+ m.editor.Blur()
228385 }
229386 return m, nil
230387 case "enter":
388+ // In agent input: Enter sends message, use the textarea's default behavior otherwise
389+ // In editor: Enter inserts newline (textarea handles it)
231390 if m.agentInput.Focused() && m.agentInput.Value() != "" {
232391 userMsg := m.agentInput.Value()
233392 m.agentInput.Reset()
234393 return m, m.sendToLLM(userMsg)
235394 }
236- return m, nil
395+ // If editor is focused or agentInput is empty, let the key pass through to components
237396 }
238397
239398 case llmUserMsg:
@@ -261,6 +420,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
261420 )
262421 m.conversation = append(m.conversation, separator)
263422 m.conversation = append(m.conversation, "")
423+ // Reset scroll to stick to bottom on new message
424+ m.conversationScrollOffset = 0
264425 // Start LLM processing and wait for updates
265426 return m, tea.Batch(m.processLLM(), m.waitForLLMUpdate())
266427
@@ -330,6 +491,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
330491 m.height = msg.Height
331492
332493 halfWidth := m.width / 2
494+ if m.customHalfWidth > 0 {
495+ // Maintain custom width if set, but constrain to new terminal size
496+ minWidth := 20
497+ maxWidth := m.width - 20
498+ if m.customHalfWidth < minWidth {
499+ m.customHalfWidth = minWidth
500+ }
501+ if m.customHalfWidth > maxWidth {
502+ m.customHalfWidth = maxWidth
503+ }
504+ halfWidth = m.customHalfWidth
505+ }
333506 contentHeight := m.height - 2 // -2 for status separator and status bar
334507 m.rightPaneWidth = m.width - halfWidth - 1
335508
@@ -342,18 +515,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
342515 m.agentInput.SetHeight(3)
343516 }
344517
345- // Update spinner
518+ // Update spinner (always)
346519 var cmd tea.Cmd
347520 m.spinner, cmd = m.spinner.Update(msg)
348521 cmds = append(cmds, cmd)
349522
350- // Update editor
351- m.editor, cmd = m.editor.Update(msg)
352- cmds = append(cmds, cmd)
523+ // Update editor and agent input only for non-mouse messages
524+ // (mouse messages are handled specially above with coordinate translation)
525+ if _, isMouseMsg := msg.(tea.MouseMsg); !isMouseMsg {
526+ m.editor, cmd = m.editor.Update(msg)
527+ cmds = append(cmds, cmd)
353528
354- // Update agent input
355- m.agentInput, cmd = m.agentInput.Update(msg)
356- cmds = append(cmds, cmd)
529+ m.agentInput, cmd = m.agentInput.Update(msg)
530+ cmds = append(cmds, cmd)
531+ }
357532
358533 return m, tea.Batch(cmds...)
359534 }
@@ -364,8 +539,11 @@ func (m Model) View() string {
364539 return ""
365540 }
366541
367- // Split width in half for left/right panes
542+ // Split width in half for left/right panes (or use custom width)
368543 halfWidth := m.width / 2
544+ if m.customHalfWidth > 0 {
545+ halfWidth = m.customHalfWidth
546+ }
369547
370548 // Content height = total - status separator - status bar
371549 contentHeight := m.height - 2
@@ -409,22 +587,51 @@ func (m Model) View() string {
409587 inputStartRow := contentHeight - 3 // 3 rows for input
410588 if i < separatorRow {
411589 // Conversation area - show conversation log
412- // Calculate which line to show (scroll to show newest at bottom)
590+ // Calculate which line to show (scroll based on offset)
413591 totalLines := len(m.conversation)
414592 visibleLines := separatorRow
415593 startLine := 0
416594 if totalLines > visibleLines {
417- startLine = totalLines - visibleLines
595+ // Start from bottom and scroll up by offset
596+ startLine = totalLines - visibleLines - m.conversationScrollOffset
597+ if startLine < 0 {
598+ startLine = 0
599+ }
418600 }
419601 lineIdx := startLine + i
420602
421603 if lineIdx < totalLines {
422604 line := m.conversation[lineIdx]
605+
606+ // Check if this line is selected
607+ isSelected := false
608+ if m.conversationSelectStart != m.conversationSelectEnd {
609+ selStart := m.conversationSelectStart
610+ selEnd := m.conversationSelectEnd
611+ if selStart > selEnd {
612+ selStart, selEnd = selEnd, selStart
613+ }
614+ if lineIdx >= selStart && lineIdx <= selEnd {
615+ isSelected = true
616+ }
617+ }
618+
619+ // Apply selection highlighting if selected
620+ if isSelected {
621+ selStyle := lipgloss.NewStyle().Background(lipgloss.Color("#444444"))
622+ line = selStyle.Render(line)
623+ }
624+
423625 lineWidth := lipgloss.Width(line)
424626 b.WriteString(line)
425627 // Pad to full width
426628 if lineWidth < m.rightPaneWidth {
427- b.WriteString(strings.Repeat(" ", m.rightPaneWidth-lineWidth))
629+ padding := strings.Repeat(" ", m.rightPaneWidth-lineWidth)
630+ if isSelected {
631+ selStyle := lipgloss.NewStyle().Background(lipgloss.Color("#444444"))
632+ padding = selStyle.Render(padding)
633+ }
634+ b.WriteString(padding)
428635 }
429636 } else {
430637 b.WriteString(strings.Repeat(" ", m.rightPaneWidth))