| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | tea "charm.land/bubbletea/v2" |
| 8 | "github.com/charmbracelet/x/ansi" |
| 9 | ) |
| 10 | |
| 11 | type pinnedPanelScroll struct { |
| 12 | owner string |
| 13 | focus, selection int |
| 14 | offset, width, capacity int |
| 15 | } |
| 16 | |
| 17 | type pinnedPanelView struct { |
| 18 | lines []string |
| 19 | owner string |
| 20 | selection int |
| 21 | start, focus, offset, capacity int |
| 22 | } |
| 23 | |
| 24 | func (p pinnedPanelView) height() int { |
| 25 | if p.capacity == 0 { |
| 26 | return 0 |
| 27 | } |
| 28 | if len(p.lines) <= p.capacity { |
| 29 | return len(p.lines) |
| 30 | } |
| 31 | return p.capacity + 1 // the last row describes the scrollable window |
| 32 | } |
| 33 | |
| 34 | func (p pinnedPanelView) render(width int) string { |
| 35 | if p.height() == 0 { |
| 36 | return "" |
| 37 | } |
| 38 | end := min(p.offset+p.capacity, len(p.lines)) |
| 39 | content := strings.Join(p.lines[p.offset:end], "\n") |
| 40 | if len(p.lines) > p.capacity { |
| 41 | hint := fmt.Sprintf("↑ %d–%d/%d ↓ · Alt+PgUp/PgDn", p.offset+1, end, len(p.lines)) |
| 42 | content += "\n" + dim(padRight(ansi.Truncate(hint, width, ""), width)) |
| 43 | } |
| 44 | return content |
| 45 | } |
| 46 | |
| 47 | // All optional bottom panels share one row budget. The composer, status and |
| 48 | // progress rows stay outside this viewport, so a wrapped item cannot displace |
| 49 | // the input or footer. Retain the complete content for wheel/keyboard scrolling. |
| 50 | func (m chatTUI) pinnedPanelView() pinnedPanelView { |
| 51 | p := m.pinnedPanelContent() |
| 52 | limit := len(p.lines) |
| 53 | if m.height > 0 { |
| 54 | limit = max(0, m.height-m.protectedBottomRows()-minTranscriptRows) |
| 55 | } |
| 56 | p.capacity = min(len(p.lines), limit) |
| 57 | if len(p.lines) > limit { |
| 58 | p.capacity = max(0, limit-1) |
| 59 | } |
| 60 | maximum := max(0, len(p.lines)-p.capacity) |
| 61 | p.offset = min(m.pinnedScroll.offset, maximum) |
| 62 | if m.pinnedScroll.owner != p.owner || m.pinnedScroll.width != m.width { |
| 63 | p.offset = min(p.start, maximum) |
| 64 | } |
| 65 | if m.pinnedScroll.owner != p.owner || m.pinnedScroll.focus != p.focus || m.pinnedScroll.selection != p.selection || m.pinnedScroll.width != m.width || m.pinnedScroll.capacity != p.capacity { |
| 66 | if p.focus < p.offset { |
| 67 | p.offset = p.focus |
| 68 | } else if p.focus >= p.offset+p.capacity { |
| 69 | p.offset = max(0, p.focus-p.capacity+1) |
| 70 | } |
| 71 | } |
| 72 | p.offset = min(max(0, p.offset), maximum) |
| 73 | return p |
| 74 | } |
| 75 | |
| 76 | func (m chatTUI) pinnedPanelContent() pinnedPanelView { |
| 77 | var p pinnedPanelView |
| 78 | add := func(owner string, content panelContent) { |
| 79 | if content.text == "" { |
| 80 | return |
| 81 | } |
| 82 | rows := strings.Split(wrapTerminalText(content.text, max(m.width, 10)), "\n") |
| 83 | p.owner, p.start, p.focus = owner, len(p.lines), len(p.lines)+content.focus |
| 84 | p.selection = content.selection |
| 85 | p.lines = append(p.lines, rows...) |
| 86 | } |
| 87 | add("todos", m.renderTodoContent()) |
| 88 | add(fmt.Sprintf("approval:%p", m.pendingApproval), m.renderApprovalBannerPanel()) |
| 89 | chooserKey := fmt.Sprintf("chooser:%p", m.chooser) |
| 90 | if m.chooser != nil { |
| 91 | chooserKey += fmt.Sprintf(":%d", m.chooser.tab) |
| 92 | } |
| 93 | add(chooserKey, m.renderChooserPanel()) |
| 94 | add(fmt.Sprintf("elicit:%p", m.elicit), m.renderElicitPanel()) |
| 95 | add(fmt.Sprintf("rewind:%p", m.rewind), m.renderRewindPanel()) |
| 96 | add(fmt.Sprintf("import:%p", m.mcpImport), m.renderMCPImportPanel()) |
| 97 | add(fmt.Sprintf("resume:%p", m.resumePick), m.renderResumePickerPanel()) |
| 98 | add(fmt.Sprintf("quick:%p", m.quickPick), m.renderQuickPickerPanel()) |
| 99 | add(fmt.Sprintf("setup:%p", m.setup), m.renderConnectionSetupPanel()) |
| 100 | add(fmt.Sprintf("copy:%p", m.copyPick), m.renderCopyPickerPanel()) |
| 101 | add("completion", m.renderCompletionPanel()) |
| 102 | if m.nativeScrollback { |
| 103 | add("manager", m.renderMainManagerPanel()) |
| 104 | } |
| 105 | return p |
| 106 | } |
| 107 | |
| 108 | func (m *chatTUI) syncPinnedPanelScroll() { |
| 109 | p := m.pinnedPanelView() |
| 110 | m.pinnedScroll = pinnedPanelScroll{owner: p.owner, focus: p.focus, selection: p.selection, offset: p.offset, width: m.width, capacity: p.capacity} |
| 111 | } |
| 112 | |
| 113 | func (m *chatTUI) scrollPinnedPanel(delta int) bool { |
| 114 | p := m.pinnedPanelView() |
| 115 | if p.capacity == 0 || len(p.lines) <= p.capacity { |
| 116 | return false |
| 117 | } |
| 118 | m.pinnedScroll = pinnedPanelScroll{ |
| 119 | owner: p.owner, focus: p.focus, selection: p.selection, width: m.width, capacity: p.capacity, |
| 120 | offset: min(max(0, p.offset+delta), len(p.lines)-p.capacity), |
| 121 | } |
| 122 | return true |
| 123 | } |
| 124 | |
| 125 | func (m *chatTUI) scrollPinnedPanelMouse(msg tea.MouseWheelMsg) bool { |
| 126 | if m.nativeScrollback || msg.X < 0 || msg.X >= m.width || msg.Y < m.viewport.Height() || msg.Y >= m.viewport.Height()+m.pinnedPanelView().height() { |
| 127 | return false |
| 128 | } |
| 129 | switch msg.Button { |
| 130 | case tea.MouseWheelUp: |
| 131 | return m.scrollPinnedPanel(-3) |
| 132 | case tea.MouseWheelDown: |
| 133 | return m.scrollPinnedPanel(3) |
| 134 | } |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | func (m *chatTUI) scrollPinnedPanelKey(msg tea.KeyPressMsg) bool { |
| 139 | switch msg.String() { |
| 140 | case "alt+pgup": |
| 141 | return m.scrollPinnedPanel(-max(1, m.pinnedPanelView().capacity-1)) |
| 142 | case "alt+pgdown": |
| 143 | return m.scrollPinnedPanel(max(1, m.pinnedPanelView().capacity-1)) |
| 144 | } |
| 145 | return false |
| 146 | } |
| 147 |