xonecas.io
62159
Sy

symb / commit 1973fc2

fix: remove half baked selection controls

xonecas2026-02-13 1973fc20b9d479e82179c4a21475e46ba9dfa1b2 parent bc53eff

3 files changed

internal/tui/editor/editor.go+2−100
@@ -12,7 +12,6 @@ import (
1212 "github.com/alecthomas/chroma/v2/formatters"
1313 "github.com/alecthomas/chroma/v2/lexers"
1414 "github.com/alecthomas/chroma/v2/styles"
15- "github.com/atotto/clipboard"
1615 "github.com/charmbracelet/bubbles/cursor"
1716 tea "github.com/charmbracelet/bubbletea"
1817 "github.com/charmbracelet/lipgloss"
@@ -144,11 +143,6 @@ type Model struct {
144143 focus bool
145144 cursor cursor.Model
146145
147- // Mouse selection
148- selecting bool
149- selectStart pos
150- selectEnd pos
151-
152146 // Cached computed values
153147 gutterWidth int // Width of line number gutter (0 if disabled)
154148 }
@@ -552,55 +546,6 @@ func (m *Model) tabIndent() {
552546 }
553547 }
554548
555-// ---------------------------------------------------------------------------
556-// Selection helpers
557-// ---------------------------------------------------------------------------
558-
559-func (m *Model) selectionOrdered() (start, end pos) {
560- s, e := m.selectStart, m.selectEnd
561- if s.row > e.row || (s.row == e.row && s.col > e.col) {
562- s, e = e, s
563- }
564- return s, e
565-}
566-
567-func (m *Model) hasSelection() bool {
568- return m.selectStart != m.selectEnd
569-}
570-
571-func (m *Model) clearSelection() {
572- m.selecting = false
573- m.selectStart = pos{}
574- m.selectEnd = pos{}
575-}
576-
577-func (m *Model) selectedText() string {
578- if !m.hasSelection() {
579- return ""
580- }
581- s, e := m.selectionOrdered()
582- if s.row == e.row {
583- line := m.lines[s.row]
584- sc := clampMax(s.col, len(line))
585- ec := clampMax(e.col, len(line))
586- return string(line[sc:ec])
587- }
588- var sb strings.Builder
589- // First line from s.col to end
590- first := m.lines[s.row]
591- sb.WriteString(string(first[clampMax(s.col, len(first)):]))
592- // Middle lines in full
593- for r := s.row + 1; r < e.row; r++ {
594- sb.WriteByte('\n')
595- sb.WriteString(string(m.lines[r]))
596- }
597- // Last line from start to e.col
598- sb.WriteByte('\n')
599- last := m.lines[e.row]
600- sb.WriteString(string(last[:clampMax(e.col, len(last))]))
601- return sb.String()
602-}
603-
604549 // screenToPos converts screen-relative x,y to a buffer row,col.
605550 // x,y are relative to the editor component origin.
606551 func (m *Model) screenToPos(x, y int) pos {
@@ -621,16 +566,6 @@ func (m *Model) screenToPos(x, y int) pos {
621566 return pos{row: bufRow, col: bufCol}
622567 }
623568
624-func clampMax(v, hi int) int {
625- if v < 0 {
626- return 0
627- }
628- if v > hi {
629- return hi
630- }
631- return v
632-}
633-
634569 // ---------------------------------------------------------------------------
635570 // Update
636571 // ---------------------------------------------------------------------------
@@ -693,20 +628,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
693628 case "tab":
694629 m.tabIndent()
695630
696- // Clipboard
697- case "ctrl+v":
698- if !m.ReadOnly {
699- if text, err := clipboard.ReadAll(); err == nil {
700- for _, r := range text {
701- if r == '\n' {
702- m.insertNewline()
703- } else {
704- m.insertRune(r)
705- }
706- }
707- }
708- }
709-
710631 default:
711632 moved = false
712633 // Insert printable runes
@@ -731,30 +652,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
731652 }
732653 switch msg.Button {
733654 case tea.MouseButtonLeft:
734- p := m.screenToPos(msg.X, msg.Y)
735- switch msg.Action {
736- case tea.MouseActionPress:
737- m.selecting = true
738- m.selectStart = p
739- m.selectEnd = p
655+ if msg.Action == tea.MouseActionPress {
656+ p := m.screenToPos(msg.X, msg.Y)
740657 m.row = p.row
741658 m.col = p.col
742659 m.clampCursor()
743- case tea.MouseActionMotion:
744- if m.selecting {
745- m.selectEnd = p
746- }
747- case tea.MouseActionRelease:
748- if m.selecting && m.hasSelection() {
749- text := m.selectedText()
750- if text != "" {
751- cmds = append(cmds, func() tea.Msg {
752- _ = clipboard.WriteAll(text)
753- return nil
754- })
755- }
756- }
757- m.clearSelection()
758660 }
759661 case tea.MouseButtonWheelUp:
760662 m.scroll -= 3
internal/tui/tui.go+7−69
@@ -11,7 +11,6 @@ import (
1111 "strings"
1212 "time"
1313
14- "github.com/atotto/clipboard"
1514 "github.com/charmbracelet/bubbles/spinner"
1615 tea "github.com/charmbracelet/bubbletea"
1716 "github.com/charmbracelet/lipgloss"
@@ -172,13 +171,6 @@ type UpdateToolsMsg struct{ Tools []mcp.Tool }
172171 // ELM commands
173172 // ---------------------------------------------------------------------------
174173
175-func copyToClipboardCmd(text string) tea.Cmd {
176- return func() tea.Msg {
177- _ = clipboard.WriteAll(text)
178- return nil
179- }
180-}
181-
182174 func (m Model) sendToLLM(userInput string) tea.Cmd {
183175 return func() tea.Msg { return llmUserMsg{content: userInput} }
184176 }
@@ -288,9 +280,6 @@ type Model struct {
288280
289281 // Mouse state
290282 resizingPane bool
291- selecting bool
292- selectStart int
293- selectEnd int
294283 }
295284
296285 // New creates a new TUI model.
@@ -734,44 +723,13 @@ func (m Model) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
734723
735724 switch msg.Button {
736725 case tea.MouseButtonLeft:
737- // Calculate which conversation line was hit
738- startLine := m.visibleStartLine()
739- clickedLine := startLine + (msg.Y - m.layout.conv.Min.Y)
740-
741- switch msg.Action {
742- case tea.MouseActionPress:
743- m.selecting = true
744- m.selectStart = clickedLine
745- m.selectEnd = clickedLine
746- case tea.MouseActionMotion:
747- if m.selecting {
748- m.selectEnd = clickedLine
726+ if msg.Action == tea.MouseActionRelease {
727+ // Single click — try to handle as interactive click
728+ startLine := m.visibleStartLine()
729+ clickedLine := startLine + (msg.Y - m.layout.conv.Min.Y)
730+ if cmd := m.handleConvClick(clickedLine); cmd != nil {
731+ cmds = append(cmds, cmd)
749732 }
750- case tea.MouseActionRelease:
751- m.selecting = false
752- if m.selectStart == m.selectEnd {
753- // Single click — try to handle as interactive click
754- if cmd := m.handleConvClick(clickedLine); cmd != nil {
755- cmds = append(cmds, cmd)
756- }
757- } else {
758- start, end := m.selectStart, m.selectEnd
759- if start > end {
760- start, end = end, start
761- }
762- start = max(start, 0)
763- end = min(end, totalLines-1)
764-
765- var sb strings.Builder
766- for i := start; i <= end; i++ {
767- sb.WriteString(ansi.Strip(lines[i]))
768- if i < end {
769- sb.WriteByte('\n')
770- }
771- }
772- cmds = append(cmds, copyToClipboardCmd(sb.String()))
773- }
774- m.selectStart, m.selectEnd = 0, 0
775733 }
776734
777735 case tea.MouseButtonWheelUp:
@@ -949,30 +907,10 @@ func (m Model) View() string {
949907 lineIdx := startLine + relY
950908 if lineIdx < len(convLines) {
951909 line := convLines[lineIdx]
952-
953- // Selection highlight
954- isSelected := false
955- if m.selectStart != m.selectEnd {
956- s, e := m.selectStart, m.selectEnd
957- if s > e {
958- s, e = e, s
959- }
960- isSelected = lineIdx >= s && lineIdx <= e
961- }
962- if isSelected {
963- line = m.styles.Selection.Render(line)
964- }
965-
966910 lw := lipgloss.Width(line)
967911 b.WriteString(line)
968912 if lw < rw {
969- pad := strings.Repeat(" ", rw-lw)
970- if isSelected {
971- pad = m.styles.Selection.Render(pad)
972- } else {
973- pad = bgFill.Render(pad)
974- }
975- b.WriteString(pad)
913+ b.WriteString(bgFill.Render(strings.Repeat(" ", rw-lw)))
976914 }
977915 } else {
978916 b.WriteString(bgFill.Render(strings.Repeat(" ", rw)))
selection-refactor.md+69−0
@@ -0,0 +1,69 @@
1+# Selection & Clipboard Refactor
2+
3+## Requirements
4+
5+- **Keyboard selection**: Hold Shift to start selection. Text movements update selection while Shift is held. Editors only (main + agent input).
6+- **Mouse selection**: Dragging over text selects continually until drag stops. Works on all text in the app (editor, conversation, input).
7+- **Ctrl+Shift+C**: Copy selection to clipboard.
8+- **Ctrl+Shift+V**: Paste on active cursor.
9+
10+## Current State (Pre-Refactor)
11+
12+Two separate half-baked selection systems:
13+
14+1. **Editor** (`editor/editor.go`): Tracks `selectStart`/`selectEnd` as `pos{row,col}` but View() never renders the highlight. Auto-copies on mouse release then immediately clears. No keyboard selection.
15+
16+2. **Conversation** (`tui.go`): Line-level selection (1D wrapped-line index). Auto-copies on release then clears. No character granularity.
17+
18+Both auto-copy on mouse release. No Ctrl+Shift+C/V. ctrl+c = quit. ctrl+v = paste (editor only).
19+
20+## Architecture
21+
22+### Editor (`editor/editor.go`)
23+
24+| Change | Description |
25+|--------|-------------|
26+| Selection rendering | `View()` must highlight selected text using a selection background style |
27+| Keyboard selection | Shift+arrow/home/end/pgup/pgdown extends selection from cursor anchor |
28+| Don't auto-copy on mouse release | Keep selection visible until explicitly copied or cleared |
29+| Clear on non-shift movement | Unmodified arrows/clicks clear selection |
30+| Delete selection on edit | Typing/backspace/paste replaces selected text |
31+| Ctrl+Shift+C | Copy selection to clipboard |
32+| Ctrl+Shift+V | Paste from clipboard |
33+| Selection style | Add `SelectionStyle lipgloss.Style` field |
34+
35+### Top-level TUI (`tui.go`)
36+
37+| Change | Description |
38+|--------|-------------|
39+| Ctrl+Shift+C | Global: copy from whichever component has selection (editor, input, or conversation) |
40+| Ctrl+Shift+V | Forward to focused editor component |
41+| ctrl+c stays as quit | No change |
42+| Conversation selection persistence | Don't auto-copy on release; keep highlight until Ctrl+Shift+C or click elsewhere |
43+| Conversation mouse selection | Keep line-level but don't auto-clear |
44+
45+### Key Bindings
46+
47+| Shortcut | Context | Action |
48+|----------|---------|--------|
49+| Shift+arrows | Editor (focused) | Start/extend selection |
50+| Shift+Home/End | Editor (focused) | Select to line start/end |
51+| Shift+PgUp/PgDown | Editor (focused) | Select page up/down |
52+| Ctrl+Shift+C | Global | Copy selection to clipboard |
53+| Ctrl+Shift+V | Editor (focused) | Paste from clipboard |
54+| Mouse drag | Anywhere | Select text (keep visible after release) |
55+| Click (no drag) | Anywhere | Clear selection + position cursor |
56+| Any edit key | Editor (focused) | Delete selection first, then apply edit |
57+
58+### Exported API (editor -> tui)
59+
60+- `HasSelection() bool` — tui queries for Ctrl+Shift+C routing
61+- `SelectedText() string` — tui reads for clipboard copy
62+- `ClearSelection()` — tui clears after copy or focus change
63+- `SelectionStyle lipgloss.Style` — set by parent
64+
65+### Risk Areas
66+
67+- Shift key detection: bubbletea uses `"shift+left"`, `"shift+right"`, `"ctrl+shift+c"`, etc.
68+- Selection rendering in View(): must intersect selection range with each visual row, handle soft-wrap, syntax highlighting, and cursor.
69+- Editor mouse currently gated on `m.focus` — mouse press in tui.go already focuses editor before forwarding, so this is fine.