gitstack

grindlemire/go-tui code browser

4.4 KB Go 177 lines 2026-06-17 · 7d3f10d raw
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
package tui

import (
	"testing"
)

func TestElement_Scroll(t *testing.T) {
	type tc struct {
		setup func(e *Element)
		check func(t *testing.T, e *Element)
	}

	tests := map[string]tc{
		"default scroll mode is none": {
			setup: func(e *Element) {},
			check: func(t *testing.T, e *Element) {
				if e.ScrollModeValue() != ScrollNone {
					t.Errorf("got %v, want ScrollNone", e.ScrollModeValue())
				}
			},
		},
		"scrollable sets vertical scroll": {
			setup: func(e *Element) {
				WithScrollable(ScrollVertical)(e)
			},
			check: func(t *testing.T, e *Element) {
				if !e.IsScrollable() {
					t.Error("expected IsScrollable() = true")
				}
			},
		},
		"scroll offset starts at zero": {
			setup: func(e *Element) {},
			check: func(t *testing.T, e *Element) {
				x, y := e.ScrollOffset()
				if x != 0 || y != 0 {
					t.Errorf("scroll offset = (%d, %d), want (0, 0)", x, y)
				}
			},
		},
		"ScrollMode vertical": {
			setup: func(e *Element) {
				WithScrollable(ScrollVertical)(e)
			},
			check: func(t *testing.T, e *Element) {
				if e.ScrollModeValue() != ScrollVertical {
					t.Errorf("got %v, want ScrollVertical", e.ScrollModeValue())
				}
			},
		},
		"ScrollMode horizontal": {
			setup: func(e *Element) {
				WithScrollable(ScrollHorizontal)(e)
			},
			check: func(t *testing.T, e *Element) {
				if e.ScrollModeValue() != ScrollHorizontal {
					t.Errorf("got %v, want ScrollHorizontal", e.ScrollModeValue())
				}
			},
		},
		"ScrollMode both": {
			setup: func(e *Element) {
				WithScrollable(ScrollBoth)(e)
			},
			check: func(t *testing.T, e *Element) {
				if e.ScrollModeValue() != ScrollBoth {
					t.Errorf("got %v, want ScrollBoth", e.ScrollModeValue())
				}
			},
		},
		"not scrollable by default": {
			setup: func(e *Element) {},
			check: func(t *testing.T, e *Element) {
				if e.IsScrollable() {
					t.Error("expected IsScrollable() = false by default")
				}
			},
		},
		"ScrollTo with no content stays at zero": {
			setup: func(e *Element) {
				e.ScrollTo(5, 5)
			},
			check: func(t *testing.T, e *Element) {
				x, y := e.ScrollOffset()
				if x != 0 || y != 0 {
					t.Errorf("scroll offset = (%d, %d), want (0, 0) when no content", x, y)
				}
			},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			e := New(WithSize(20, 10))
			tt.setup(e)
			tt.check(t, e)
		})
	}
}

func TestElement_ScrollbarHidden(t *testing.T) {
	const width, height = 20, 5

	makeScrollable := func(opts ...Option) *Element {
		opts = append([]Option{
			WithSize(width, height),
			WithScrollable(ScrollVertical),
			WithDirection(Column),
		}, opts...)
		e := New(opts...)
		// More rows than viewport so a scrollbar would be needed.
		for range height * 3 {
			e.AddChild(New(WithHeight(1), WithBackground(NewStyle().Background(Red))))
		}
		return e
	}

	t.Run("default shows scrollbar and reserves gutter", func(t *testing.T) {
		e := makeScrollable()
		buf := NewBuffer(width, height)
		e.Render(buf, width, height)

		if !e.needsVerticalScrollbar() {
			t.Fatalf("expected scrollbar to be needed")
		}

		// Rightmost column should be the scrollbar, not the child's red fill.
		for y := range height {
			cell := buf.Cell(width-1, y)
			if cell.Text != "█" && cell.Text != "│" {
				t.Errorf("row %d rightmost column rune = %q, want scrollbar glyph", y, cell.Text)
			}
			if cell.Style.Bg.Equal(Red) {
				t.Errorf("row %d rightmost column bg = Red, expected scrollbar to cover child", y)
			}
		}
	})

	t.Run("hidden skips scrollbar and reclaims gutter", func(t *testing.T) {
		e := makeScrollable(WithScrollbarHidden(true))
		buf := NewBuffer(width, height)
		e.Render(buf, width, height)

		if e.needsVerticalScrollbar() {
			t.Fatalf("expected scrollbar to be hidden")
		}

		// Rightmost column should be the child's red background, proving the
		// scrollbar gutter was reclaimed for child layout and not left blank.
		for y := range height {
			cell := buf.Cell(width-1, y)
			if !cell.Style.Bg.Equal(Red) {
				t.Errorf("row %d rightmost column bg = %v, want Red (child fills reclaimed gutter)", y, cell.Style.Bg)
			}
		}
	})
}

func TestElement_ScrollToTop(t *testing.T) {
	e := New(
		WithSize(20, 5),
		WithScrollable(ScrollVertical),
		WithDirection(Column),
	)
	for range 20 {
		e.AddChild(New(WithHeight(1)))
	}
	buf := NewBuffer(20, 5)
	e.Render(buf, 20, 5)

	e.ScrollTo(0, 10)
	e.ScrollToTop()
	_, y := e.ScrollOffset()
	if y != 0 {
		t.Errorf("after ScrollToTop, y = %d, want 0", y)
	}
}