xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / scrollbar.go

1.9 KB
1package tui
2
3import (
4	"strings"
5
6	"github.com/charmbracelet/lipgloss"
7)
8
9const (
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.
19func 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(colorBorder) // Purple-tinted border
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(colorBorder) // Purple-tinted border
60	thumbStyle := lipgloss.NewStyle().Foreground(colorBorder) // Purple-tinted border (same color)
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}