gitstack

grindlemire/go-tui code browser

4.2 KB Go 144 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
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
// Code generated by tui generate. DO NOT EDIT.
// Source: events.gsx

package main

import (
	"fmt"

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

type explorer struct {
	lastKey  *tui.State[string]
	keyCount *tui.State[int]
}

func Explorer() *explorer {
	return &explorer{
		lastKey:  tui.NewState("(none)"),
		keyCount: tui.NewState(0),
	}
}

func (e *explorer) record(name string) {
	e.keyCount.Set(e.keyCount.Get() + 1)
	e.lastKey.Set(name)
}

func (e *explorer) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.OnStop(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.AnyRune, func(ke tui.KeyEvent) {
			e.record(fmt.Sprintf("'%c' (rune)", ke.Rune))
		}),
		// With Kitty keyboard protocol, Ctrl+H/I/M arrive as KeyRune
		// events with ModCtrl and are matched by On with Rune('x').Ctrl().
		// Without Kitty, they are indistinguishable from Backspace/Tab/Enter
		// and match the On handlers below instead.
		tui.On(tui.Rune('h').Ctrl(), func(ke tui.KeyEvent) { e.record("Ctrl+'h' (rune)") }),
		tui.On(tui.Rune('i').Ctrl(), func(ke tui.KeyEvent) { e.record("Ctrl+'i' (rune)") }),
		tui.On(tui.Rune('m').Ctrl(), func(ke tui.KeyEvent) { e.record("Ctrl+'m' (rune)") }),
		tui.On(tui.KeyEnter, func(ke tui.KeyEvent) { e.record("Enter") }),
		tui.On(tui.KeyTab, func(ke tui.KeyEvent) { e.record("Tab") }),
		tui.On(tui.KeyBackspace, func(ke tui.KeyEvent) { e.record("Backspace") }),
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.KeyUp, func(ke tui.KeyEvent) { e.record("Up") }),
		tui.On(tui.KeyDown, func(ke tui.KeyEvent) { e.record("Down") }),
		tui.On(tui.KeyLeft, func(ke tui.KeyEvent) { e.record("Left") }),
		tui.On(tui.KeyRight, func(ke tui.KeyEvent) { e.record("Right") }),
		tui.On(tui.Rune('a').Ctrl(), func(ke tui.KeyEvent) { e.record("Ctrl+A") }),
		tui.On(tui.Rune('s').Ctrl(), func(ke tui.KeyEvent) { e.record("Ctrl+S") }),
	}
}

func (e *explorer) Render(app *tui.App) *tui.Element {
	__tui_0 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
		tui.WithGap(1),
		tui.WithPadding(2),
		tui.WithBorder(tui.BorderRounded),
		tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
	)
	__tui_1 := tui.New(
		tui.WithText("Keyboard Explorer"),
		tui.WithTextGradient(tui.NewGradient(tui.Cyan, tui.Magenta).WithDirection(tui.GradientHorizontal)),
		tui.WithTextStyle(tui.NewStyle().Bold()),
	)
	__tui_0.AddChild(__tui_1)
	__tui_2 := tui.New(
		tui.WithHR(),
		tui.WithBorder(tui.BorderSingle),
	)
	__tui_0.AddChild(__tui_2)
	__tui_3 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row),
		tui.WithGap(2),
	)
	__tui_4 := tui.New(
		tui.WithText("Last Key:"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_3.AddChild(__tui_4)
	__tui_5 := tui.New(
		tui.WithText(e.lastKey.Get()),
		tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan).Bold()),
	)
	__tui_3.AddChild(__tui_5)
	__tui_0.AddChild(__tui_3)
	__tui_6 := tui.New(
		tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Row),
		tui.WithGap(2),
	)
	__tui_7 := tui.New(
		tui.WithText("Key Count:"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_6.AddChild(__tui_7)
	__tui_8 := tui.New(
		tui.WithText(fmt.Sprintf("%d", e.keyCount.Get())),
		tui.WithTextStyle(tui.NewStyle().Foreground(tui.Cyan).Bold()),
	)
	__tui_6.AddChild(__tui_8)
	__tui_0.AddChild(__tui_6)
	__tui_9 := tui.New(
		tui.WithWidth(0),
		tui.WithHeight(1),
	)
	__tui_0.AddChild(__tui_9)
	__tui_10 := tui.New(
		tui.WithText("Press any key to see it displayed above"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_0.AddChild(__tui_10)
	__tui_11 := tui.New(
		tui.WithText("Press q or Esc to quit"),
		tui.WithTextStyle(tui.NewStyle().Dim()),
	)
	__tui_0.AddChild(__tui_11)

	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 (e *explorer) bindAppFields(app *tui.App) {
	if e.lastKey != nil {
		e.lastKey.BindApp(app)
	}
	if e.keyCount != nil {
		e.keyCount.BindApp(app)
	}
}

func (e *explorer) BindApp(app *tui.App) {
	e.bindAppFields(app)
}

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

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