62159
Sy
symb / files
symb / internal / tui / selection.go
3.5 KB
1package tui 2 3import ( 4 "strings" 5 6 tea "charm.land/bubbletea/v2" 7 "charm.land/lipgloss/v2" 8 "github.com/charmbracelet/x/ansi" 9) 10 11// --------------------------------------------------------------------------- 12// Selection and clipboard 13// --------------------------------------------------------------------------- 14 15// copySelection copies the active selection (from any component) to the 16// clipboard using both OSC 52 (for SSH/tmux) and native clipboard. 17func (m *Model) copySelection() tea.Cmd { 18 var text string 19 switch { 20 case m.agentInput.HasSelection(): 21 text = m.agentInput.SelectedText() 22 case m.convSel != nil && !m.convSel.empty(): 23 text = m.selectedConvText() 24 } 25 if text == "" { 26 return nil 27 } 28 return tea.SetClipboard(text) // OSC 52 — works through SSH/tmux 29} 30 31// selectedConvText returns the plain text of the conversation selection. 32func (m *Model) selectedConvText() string { 33 if m.convSel == nil || m.convSel.empty() { 34 return "" 35 } 36 lines := m.wrappedConvLines() 37 s, e := m.convSel.ordered() 38 s.line = max(s.line, 0) 39 e.line = min(e.line, len(lines)-1) 40 41 var sb strings.Builder 42 for i := s.line; i <= e.line; i++ { 43 plain := ansi.Strip(lines[i]) 44 runes := []rune(plain) 45 start := 0 46 end := len(runes) 47 if i == s.line { 48 start = min(s.col, len(runes)) 49 } 50 if i == e.line { 51 end = min(e.col, len(runes)) 52 } 53 sb.WriteString(string(runes[start:end])) 54 if i < e.line { 55 sb.WriteByte('\n') 56 } 57 } 58 return sb.String() 59} 60 61// applyClickableStyle returns the line as-is for entries that are already 62// pre-styled (undo, tool results with [view]). For plain text lines 63// containing file path references, it applies the clickable highlight. 64func (m Model) applyClickableStyle(line string, lineIdx int, _ lipgloss.Style) string { 65 if !m.isClickableLine(lineIdx) { 66 return line 67 } 68 m.wrappedConvLines() 69 src := m.convLineSource 70 if lineIdx < 0 || lineIdx >= len(src) { 71 return line 72 } 73 entryIdx := src[lineIdx] 74 if entryIdx < 0 || entryIdx >= len(m.convEntries) { 75 return line 76 } 77 entry := m.convEntries[entryIdx] 78 // Undo and tool results are pre-styled with clickable elements. 79 if entry.kind == entryUndo || entry.kind == entryToolResult { 80 return line 81 } 82 // Plain text with file path reference — highlight the whole line. 83 if lineIdx > 0 && src[lineIdx-1] == entryIdx { 84 return line 85 } 86 return m.styles.Clickable.Render(ansi.Strip(line)) 87} 88 89// renderConvLine renders a single conversation line with optional selection highlight. 90// Returns the styled line. Padding is handled by the caller. 91func (m Model) renderConvLine(line string, lineIdx int, bgFill lipgloss.Style) string { 92 if m.convSel == nil || m.convSel.empty() { 93 return m.applyClickableStyle(line, lineIdx, bgFill) 94 } 95 line = m.applyClickableStyle(line, lineIdx, bgFill) 96 97 s, e := m.convSel.ordered() 98 if lineIdx < s.line || lineIdx > e.line { 99 return line 100 } 101 102 plain := ansi.Strip(line) 103 runes := []rune(plain) 104 lineLen := len(runes) 105 106 // Compute selection column range for this line 107 selStart := 0 108 if lineIdx == s.line { 109 selStart = s.col 110 } 111 selEnd := lineLen 112 if lineIdx == e.line { 113 selEnd = e.col 114 } 115 116 // Clamp 117 if selStart < 0 { 118 selStart = 0 119 } 120 if selEnd > lineLen { 121 selEnd = lineLen 122 } 123 if selStart >= selEnd { 124 return line 125 } 126 127 // Build: [before] [selected] [after] 128 before := string(runes[:selStart]) 129 selected := string(runes[selStart:selEnd]) 130 after := string(runes[selEnd:]) 131 132 var sb strings.Builder 133 if before != "" { 134 sb.WriteString(bgFill.Render(before)) 135 } 136 sb.WriteString(m.styles.Selection.Render(selected)) 137 if after != "" { 138 sb.WriteString(bgFill.Render(after)) 139 } 140 return sb.String() 141}