xonecas.io
60255
Zn

Zoea-Nova / commit bf3895d

feat(tui): add visual scrollbar indicator to focus view

- Implement Unicode scrollbar with proportional thumb
- Show scroll position and total content height
- Update viewport width to accommodate scrollbar
- Track total line count for accurate scrollbar sizing
- Add test coverage for scrollbar rendering

xonecas2026-02-05 bf3895df6ca5353719bc30e50027586e98897a77 parent 6f6f3ce

5 files changed

internal/tui/app.go+6−4
@@ -51,8 +51,9 @@ type Model struct {
5151 loadingSet map[string]bool // mysisIDs currently loading
5252
5353 // Conversation viewport
54- viewport viewport.Model
55- autoScroll bool // true if viewport should auto-scroll to bottom
54+ viewport viewport.Model
55+ autoScroll bool // true if viewport should auto-scroll to bottom
56+ viewportTotalLines int // total lines in viewport content
5657
5758 // Network activity indicator
5859 netIndicator NetIndicator
@@ -127,7 +128,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
127128 if vpHeight < 5 {
128129 vpHeight = 5
129130 }
130- m.viewport.Width = msg.Width - 6
131+ m.viewport.Width = msg.Width - 6 - 1 // -6 for existing padding, -1 for scrollbar
131132 m.viewport.Height = vpHeight
132133
133134 // Re-render content if in focus view
@@ -236,7 +237,7 @@ func (m Model) View() string {
236237 if m.showHelp {
237238 content = RenderHelp(m.width, contentHeight)
238239 } else if m.view == ViewFocus {
239- content = RenderFocusViewWithViewport(m.mysisByID(m.focusID), m.viewport, m.width, isLoading, m.spinner.View(), m.autoScroll, m.verboseJSON)
240+ content = RenderFocusViewWithViewport(m.mysisByID(m.focusID), m.viewport, m.width, isLoading, m.spinner.View(), m.autoScroll, m.verboseJSON, m.viewportTotalLines)
240241 } else {
241242 // Convert swarm messages for display (reversed so most recent is first)
242243 swarmInfos := make([]SwarmMessageInfo, len(m.swarmMessages))
@@ -650,6 +651,7 @@ func (m *Model) updateViewportContent() {
650651
651652 content := strings.Join(lines, "\n")
652653 m.viewport.SetContent(content)
654+ m.viewportTotalLines = len(lines) // Track total lines
653655
654656 // Auto-scroll to bottom if enabled
655657 if m.autoScroll {
internal/tui/focus.go+28−4
@@ -133,7 +133,7 @@ func RenderFocusView(mysis MysisInfo, logs []LogEntry, width, height int, isLoad
133133 }
134134
135135 // RenderFocusViewWithViewport renders the detailed mysis view using a scrollable viewport.
136-func RenderFocusViewWithViewport(mysis MysisInfo, vp viewport.Model, width int, isLoading bool, spinnerView string, autoScroll bool, verbose bool) string {
136+func RenderFocusViewWithViewport(mysis MysisInfo, vp viewport.Model, width int, isLoading bool, spinnerView string, autoScroll bool, verbose bool, totalLines int) string {
137137 var sections []string
138138
139139 // Header with mysis name - spans full width
@@ -171,9 +171,33 @@ func RenderFocusViewWithViewport(mysis MysisInfo, vp viewport.Model, width int,
171171 logTitle := renderSectionTitleWithSuffix("CONVERSATION LOG", scrollInfo, width)
172172 sections = append(sections, logTitle)
173173
174- // Viewport content (scrollable)
175- // Add horizontal padding (2 spaces each side) for content inside panel border
176- vpView := logStyle.Width(width-2).Padding(0, 2).Render(vp.View())
174+ // Viewport content (scrollable) with scrollbar
175+ // Render scrollbar based on viewport state
176+ scrollOffset := vp.YOffset
177+ scrollbarStr := renderScrollbar(vp.Height, totalLines, scrollOffset)
178+ scrollbarLines := strings.Split(scrollbarStr, "\n")
179+
180+ // Get viewport content lines
181+ vpContentLines := strings.Split(vp.View(), "\n")
182+
183+ // Combine each line with scrollbar
184+ combinedLines := make([]string, vp.Height)
185+ for i := 0; i < vp.Height; i++ {
186+ var contentLine string
187+ if i < len(vpContentLines) {
188+ contentLine = vpContentLines[i]
189+ }
190+ var scrollLine string
191+ if i < len(scrollbarLines) {
192+ scrollLine = " " + scrollbarLines[i] // Space before scrollbar
193+ } else {
194+ scrollLine = " " // Empty if no scrollbar line
195+ }
196+ combinedLines[i] = contentLine + scrollLine
197+ }
198+
199+ combinedContent := strings.Join(combinedLines, "\n")
200+ vpView := logStyle.Width(width-2).Padding(0, 2).Render(combinedContent)
177201 sections = append(sections, vpView)
178202
179203 // Footer with scroll hints and verbose toggle
internal/tui/focus_test.go+27−0
@@ -5,6 +5,7 @@ import (
55 "testing"
66 "time"
77
8+ "github.com/charmbracelet/bubbles/viewport"
89 "github.com/charmbracelet/lipgloss"
910 "github.com/muesli/termenv"
1011 "github.com/xonecas/zoea-nova/internal/store"
@@ -142,3 +143,29 @@ func TestRenderLogEntryToolWithJSON(t *testing.T) {
142143 t.Error("Expected JSON field names in tree output")
143144 }
144145 }
146+func TestRenderFocusViewWithScrollbar(t *testing.T) {
147+ lipgloss.SetColorProfile(termenv.TrueColor)
148+ defer lipgloss.SetColorProfile(termenv.Ascii)
149+
150+ // Create viewport with content
151+ vp := viewport.New(80, 10)
152+ longContent := strings.Repeat("line\n", 50) // 50 lines
153+ vp.SetContent(longContent)
154+ vp.GotoTop()
155+
156+ mysis := MysisInfo{
157+ ID: "test-id",
158+ Name: "test-mysis",
159+ State: "running",
160+ Provider: "ollama",
161+ }
162+
163+ width := 100
164+ totalLines := 50
165+ output := RenderFocusViewWithViewport(mysis, vp, width, false, "⬡", true, false, totalLines)
166+
167+ // Should contain scrollbar characters
168+ if !strings.Contains(output, "█") && !strings.Contains(output, "│") {
169+ t.Error("Expected scrollbar characters in focus view output")
170+ }
171+}
internal/tui/scrollbar.go+71−0
@@ -0,0 +1,71 @@
1+package tui
2+
3+import (
4+ "strings"
5+
6+ "github.com/charmbracelet/lipgloss"
7+)
8+
9+const (
10+ scrollbarThumb = "█" // Solid block for thumb
11+ scrollbarTrack = "│" // Thin vertical line for track
12+)
13+
14+// renderScrollbar creates a vertical scrollbar indicator.
15+// height: viewport height in lines
16+// totalLines: total content lines
17+// scrollOffset: current scroll position (line number at top of viewport)
18+// Returns a multi-line string with one character per line.
19+func renderScrollbar(height int, totalLines int, scrollOffset int) string {
20+ if height <= 0 {
21+ return ""
22+ }
23+
24+ // If content fits in viewport, show empty track
25+ if totalLines <= height {
26+ track := make([]string, height)
27+ trackStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240")) // Dim gray
28+ for i := 0; i < height; i++ {
29+ track[i] = trackStyle.Render(scrollbarTrack)
30+ }
31+ return strings.Join(track, "\n")
32+ }
33+
34+ // Calculate thumb position and size
35+ // Thumb size is proportional to viewport/content ratio, minimum 1 line
36+ thumbSize := (height * height) / totalLines
37+ if thumbSize < 1 {
38+ thumbSize = 1
39+ }
40+ if thumbSize > height {
41+ thumbSize = height
42+ }
43+
44+ // Calculate thumb position based on scroll offset
45+ // Position is proportional to scroll position
46+ scrollRatio := float64(scrollOffset) / float64(totalLines-height)
47+ if scrollRatio < 0 {
48+ scrollRatio = 0
49+ }
50+ if scrollRatio > 1 {
51+ scrollRatio = 1
52+ }
53+
54+ maxThumbPos := height - thumbSize
55+ thumbPos := int(scrollRatio * float64(maxThumbPos))
56+
57+ // Build scrollbar lines
58+ lines := make([]string, height)
59+ trackStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240")) // Dim gray
60+ thumbStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("245")) // Lighter gray
61+
62+ for i := 0; i < height; i++ {
63+ if i >= thumbPos && i < thumbPos+thumbSize {
64+ lines[i] = thumbStyle.Render(scrollbarThumb)
65+ } else {
66+ lines[i] = trackStyle.Render(scrollbarTrack)
67+ }
68+ }
69+
70+ return strings.Join(lines, "\n")
71+}
internal/tui/scrollbar_test.go+84−0
@@ -0,0 +1,84 @@
1+package tui
2+
3+import (
4+ "strings"
5+ "testing"
6+)
7+
8+func TestRenderScrollbar_AtTop(t *testing.T) {
9+ height := 10
10+ totalLines := 100
11+ scrollOffset := 0
12+
13+ bar := renderScrollbar(height, totalLines, scrollOffset)
14+
15+ // Should be height lines
16+ lines := strings.Split(bar, "\n")
17+ if len(lines) != height {
18+ t.Errorf("Expected %d lines, got %d", height, len(lines))
19+ }
20+
21+ // First line should have thumb indicator
22+ if !strings.Contains(lines[0], "█") && !strings.Contains(lines[0], "▓") {
23+ t.Error("Expected thumb indicator in first line when at top")
24+ }
25+}
26+
27+func TestRenderScrollbar_AtBottom(t *testing.T) {
28+ height := 10
29+ totalLines := 100
30+ scrollOffset := 90 // At bottom (90 + 10 = 100)
31+
32+ bar := renderScrollbar(height, totalLines, scrollOffset)
33+
34+ lines := strings.Split(bar, "\n")
35+ if len(lines) != height {
36+ t.Errorf("Expected %d lines, got %d", height, len(lines))
37+ }
38+
39+ // Last line should have thumb indicator
40+ lastLine := lines[len(lines)-1]
41+ if !strings.Contains(lastLine, "█") && !strings.Contains(lastLine, "▓") {
42+ t.Error("Expected thumb indicator in last line when at bottom")
43+ }
44+}
45+
46+func TestRenderScrollbar_Middle(t *testing.T) {
47+ height := 10
48+ totalLines := 100
49+ scrollOffset := 45 // Roughly middle
50+
51+ bar := renderScrollbar(height, totalLines, scrollOffset)
52+
53+ lines := strings.Split(bar, "\n")
54+
55+ // Thumb should be somewhere in the middle lines (not first or last)
56+ hasThumbInMiddle := false
57+ for i := 2; i < len(lines)-2; i++ {
58+ if strings.Contains(lines[i], "█") || strings.Contains(lines[i], "▓") {
59+ hasThumbInMiddle = true
60+ break
61+ }
62+ }
63+
64+ if !hasThumbInMiddle {
65+ t.Error("Expected thumb indicator in middle lines when scrolled to middle")
66+ }
67+}
68+
69+func TestRenderScrollbar_NoScroll(t *testing.T) {
70+ height := 10
71+ totalLines := 5 // Content fits in viewport
72+ scrollOffset := 0
73+
74+ bar := renderScrollbar(height, totalLines, scrollOffset)
75+
76+ lines := strings.Split(bar, "\n")
77+
78+ // All lines should show track (no thumb needed when everything fits)
79+ for i, line := range lines {
80+ if !strings.Contains(line, "│") && !strings.Contains(line, "║") {
81+ t.Errorf("Expected track character in line %d when no scroll needed", i)
82+ }
83+ }
84+}