| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | tea "charm.land/bubbletea/v2" |
| 7 | |
| 8 | "reasonix/internal/i18n" |
| 9 | ) |
| 10 | |
| 11 | // copyPicker is an in-chat overlay for "/copy" that lets the user pick an |
| 12 | // assistant message to copy with ↑/↓ and confirm with Enter. Esc closes it. |
| 13 | type copyPicker struct { |
| 14 | parts []string // assistant Content lines (newest-first: index 0 = most recent) |
| 15 | sel int // selected index |
| 16 | } |
| 17 | |
| 18 | // openCopyPicker populates the picker from the session history and opens it. |
| 19 | func (m *chatTUI) openCopyPicker() { |
| 20 | msgs := m.ctrl.History() |
| 21 | parts := copyAssistantParts(msgs) |
| 22 | if len(parts) == 0 { |
| 23 | m.notice(i18n.M.SlashCopyEmpty) |
| 24 | return |
| 25 | } |
| 26 | // Reverse so newest-first matches the selection order (0 = most recent). |
| 27 | for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { |
| 28 | parts[i], parts[j] = parts[j], parts[i] |
| 29 | } |
| 30 | m.copyPick = ©Picker{parts: parts, sel: 0} |
| 31 | } |
| 32 | |
| 33 | func (m chatTUI) handleCopyPickerKey(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { |
| 34 | p := m.copyPick |
| 35 | if p == nil { |
| 36 | return m, nil |
| 37 | } |
| 38 | switch msg.String() { |
| 39 | case "up", "k": |
| 40 | if p.sel > 0 { |
| 41 | p.sel-- |
| 42 | } |
| 43 | case "down", "j": |
| 44 | if p.sel < len(p.parts)-1 { |
| 45 | p.sel++ |
| 46 | } |
| 47 | case "enter": |
| 48 | return m.applyCopyPick() |
| 49 | case "esc": |
| 50 | m.copyPick = nil |
| 51 | } |
| 52 | return m, nil |
| 53 | } |
| 54 | |
| 55 | func (m chatTUI) applyCopyPick() (tea.Model, tea.Cmd) { |
| 56 | p := m.copyPick |
| 57 | if p == nil || p.sel < 0 || p.sel >= len(p.parts) { |
| 58 | return m, nil |
| 59 | } |
| 60 | text := p.parts[p.sel] |
| 61 | m.copyPick = nil |
| 62 | return m, copyToClipboard(text) |
| 63 | } |
| 64 | |
| 65 | func (m chatTUI) renderCopyPicker() string { |
| 66 | p := m.copyPick |
| 67 | if p == nil { |
| 68 | return "" |
| 69 | } |
| 70 | w := max(m.width, 10) |
| 71 | var b strings.Builder |
| 72 | b.WriteString(accent(i18n.M.SlashCopyListHeader) + "\n") |
| 73 | for i, part := range p.parts { |
| 74 | b.WriteString(rowLine(i == p.sel, i+1, "", firstLine(part), false) + "\n") |
| 75 | } |
| 76 | b.WriteString(dim("↑/↓ navigate · Enter copy · Esc cancel")) |
| 77 | return choicePanelStyle.Width(w).Render(b.String()) |
| 78 | } |
| 79 |