gitstack

grindlemire/go-tui code browser

5.1 KB Go 198 lines 2026-06-12 · 57378db 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Code generated by tui generate. DO NOT EDIT.
// Source: scrolling.gsx

package main

import (
	"fmt"

	tui "github.com/grindlemire/go-tui"
)

type fileList struct {
	files         []string
	selected      *tui.State[int]
	scrollY       *tui.State[int]
	hideScrollbar *tui.State[bool]
	content       *tui.Ref
}

func FileList() *fileList {
	files := []string{
		"main.go", "app.go", "app_loop.go", "app_render.go",
		"buffer.go", "cell.go", "color.go", "component.go",
		"dirty.go", "element.go", "element_options.go",
		"element_render.go", "element_scroll.go", "escape.go",
		"event.go", "focus.go", "key.go", "keymap.go",
		"layout.go", "mount.go", "ref.go", "render.go",
		"state.go", "style.go", "terminal.go", "watcher.go",
	}
	return &fileList{
		files:         files,
		selected:      tui.NewState(0),
		scrollY:       tui.NewState(0),
		hideScrollbar: tui.NewState(false),
		content:       tui.NewRef(),
	}
}

func (f *fileList) scrollBy(delta int) {
	el := f.content.El()
	if el == nil {
		return
	}
	_, maxY := el.MaxScroll()
	newY := f.scrollY.Get() + delta
	if newY < 0 {
		newY = 0
	}
	if newY > maxY {
		newY = maxY
	}
	f.scrollY.Set(newY)
}

func (f *fileList) moveTo(idx int) {
	if idx < 0 {
		idx = len(f.files) - 1
	}
	if idx >= len(f.files) {
		idx = 0
	}
	f.selected.Set(idx)

	// Keep selected item visible by adjusting scroll
	el := f.content.El()
	if el == nil {
		return
	}
	_, vpH := el.ViewportSize()
	y := f.scrollY.Get()
	if idx < y {
		f.scrollY.Set(idx)
	} else if idx >= y+vpH {
		f.scrollY.Set(idx - vpH + 1)
	}
}

func (f *fileList) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('j'), func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() + 1) }),
		tui.On(tui.Rune('k'), func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() - 1) }),
		tui.On(tui.KeyDown, func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() + 1) }),
		tui.On(tui.KeyUp, func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() - 1) }),
		tui.On(tui.KeyPageDown, func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() + 10) }),
		tui.On(tui.KeyPageUp, func(ke tui.KeyEvent) { f.moveTo(f.selected.Get() - 10) }),
		tui.On(tui.KeyHome, func(ke tui.KeyEvent) { f.moveTo(0) }),
		tui.On(tui.KeyEnd, func(ke tui.KeyEvent) { f.moveTo(len(f.files) - 1) }),
		tui.On(tui.Rune('h'), func(ke tui.KeyEvent) { f.hideScrollbar.Set(!f.hideScrollbar.Get()) }),
	}
}

func (f *fileList) HandleMouse(me tui.MouseEvent) bool {
	switch me.Button {
	case tui.MouseWheelUp:
		f.scrollBy(-3)
		return true
	case tui.MouseWheelDown:
		f.scrollBy(3)
		return true
	}
	return false
}

func (f *fileList) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithPadding(1),
		tui.WithBorder(tui.BorderRounded),
		tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
	)
	__tui_1 := tui.New(
		tui.WithText("Files"),
		tui.WithTextGradient(tui.NewGradient(tui.Cyan, tui.Magenta).WithDirection(tui.GradientHorizontal)),
		tui.WithTextStyle(tui.NewStyle().Bold()),
	)
	__tui_0.AddChild(__tui_1)
	__tui_2 := tui.New(
		tui.WithScrollable(tui.ScrollVertical),
		tui.WithScrollbarStyle(tui.NewStyle().Foreground(tui.Cyan)),
		tui.WithScrollbarThumbStyle(tui.NewStyle().Foreground(tui.BrightCyan)),
		tui.WithHeight(12),
		tui.WithScrollbarHidden(f.hideScrollbar.Get()),
		tui.WithScrollOffset(0, f.scrollY.Get()),
	)
	f.content.Set(__tui_2)
	for i, name := range f.files {
		_ = i
		if i == f.selected.Get() {
			__tui_3 := tui.New(
				tui.WithText(fmt.Sprintf(" > %s ", name)),
				tui.WithBackground(tui.NewStyle().Background(tui.BrightBlack)),
				tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan).Bold()),
			)
			__tui_2.AddChild(__tui_3)
		} else {
			__tui_4 := tui.New(
				tui.WithText(fmt.Sprintf("   %s", name)),
			)
			__tui_2.AddChild(__tui_4)
		}
	}
	__tui_0.AddChild(__tui_2)
	__tui_5 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row),
		tui.WithJustify(tui.JustifySpaceBetween),
	)
	__tui_6 := tui.New(
		tui.WithText("j/k navigate | h toggle scrollbar | esc quit"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_5.AddChild(__tui_6)
	__tui_7 := tui.New(
		tui.WithText(fmt.Sprintf("%d/%d", f.selected.Get()+1, len(f.files))),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_5.AddChild(__tui_7)
	__tui_0.AddChild(__tui_5)

	return __tui_0
}

func (f *fileList) UpdateProps(fresh tui.Component) {
	ff, ok := fresh.(*fileList)
	if !ok {
		return
	}
	f.files = ff.files
}

var _ tui.PropsUpdater = (*fileList)(nil)

// bindAppFields is generated. It wires the component's *tui.App,
// State, Events, and TextArea fields to app. When you override BindApp,
// call this helper instead of hand-maintaining the delegation list.
func (f *fileList) bindAppFields(app *tui.App) {
	if f.selected != nil {
		f.selected.BindApp(app)
	}
	if f.scrollY != nil {
		f.scrollY.BindApp(app)
	}
	if f.hideScrollbar != nil {
		f.hideScrollbar.BindApp(app)
	}
}

func (f *fileList) BindApp(app *tui.App) {
	f.bindAppFields(app)
}

var _ tui.AppBinder = (*fileList)(nil)

// Compile-time interface satisfaction checks.
var (
	_ tui.KeyListener   = (*fileList)(nil)
	_ tui.MouseListener = (*fileList)(nil)
)