1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
)
// DetailViewModel manages a generic scrollable overlay box that can display any content.
type DetailViewModel struct {
showing bool // Whether detail view is currently displayed
title string // Title for the detail box ("Help", "Commit Details", etc.)
content ColorizedText // Content to display in detail view
scrollOffset int // Vertical scroll offset (0 = top)
}
// NewDetailViewModel creates a new detail view model.
func NewDetailViewModel() DetailViewModel {
return DetailViewModel{
showing: false,
title: "",
content: ColorizedText{},
scrollOffset: 0,
}
}
// Update handles keyboard input for the detail view.
// Returns true if the message was handled, false otherwise.
func (dv *DetailViewModel) Update(msg tea.KeyMsg, pageSize int, windowWidth int, windowHeight int) bool {
if !dv.showing {
return false
}
switch msg.String() {
case "q":
dv.Hide()
return true
case "up", "k":
dv.scrollOffset--
dv.clampScroll(windowWidth, windowHeight)
return true
case "down", "j":
dv.scrollOffset++
dv.clampScroll(windowWidth, windowHeight)
return true
case "pgup":
dv.scrollOffset -= pageSize
dv.clampScroll(windowWidth, windowHeight)
return true
case "pgdown":
dv.scrollOffset += pageSize
dv.clampScroll(windowWidth, windowHeight)
return true
case "home", "g":
dv.scrollOffset = 0
return true
case "end", "G":
dv.scrollOffset = 999999
dv.clampScroll(windowWidth, windowHeight)
return true
}
return false
}
// UpdateMouse handles mouse input for the detail view.
// Returns true if the message was handled, false otherwise.
func (dv *DetailViewModel) UpdateMouse(msg tea.MouseMsg, windowWidth int, windowHeight int) bool {
if !dv.showing {
return false
}
// Swallow all mouse events while showing, but only wheel events scroll
if msg.Action != tea.MouseActionPress {
return true
}
switch msg.Button {
case tea.MouseButtonWheelUp:
dv.scrollOffset -= mouseScrollLines
dv.clampScroll(windowWidth, windowHeight)
case tea.MouseButtonWheelDown:
dv.scrollOffset += mouseScrollLines
dv.clampScroll(windowWidth, windowHeight)
}
return true
}
// View renders the detail view with scrolling support.
func (dv DetailViewModel) View(windowWidth int, windowHeight int) string {
// Create a temporary style to measure frame size (borders + padding)
tempStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
Padding(1, 2)
horizontalFrameSize := tempStyle.GetHorizontalFrameSize()
// Start with max available width (leave some margin for window edges)
maxBoxWidth := max(windowWidth-4, 40)
// Calculate content width by subtracting frame size
contentWidth := max(maxBoxWidth-horizontalFrameSize+2, 30)
wrappedText := wrapTextWithLineNumbers(dv.content, contentWidth)
totalLines := wrappedText.LineCount()
// Find the longest line in wrapped text to determine actual needed width
maxLineWidth := 0
for i := range totalLines {
lineWidth := wrappedText.LineWidth(i)
if lineWidth > maxLineWidth {
maxLineWidth = lineWidth
}
}
// Calculate box width based on longest line (add back frame size)
neededBoxWidth := maxLineWidth + horizontalFrameSize
// Also need to account for title and footer
titleWidth := ansi.StringWidth(dv.title)
footerWidth := ansi.StringWidth("↑↓/jk: scroll | q: return")
if titleWidth > maxLineWidth {
neededBoxWidth = titleWidth + horizontalFrameSize
}
if footerWidth > maxLineWidth {
neededBoxWidth = footerWidth + horizontalFrameSize
}
// Use minimum of needed width and max available width
boxWidth := min(neededBoxWidth, maxBoxWidth)
if boxWidth < 40 {
boxWidth = 40
}
// Calculate available height for detail text
verticalFrameSize := tempStyle.GetVerticalFrameSize()
fixedContentLines := 4 // title + empty line + footer empty line + footer text
availableHeight := max(windowHeight-verticalFrameSize-fixedContentLines, 5)
// Extract visible lines (scroll position is already clamped in Update)
endLine := min(dv.scrollOffset+availableHeight, totalLines)
visibleText := wrappedText.GetLineRange(dv.scrollOffset, endLine)
// Show scroll indicator in footer
scrollInfo := ""
if totalLines > availableHeight {
scrollInfo = fmt.Sprintf(" (line %d-%d of %d)", dv.scrollOffset+1, endLine, totalLines)
}
// Check scroll position
isAtTop := (dv.scrollOffset == 0)
isAtBottom := (dv.scrollOffset+availableHeight >= totalLines)
// Create border - show continuation indicators when scrolled
border := lipgloss.RoundedBorder()
if !isAtTop {
border.Top = ""
border.TopLeft = "┊"
border.TopRight = "┊"
}
if !isAtBottom {
border.Bottom = ""
border.BottomLeft = "┊"
border.BottomRight = "┊"
}
style := lipgloss.NewStyle().
Border(border).
BorderForeground(ColorDetailBorder).
Padding(1, 2).
Width(boxWidth)
return style.Render(
lipgloss.JoinVertical(
lipgloss.Left,
lipgloss.NewStyle().Bold(true).Render(dv.title),
"",
visibleText,
"",
lipgloss.NewStyle().Foreground(ColorTextDim).Render("↑↓/jk: scroll | q: return"+scrollInfo),
),
)
}
// Show displays the detail view with the given title and content.
func (dv *DetailViewModel) Show(title string, content ColorizedText) {
dv.showing = true
dv.title = title
dv.content = content
dv.scrollOffset = 0
}
// Hide closes the detail view.
func (dv *DetailViewModel) Hide() {
dv.showing = false
dv.scrollOffset = 0
}
// IsShowing returns whether the detail view is currently displayed.
func (dv DetailViewModel) IsShowing() bool {
return dv.showing
}
// clampScroll ensures scroll position stays within valid bounds.
// Accounts for wrapped text line count and available viewport height.
func (dv *DetailViewModel) clampScroll(windowWidth int, windowHeight int) {
if !dv.showing {
return
}
// Calculate available height for detail text
// Reserve space for: border (2), padding (2), title (1), empty line (1), footer empty line (1), footer text (1) = 8 lines
availableHeight := max(windowHeight-8, 5)
// Calculate box width and content width (same as in View())
boxWidth := max(windowWidth-4, 40)
contentWidth := max(boxWidth-6, 30)
// Wrap text to get actual line count
wrappedText := wrapTextWithLineNumbers(dv.content, contentWidth)
// Calculate max scroll
totalLines := wrappedText.LineCount()
maxScroll := max(totalLines-availableHeight, 0)
// Clamp scroll position
if dv.scrollOffset < 0 {
dv.scrollOffset = 0
}
if dv.scrollOffset > maxScroll {
dv.scrollOffset = maxScroll
}
}
|