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
|
package main
import (
"fmt"
tui "github.com/grindlemire/go-tui"
)
type elementsApp struct {
progress *tui.State[int]
scrollY *tui.State[int]
content *tui.Ref
name *tui.State[string]
note *tui.State[string]
selectedBtn *tui.State[string]
btnRefs *tui.RefMap[string]
}
func Elements() *elementsApp {
return &elementsApp{
progress: tui.NewState(62),
scrollY: tui.NewState(0),
content: tui.NewRef(),
name: tui.NewState(""),
note: tui.NewState(""),
selectedBtn: tui.NewState(""),
btnRefs: tui.NewRefMap[string](),
}
}
func (e *elementsApp) onNoteSubmit(text string) {
e.note.Set(text)
}
func greeting(name string) string {
if name == "" {
return "Hello, World!"
}
return fmt.Sprintf("Hello, %s!", name)
}
func (e *elementsApp) scrollBy(delta int) {
el := e.content.El()
if el == nil {
return
}
_, maxY := el.MaxScroll()
newY := e.scrollY.Get() + delta
if newY < 0 {
newY = 0
} else if newY > maxY {
newY = maxY
}
e.scrollY.Set(newY)
}
func (e *elementsApp) KeyMap() tui.KeyMap {
return tui.KeyMap{
tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
tui.On(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
tui.On(tui.KeyTab, func(ke tui.KeyEvent) { ke.App().FocusNext() }),
tui.On(tui.Rune('n'), func(ke tui.KeyEvent) {e.name.Set("John Doe") }),
tui.On(tui.KeyTab.Shift(), func(ke tui.KeyEvent) { ke.App().FocusPrev() }),
tui.On(tui.Rune('+'), func(ke tui.KeyEvent) {
v := e.progress.Get() + 5
if v > 100 {
v = 100
}
e.progress.Set(v)
}),
tui.On(tui.Rune('-'), func(ke tui.KeyEvent) {
v := e.progress.Get() - 5
if v < 0 {
v = 0
}
e.progress.Set(v)
}),
tui.On(tui.Rune('j'), func(ke tui.KeyEvent) { e.scrollBy(1) }),
tui.On(tui.Rune('k'), func(ke tui.KeyEvent) { e.scrollBy(-1) }),
tui.On(tui.KeyDown, func(ke tui.KeyEvent) { e.scrollBy(1) }),
tui.On(tui.KeyUp, func(ke tui.KeyEvent) { e.scrollBy(-1) }),
}
}
func (e *elementsApp) HandleMouse(me tui.MouseEvent) bool {
switch me.Button {
case tui.MouseWheelUp:
e.scrollBy(-1)
return true
case tui.MouseWheelDown:
e.scrollBy(1)
return true
}
if me.Button == tui.MouseLeft && me.Action == tui.MousePress {
for name, el := range e.btnRefs.All() {
if el != nil && el.ContainsPoint(me.X, me.Y) {
if name != "Disabled" {
e.selectedBtn.Set(name)
}
return true
}
}
}
return false
}
var buttonLabels = []string{"Save", "Cancel", "Submit", "Disabled"}
func progressBar(value, width int) string {
filled := value * width / 100
bar := ""
for i := 0; i < width; i++ {
if i < filled {
bar += "█"
} else {
bar += "░"
}
}
return bar
}
templ (e *elementsApp) Render() {
<div
ref={e.content}
class="flex-col gap-1 h-full"
scrollable={tui.ScrollVertical}
scrollOffset={0, e.scrollY.Get()}
>
<span class="text-gradient-cyan-magenta font-bold">Built-in Elements</span>
// Text Elements
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">Text Elements</span>
<p>{"Paragraph text (<p>) wraps automatically when the content exceeds the available width. This demonstrates how longer text content is displayed."}</p>
<hr />
<span class="text-cyan">{"This is a <span> element for inline styled text"}</span>
<br />
<span class="font-dim">{"<hr> above draws a line, <br> inserts a blank line"}</span>
</div>
// Lists and Table side by side
<div class="flex gap-1">
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">{"Lists (<ul> / <li>)"}</span>
<ul class="flex-col p-1">
<li>
<span>First item</span>
</li>
<li>
<span>Second item</span>
</li>
<li>
<span>Third item</span>
</li>
<li>
<span class="text-cyan">Fourth (styled)</span>
</li>
</ul>
</div>
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">Table</span>
<table class="p-1">
<tr>
<th>Name</th>
<th>Role</th>
<th>Lvl</th>
</tr>
<hr />
<tr>
<td class="text-cyan">Alice</td>
<td>Engineer</td>
<td class="text-green">Sr</td>
</tr>
<tr>
<td class="text-cyan">Bob</td>
<td>Designer</td>
<td class="text-yellow">Jr</td>
</tr>
</table>
</div>
</div>
// Buttons
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">Buttons</span>
<div class="flex gap-2">
for _, label := range buttonLabels {
if label == "Disabled" {
<button ref={e.btnRefs} key={label} class="font-dim" disabled={true}>{label}</button>
} else if label == e.selectedBtn.Get() {
<button ref={e.btnRefs} key={label} class="font-bold text-cyan">{label}</button>
} else {
<button ref={e.btnRefs} key={label}>{label}</button>
}
}
</div>
</div>
// Input & TextArea
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">Input & TextArea</span>
<div class="flex gap-2">
<div class="flex-col gap-1 w-1/2">
<div class="flex gap-2 items-center">
<span class="font-dim">Name:</span>
<input
placeholder="Type your name..."
value={e.name}
width={30}
border={tui.BorderRounded}
focusGradient={tui.NewGradient(tui.Cyan, tui.Magenta)}
/>
</div>
<span
class="text-cyan font-bold"
width={30}>
{greeting(e.name.Get())}
</span>
</div>
<div class="flex-col gap-1 w-1/2">
<div class="flex gap-2 items-center">
<span class="font-dim">Note:</span>
<textarea
placeholder="Write a note..."
width={30}
maxHeight={4}
border={tui.BorderRounded}
onSubmit={e.onNoteSubmit}
focusColor={tui.BrightRed}
/>
</div>
if e.note.Get() != "" {
<span class="text-cyan font-bold">{fmt.Sprintf("Saved: %s", e.note.Get())}</span>
}
</div>
</div>
<span class="font-dim">Tab to cycle focus | Esc to blur | Enter submits note</span>
</div>
// Progress bars (using custom rendering since <progress> attributes aren't supported yet)
<div class="flex-col border-rounded p-1 gap-1">
<span class="text-gradient-cyan-magenta font-bold">Progress Bars</span>
<div class="flex gap-2 items-center">
<span class="font-dim w-10">Download:</span>
<span class="text-cyan">{progressBar(e.progress.Get(), 25)}</span>
<span class="text-cyan font-bold">{fmt.Sprintf("%d%%", e.progress.Get())}</span>
</div>
<div class="flex gap-2 items-center">
<span class="font-dim w-10">Upload:</span>
<span class="text-green">{progressBar(100, 25)}</span>
<span class="text-green font-bold">{"100%"}</span>
</div>
<div class="flex gap-2 items-center">
<span class="font-dim w-10">Build:</span>
<span class="text-yellow">{progressBar(35, 25)}</span>
<span class="text-yellow font-bold">{"35%"}</span>
</div>
</div>
<span class="font-dim">tab focus input | +/- progress | j/k scroll | q quit</span>
</div>
}
|