gitstack

grindlemire/go-tui code browser

1.6 KB Go 73 lines 2026-04-18 · dcf2824 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
// Code generated by tui generate. DO NOT EDIT.
// Source: counter.gsx

package main

import (
	"fmt"

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

type counter struct {
	count *tui.State[int]
}

func NewCounter() *counter {
	return &counter{
		count: tui.NewState(0),
	}
}

func (c *counter) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('+'), func(ke tui.KeyEvent) {
			c.count.Update(func(v int) int { return v + 1 })
		}),
		tui.On(tui.Rune('-'), func(ke tui.KeyEvent) {
			c.count.Update(func(v int) int { return v - 1 })
		}),
	}
}

func (c *counter) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithFlexGrow(1),
		tui.WithAlign(tui.AlignCenter),
		tui.WithJustify(tui.JustifyCenter),
	)
	__tui_1 := tui.New(
		tui.WithText(fmt.Sprintf("Count: %d", c.count.Get())),
		tui.WithTextStyle(tui.NewStyle().Bold()),
	)
	__tui_0.AddChild(__tui_1)
	__tui_2 := tui.New(
		tui.WithText("Press + / - to change, Esc to quit"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_0.AddChild(__tui_2)

	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 (c *counter) bindAppFields(app *tui.App) {
	if c.count != nil {
		c.count.BindApp(app)
	}
}

func (c *counter) BindApp(app *tui.App) {
	c.bindAppFields(app)
}

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

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