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