gitstack

grindlemire/go-tui code browser

1.9 KB Go 84 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
74
75
76
77
78
79
80
81
82
83
84
// Code generated by tui generate. DO NOT EDIT.
// Source: styling.gsx

package main

import (
	"fmt"

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

type statusApp struct {
	value *tui.State[int]
}

func StatusApp() *statusApp {
	return &statusApp{
		value: tui.NewState(0),
	}
}

func (s *statusApp) 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) {
			s.value.Update(func(v int) int { return v + 1 })
		}),
		tui.On(tui.Rune('-'), func(ke tui.KeyEvent) {
			s.value.Update(func(v int) int { return v - 1 })
		}),
	}
}

func valueStyle(v int) tui.Style {
	if v > 0 {
		return tui.NewStyle().Bold().Foreground(tui.Green)
	}
	if v < 0 {
		return tui.NewStyle().Bold().Foreground(tui.Red)
	}
	return tui.NewStyle().Dim()
}

func (s *statusApp) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithAlign(tui.AlignCenter),
		tui.WithJustify(tui.JustifyCenter),
		tui.WithHeightPercent(100.00),
		tui.WithGap(1),
	)
	__tui_1 := tui.New(
		tui.WithText(fmt.Sprintf("Value: %d", s.value.Get())),
		tui.WithTextStyle(valueStyle(s.value.Get())),
	)
	__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 (s *statusApp) bindAppFields(app *tui.App) {
	if s.value != nil {
		s.value.BindApp(app)
	}
}

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

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

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