gitstack

grindlemire/go-tui code browser

7.3 KB 276 lines 2026-03-22 · 79551b2 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
package example

import (
	"fmt"
	"strings"
	"time"
	tui "github.com/grindlemire/go-tui"
)

// =============================================================================
// Struct-based component with KeyMap and Watchers
// =============================================================================
type complexApp struct {
	// State
	count        *tui.State[int]
	selected     *tui.State[int]
	items        *tui.State[[]string]
	showHeader   *tui.State[bool]
	showFooter   *tui.State[bool]
	timerSeconds *tui.State[int]
	messages     *tui.State[[]string]
	msgCh        chan string

	// Refs for click handling
	incrementBtn *tui.Ref
	decrementBtn *tui.Ref
	resetBtn     *tui.Ref
	itemRefs     *tui.RefList
}

func ComplexApp() *complexApp {
	msgCh := make(chan string, 10)
	app := &complexApp{
		count:        tui.NewState(0),
		selected:     tui.NewState(0),
		items:        tui.NewState([]string{"Item 1", "Item 2", "Item 3"}),
		showHeader:   tui.NewState(true),
		showFooter:   tui.NewState(true),
		timerSeconds: tui.NewState(0),
		messages:     tui.NewState([]string{}),
		msgCh:        msgCh,
		incrementBtn: tui.NewRef(),
		decrementBtn: tui.NewRef(),
		resetBtn:     tui.NewRef(),
		itemRefs:     tui.NewRefList(),
	}

	// Simulate background message producer
	go func() {
		for i := 0; ; i++ {
			time.Sleep(5 * time.Second)
			msgCh <- fmt.Sprintf("Message #%d received", i+1)
		}
	}()

	return app
}

// KeyMap demonstrates the new keyboard handling API
func (c *complexApp) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		// Quit handlers
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.OnStop(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),

		// Counter controls
		tui.On(tui.Rune('+'), func(ke tui.KeyEvent) { c.count.Set(c.count.Get() + 1) }),
		tui.On(tui.Rune('-'), func(ke tui.KeyEvent) { c.count.Set(c.count.Get() - 1) }),
		tui.On(tui.Rune('r'), func(ke tui.KeyEvent) { c.count.Set(0) }),

		// Navigation
		tui.On(tui.KeyUp, func(ke tui.KeyEvent) {
			if c.selected.Get() > 0 {
				c.selected.Set(c.selected.Get() - 1)
			}
		}),
		tui.On(tui.KeyDown, func(ke tui.KeyEvent) {
			if c.selected.Get() < len(c.items.Get())-1 {
				c.selected.Set(c.selected.Get() + 1)
			}
		}),

		// Toggle visibility
		tui.On(tui.Rune('h'), func(ke tui.KeyEvent) { c.showHeader.Set(!c.showHeader.Get()) }),
		tui.On(tui.Rune('f'), func(ke tui.KeyEvent) { c.showFooter.Set(!c.showFooter.Get()) }),

		// Catch-all for other runes
		tui.On(tui.AnyRune, func(ke tui.KeyEvent) {
			// Handle any other character input
		}),
	}
}

// Watchers demonstrates the new watcher API for timers and channels
func (c *complexApp) Watchers() []tui.Watcher {
	return []tui.Watcher{
		tui.OnTimer(time.Second, c.tick),
		tui.Watch(c.msgCh, c.addMessage),
	}
}

func (c *complexApp) tick() {
	c.timerSeconds.Set(c.timerSeconds.Get() + 1)
}

func (c *complexApp) addMessage(msg string) {
	current := c.messages.Get()
	ts := time.Now().Format("15:04:05")
	entry := fmt.Sprintf("[%s] %s", ts, msg)
	// Keep last 5 messages
	if len(current) >= 5 {
		current = current[1:]
	}
	c.messages.Set(append(current, entry))
}

// HandleMouse demonstrates the new click handling API with refs
func (c *complexApp) HandleMouse(me tui.MouseEvent) bool {
	return tui.HandleClicks(me,
		tui.Click(c.incrementBtn, func() { c.count.Set(c.count.Get() + 1) }),
		tui.Click(c.decrementBtn, func() { c.count.Set(c.count.Get() - 1) }),
		tui.Click(c.resetBtn, func() { c.count.Set(0) }),
	)
}

templ (c *complexApp) Render() {
	<div class="flex-col gap-2 p-2 border-rounded border-cyan">
		<span class="text-gradient-cyan-magenta font-bold">Complex Component Demo</span>

		// Conditional rendering
		if c.showHeader.Get() {
			<div class="border-single p-1">
				<span class="font-bold">Header Section</span>
			</div>
		}
		// Counter with refs for click handling
		<div class="flex-col gap-1 border-rounded p-1">
			<span class="text-cyan font-bold">Counter:{fmt.Sprintf("%d", c.count.Get())}</span>
			<div class="flex gap-1">
				<button ref={c.incrementBtn} class="px-2">+</button>
				<button ref={c.decrementBtn} class="px-2">-</button>
				<button ref={c.resetBtn} class="px-2">Reset</button>
			</div>
		</div>

		// List with selection
		<div class="flex-col gap-1 border-rounded p-1">
			<span class="font-bold">Items(↑/↓to navigate)</span>
			for i, item := range c.items.Get() {
				if i == c.selected.Get() {
					<div ref={c.itemRefs} class="border-single px-1">
						<span class="text-cyan font-bold">{fmt.Sprintf("> %s", item)}</span>
					</div>
				} else {
					<span ref={c.itemRefs} class="font-dim">{fmt.Sprintf("  %s", item)}</span>
				}
			}
		</div>

		// Timer from watcher
		<div class="border-rounded p-1">
			<span class="font-dim">Uptime:{fmt.Sprintf("%d seconds", c.timerSeconds.Get())}</span>
		</div>

		// Messages from channel watcher
		<div class="flex-col gap-1 border-rounded p-1">
			<span class="font-bold">Live Messages</span>
			for _, msg := range c.messages.Get() {
				<span class="text-green">{msg}</span>
			}
			if len(c.messages.Get()) == 0 {
				<span class="font-dim">Waitingmessages...</span>
			}
		</div>

		// Footer with conditional
		if c.showFooter.Get() {
			<div class="border-single p-1">
				<span class="font-dim">Footer Section</span>
			</div>
		} else {
			<span class="font-dim">Footer hidden</span>
		}

		// Help text
		<div class="flex-col gap-1">
			<span class="font-dim">+/-counter|↑/↓navigate|h toggle header|f toggle footer|r reset|q quit</span>
		</div>
	</div>
}

// =============================================================================
// Simple component examples (for syntax testing)
// =============================================================================

// Let bindings
templ LetBindingExample(count int, label string) {
	formattedLabel := fmt.Sprintf("%s:", strings.ToUpper(label))
	countText := <span class="font-bold">{fmt.Sprintf("%d", count)}</span>
	<div class="flex-col gap-1 p-1">
		<span>{formattedLabel}</span>
		{countText}
	</div>
}

// Refs - simple, loop, keyed, conditional
templ RefsExample(items []string, users map[string]string, showWarning bool) {
	container := tui.NewRef()
	titleRef := tui.NewRef()
	itemRefs := tui.NewRefList()
	userRefs := tui.NewRefMap[string]()
	warning := tui.NewRef()
	<div ref={container} class="flex-col gap-1">
		<span ref={titleRef} class="font-bold">Dashboard</span>

		for _, item := range items {
			<span ref={itemRefs}>{item}</span>
		}

		for id, name := range users {
			<span ref={userRefs} key={id}>{name}</span>
		}

		if showWarning {
			<div ref={warning} class="text-red border-single p-1">
				<span>Warning!</span>
			</div>
		}
	</div>
}

// Numeric values
templ NumericValues() {
	<div>
		<span padding={10}>Integer:{42}</span>
		<span>{3.14159}</span>
		<span>{0xFF}</span>
		<span>{0b1010}</span>
		<span>{0o755}</span>
	</div>
}

// String values
templ StringValues() {
	<div>
		<span>{"Hello, World!"}</span>
		<span>{`Raw string with newlines`}</span>
		<span>{"Escaped: \n\t\""}</span>
	</div>
}

// Self-closing elements
templ SelfClosing() {
	<div>
		<hr />
		<br />
	</div>
}

// Attributes with various types
templ AttributeTypes(enabled bool, size int) {
	<div
		border={tui.BorderDouble}
		padding={2}
		margin={1}
		width="100%"
		height={size}
		class="flex-col items-center">
		<span>Content</span>
	</div>
}

// Helper function
func helperFunction(s string) string {
	return fmt.Sprintf("[%s]", strings.ToUpper(s))
}