gitstack

grindlemire/go-tui code browser

6.1 KB Go 258 lines 2026-05-18 · c9ecaf5 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Code generated by tui generate. DO NOT EDIT.
// Source: dump.gsx

package main

import (
	"fmt"

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

const maxEventLog = 500

type eventDump struct {
	log     *tui.State[[]string]
	scrollY *tui.State[int]
	// following is wrapped in State[bool] so the generated UpdateProps treats
	// it as framework-managed and does NOT overwrite it from a freshly-
	// constructed eventDump every render. A plain bool field would get
	// reset to its constructor value on every re-render, causing scroll
	// position to snap back to the bottom.
	following *tui.State[bool]
	// seq is a monotonically increasing counter prefixed onto every log line
	// so the entries are visually distinct even when the same key is pressed
	// repeatedly. Wrapped in State so the generator treats it as framework-
	// managed and doesn't reset it across renders.
	seq  *tui.State[int]
	feed *tui.Ref
}

func EventDump() *eventDump {
	return &eventDump{
		log:       tui.NewState([]string{}),
		scrollY:   tui.NewState(0),
		following: tui.NewState(true),
		seq:       tui.NewState(0),
		feed:      tui.NewRef(),
	}
}

func (e *eventDump) Watchers() []tui.Watcher {
	return []tui.Watcher{
		tui.OnChange(e.log, e.autoScrollToBottom),
	}
}

func (e *eventDump) autoScrollToBottom(_ []string) {
	if !e.following.Get() {
		return
	}
	el := e.feed.El()
	if el == nil {
		return
	}
	_, maxY := el.MaxScroll()
	e.scrollY.Set(maxY)
}

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

func (e *eventDump) append(line string) {
	n := e.seq.Get() + 1
	e.seq.Set(n)
	tagged := fmt.Sprintf("#%03d  %s", n, line)
	e.log.Update(func(prev []string) []string {
		next := append(prev, tagged)
		if len(next) > maxEventLog {
			next = next[len(next)-maxEventLog:]
		}
		return next
	})
}

func (e *eventDump) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		// High-priority quit handlers stop propagation so the AnyKey catch-all
		// below doesn't double-log the quit keystroke.
		tui.OnStop(tui.KeyEscape, func(ke tui.KeyEvent) {
			e.append(formatKey(ke) + "  [QUIT]")
			ke.App().Stop()
		}),
		tui.OnStop(tui.Rune('q'), func(ke tui.KeyEvent) {
			e.append(formatKey(ke) + "  [QUIT]")
			ke.App().Stop()
		}),
		tui.On(tui.AnyKey, func(ke tui.KeyEvent) {
			e.append(formatKey(ke))
		}),
	}
}

func (e *eventDump) statusLine() string {
	stateY := e.scrollY.Get()
	following := e.following.Get()
	entries := len(e.log.Get())
	elY, contentH, viewportH := -1, -1, -1
	if el := e.feed.El(); el != nil {
		_, elY = el.ScrollOffset()
		_, contentH = el.ContentSize()
		_, viewportH = el.ViewportSize()
	}
	return fmt.Sprintf(
		"STATUS: stateY=%d elY=%d  following=%v  entries=%d  contentH=%d viewportH=%d",
		stateY, elY, following, entries, contentH, viewportH,
	)
}

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

func formatKey(ke tui.KeyEvent) string {
	if ke.Key == tui.KeyRune {
		return fmt.Sprintf("KEY  Rune=%q  Mod=%s", ke.Rune, ke.Mod)
	}
	return fmt.Sprintf("KEY  %s  Mod=%s", ke.Key, ke.Mod)
}

func formatMouse(me tui.MouseEvent) string {
	return fmt.Sprintf("MOUSE  %s %s  X=%d Y=%d  Mod=%s",
		buttonString(me.Button), actionString(me.Action), me.X, me.Y, me.Mod)
}

func buttonString(b tui.MouseButton) string {
	switch b {
	case tui.MouseLeft:
		return "Left"
	case tui.MouseMiddle:
		return "Middle"
	case tui.MouseRight:
		return "Right"
	case tui.MouseWheelUp:
		return "WheelUp"
	case tui.MouseWheelDown:
		return "WheelDown"
	case tui.MouseNone:
		return "None"
	}
	return "?"
}

func actionString(a tui.MouseAction) string {
	switch a {
	case tui.MousePress:
		return "Press"
	case tui.MouseRelease:
		return "Release"
	case tui.MouseDrag:
		return "Drag"
	}
	return "?"
}

func (e *eventDump) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithGap(1),
		tui.WithPadding(1),
		tui.WithBorder(tui.BorderRounded),
		tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
		tui.WithFlexGrow(1),
	)
	__tui_1 := tui.New(
		tui.WithText("Event Dump"),
		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.WithText(e.statusLine()),
		tui.WithTextStyle(tui.NewStyle().Foreground(tui.Yellow).Bold()),
	)
	__tui_0.AddChild(__tui_2)
	__tui_3 := tui.New(
		tui.WithText("Press any key or click/scroll the mouse. Esc or q to quit."),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_0.AddChild(__tui_3)
	__tui_4 := tui.New(
		tui.WithHR(),
		tui.WithBorder(tui.BorderSingle),
	)
	__tui_0.AddChild(__tui_4)
	__tui_5 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithFlexGrow(1),
		tui.WithScrollable(tui.ScrollVertical),
		tui.WithScrollbarHidden(true),
		tui.WithScrollOffset(0, e.scrollY.Get()),
	)
	e.feed.Set(__tui_5)
	for __idx_0, line := range e.log.Get() {
		_ = __idx_0
		__tui_6 := tui.New(
			tui.WithText(line),
			tui.WithTruncate(true),
		)
		__tui_5.AddChild(__tui_6)
	}
	__tui_0.AddChild(__tui_5)

	return __tui_0
}

// 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 (e *eventDump) bindAppFields(app *tui.App) {
	if e.log != nil {
		e.log.BindApp(app)
	}
	if e.scrollY != nil {
		e.scrollY.BindApp(app)
	}
	if e.following != nil {
		e.following.BindApp(app)
	}
	if e.seq != nil {
		e.seq.BindApp(app)
	}
}

func (e *eventDump) BindApp(app *tui.App) {
	e.bindAppFields(app)
}

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

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