gitstack

grindlemire/go-tui code browser

4.1 KB 165 lines 2026-03-15 · e5befc1 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
package main

import (
	"fmt"
	"math/rand/v2"
	"time"
	tui "github.com/grindlemire/go-tui"
)

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

// phrases to cycle through when the user presses Enter.
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]
}

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

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 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
}

templ ReportCard(people []person) {
	<div class="flex justify-center">
		<div class="flex-col border-rounded w-3/4 px-1" borderStyle={tui.NewStyle().Foreground(tui.BrightCyan)}>
			<span class="font-bold text-bright-magenta">Streaming Report</span>
			<table>
				<tr>
					<th class="grow">Name</th>
					<th class="grow">Role</th>
					<th class="grow">Status</th>
					<th>Score</th>
				</tr>
				<hr />
				for _, p := range people {
					<tr>
						<td class="text-cyan grow">{p.Name}</td>
						<td class="grow">{p.Role}</td>
						<td class="grow" textStyle={tui.NewStyle().Foreground(statusColors[p.Status])}>{p.Status}</td>
						<td class="font-bold">{fmt.Sprintf("%d", p.Score)}</td>
					</tr>
				}
			</table>
		</div>
	</div>
}

// streamWithElement opens a StreamAbove writer, writes gradient intro text,
// inserts a styled card with a randomized table via WriteElement, then writes
// gradient outro text.
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)
		})
	}()
}

// streamPhrase opens a StreamAbove writer, writes each character with a
// gradient color and a small delay, then closes the writer.
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"
}

templ (s *streamDemo) Render() {
	<div class="border-rounded border-cyan items-center justify-center">
		<span class="text-cyan">{s.statusText()}</span>
	</div>
}