gitstack

grindlemire/go-tui code browser

7.7 KB Go 340 lines 2026-07-10 ยท 0592ab2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Code generated by tui generate. DO NOT EDIT.
// Source: stream.gsx

package main

import (
	"fmt"
	"math/rand/v2"
	"time"

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

var gradient = tui.NewGradient(tui.BrightCyan, tui.BrightMagenta)

var phrases = []string{
	"The quick brown fox jumps over the lazy dog.",
	"Stars scattered across the midnight canvas, each one a whisper of ancient light.",
	"Line one of a multi-line message.\nLine two continues here.\nAnd line three wraps it up.",
	"Streaming text appears character by character, just like a real-time API response.",
}

type streamDemo struct {
	app       *tui.App
	phraseIdx int
	streaming *tui.State[bool]
}

type person struct {
	Name   string
	Role   string
	Status string
	Score  int
}

var (
	allNames     = []string{"Alice", "Bob", "Carol", "Dave", "Eve", "Frank"}
	allRoles     = []string{"Engineer", "Designer", "PM", "Analyst", "DevOps", "QA"}
	allStatuses  = []string{"Active", "Away", "Busy", "Offline"}
	statusColors = map[string]tui.Color{
		"Active":  tui.Green,
		"Away":    tui.Yellow,
		"Busy":    tui.Red,
		"Offline": tui.BrightBlack,
	}
)

func StreamDemo() *streamDemo {
	return &streamDemo{
		streaming: tui.NewState(false),
	}
}

func randomPeople() []person {
	n := 3 + rand.IntN(3)
	used := map[int]bool{}
	people := make([]person, 0, n)
	for range n {
		idx := rand.IntN(len(allNames))
		for used[idx] {
			idx = rand.IntN(len(allNames))
		}
		used[idx] = true
		people = append(people, person{
			Name:   allNames[idx],
			Role:   allRoles[rand.IntN(len(allRoles))],
			Status: allStatuses[rand.IntN(len(allStatuses))],
			Score:  50 + rand.IntN(51),
		})
	}
	return people
}

func (s *streamDemo) streamWithElement() {
	if s.streaming.Get() {
		return
	}
	s.streaming.Set(true)

	go func() {
		w := s.app.StreamAbove()
		w.WriteGradient("Here's a summary:\n", gradient)
		time.Sleep(100 * time.Millisecond)

		w.WriteElement(ReportCard(randomPeople()))

		time.Sleep(100 * time.Millisecond)
		w.WriteGradient("Done!\n", gradient)
		w.Close()
		s.app.QueueUpdate(func() {
			s.streaming.Set(false)
		})
	}()
}

func (s *streamDemo) streamPhrase() {
	if s.streaming.Get() {
		return
	}
	s.streaming.Set(true)

	text := phrases[s.phraseIdx%len(phrases)]
	s.phraseIdx++

	go func() {
		w := s.app.StreamAbove()
		for _, r := range text {
			w.WriteGradient(string(r), gradient)
			time.Sleep(30 * time.Millisecond)
		}
		w.Close()
		s.app.QueueUpdate(func() {
			s.streaming.Set(false)
		})
	}()
}

func (s *streamDemo) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.OnStop(tui.KeyEnter, func(ke tui.KeyEvent) { s.streamPhrase() }),
		tui.OnStop(tui.KeyTab, func(ke tui.KeyEvent) { s.streamWithElement() }),
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('c').Ctrl(), func(ke tui.KeyEvent) { ke.App().Stop() }),
	}
}

func (s *streamDemo) statusText() string {
	if s.streaming.Get() {
		return "streaming..."
	}
	return "Enter to stream  |  Tab to stream with element  |  Esc to quit"
}

type ReportCardView struct {
	Root      *tui.Element
	watchers  []tui.Watcher
	bindApp   func(*tui.App)
	unbindApp func()
}

func (v *ReportCardView) UnbindApp() {
	if v.unbindApp != nil {
		v.unbindApp()
	}
}

func (v *ReportCardView) GetRoot() *tui.Element { return v.Root }

func (v *ReportCardView) GetWatchers() []tui.Watcher { return v.watchers }

func (v *ReportCardView) Render(app *tui.App) *tui.Element { return v.Root }

func (v *ReportCardView) BindApp(app *tui.App) {
	if v.bindApp != nil {
		v.bindApp(app)
	}
}

func (v *ReportCardView) UpdateProps(fresh tui.Component) {
	f, ok := fresh.(*ReportCardView)
	if !ok {
		return
	}
	v.Root = f.Root
	v.watchers = f.watchers
	v.bindApp = f.bindApp
	v.unbindApp = f.unbindApp
}

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

var _ tui.AppUnbinder = (*ReportCardView)(nil)

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

func ReportCard(people []person) *ReportCardView {
	var view ReportCardView
	var watchers []tui.Watcher

	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row),
		tui.WithJustify(tui.JustifyCenter),
	)
	__tui_1 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithBorder(tui.BorderRounded),
		tui.WithWidthPercent(75.00),
		tui.WithPaddingTRBL(0, 1, 0, 1),
		tui.WithBorderStyle(tui.NewStyle().Foreground(tui.BrightCyan)),
	)
	__tui_2 := tui.New(
		tui.WithText("Streaming Report"),
		tui.WithTextStyle(tui.NewStyle().Bold().Foreground(tui.BrightMagenta)),
	)
	__tui_1.AddChild(__tui_2)
	__tui_3 := tui.New(
		tui.WithTag("table"),
		tui.WithDisplay(tui.DisplayFlex),
		tui.WithDirection(tui.Column),
	)
	__tui_4 := tui.New(
		tui.WithTag("tr"),
		tui.WithDisplay(tui.DisplayFlex),
		tui.WithDirection(tui.Row),
	)
	__tui_5 := tui.New(
		tui.WithTag("th"),
		tui.WithText("Name"),
		tui.WithFlexGrow(1),
	)
	__tui_4.AddChild(__tui_5)
	__tui_6 := tui.New(
		tui.WithTag("th"),
		tui.WithText("Role"),
		tui.WithFlexGrow(1),
	)
	__tui_4.AddChild(__tui_6)
	__tui_7 := tui.New(
		tui.WithTag("th"),
		tui.WithText("Status"),
		tui.WithFlexGrow(1),
	)
	__tui_4.AddChild(__tui_7)
	__tui_8 := tui.New(
		tui.WithTag("th"),
		tui.WithText("Score"),
	)
	__tui_4.AddChild(__tui_8)
	__tui_3.AddChild(__tui_4)
	__tui_9 := tui.New(
		tui.WithHR(),
	)
	__tui_3.AddChild(__tui_9)
	for __idx_0, p := range people {
		_ = __idx_0
		__tui_10 := tui.New(
			tui.WithTag("tr"),
			tui.WithDisplay(tui.DisplayFlex),
			tui.WithDirection(tui.Row),
		)
		__tui_11 := tui.New(
			tui.WithTag("td"),
			tui.WithText(p.Name),
			tui.WithFlexGrow(1),
			tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan)),
		)
		__tui_10.AddChild(__tui_11)
		__tui_12 := tui.New(
			tui.WithTag("td"),
			tui.WithText(p.Role),
			tui.WithFlexGrow(1),
		)
		__tui_10.AddChild(__tui_12)
		__tui_13 := tui.New(
			tui.WithTag("td"),
			tui.WithText(p.Status),
			tui.WithFlexGrow(1),
			tui.WithTextStyle(tui.NewStyle().Foreground(statusColors[p.Status])),
		)
		__tui_10.AddChild(__tui_13)
		__tui_14 := tui.New(
			tui.WithTag("td"),
			tui.WithText(fmt.Sprintf("%d", p.Score)),
			tui.WithTextStyle(tui.NewStyle().Bold()),
		)
		__tui_10.AddChild(__tui_14)
		__tui_3.AddChild(__tui_10)
	}
	__tui_1.AddChild(__tui_3)
	__tui_0.AddChild(__tui_1)

	__bindApp := func(app *tui.App) {
	}

	__unbindApp := func() {
	}

	view = ReportCardView{
		Root:      __tui_0,
		watchers:  watchers,
		bindApp:   __bindApp,
		unbindApp: __unbindApp,
	}
	return &view
}

func (s *streamDemo) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithBorder(tui.BorderRounded),
		tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
		tui.WithAlign(tui.AlignCenter),
		tui.WithJustify(tui.JustifyCenter),
	)
	__tui_1 := tui.New(
		tui.WithText(s.statusText()),
		tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan)),
	)
	__tui_0.AddChild(__tui_1)

	return __tui_0
}

// updatePropsFields is generated. It copies prop fields from fresh onto
// the receiver. When you override UpdateProps, call this helper instead
// of hand-maintaining the copy list.
func (s *streamDemo) updatePropsFields(fresh tui.Component) {
	f, ok := fresh.(*streamDemo)
	if !ok {
		return
	}
	s.app = f.app
	s.phraseIdx = f.phraseIdx
}

func (s *streamDemo) UpdateProps(fresh tui.Component) {
	s.updatePropsFields(fresh)
}

var _ tui.PropsUpdater = (*streamDemo)(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 (s *streamDemo) bindAppFields(app *tui.App) {
	s.app = app
	if s.streaming != nil {
		s.streaming.BindApp(app)
	}
}

func (s *streamDemo) BindApp(app *tui.App) {
	s.bindAppFields(app)
}

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

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