gitstack

grindlemire/go-tui code browser

3.6 KB Go 157 lines 2026-07-10 · 0592ab2 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
// Code generated by tui generate. DO NOT EDIT.
// Source: inline.gsx

package main

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

type myApp struct {
	app          *tui.App
	showSettings *tui.State[bool]
	textarea     *tui.TextArea
}

func InlineApp() *myApp {
	a := &myApp{
		showSettings: tui.NewState(false),
	}
	a.textarea = tui.NewTextArea(
		tui.WithTextAreaWidth(60),
		tui.WithTextAreaBorder(tui.BorderRounded),
		tui.WithTextAreaPlaceholder("Type here..."),
		tui.WithTextAreaOnSubmit(a.send),
	)
	return a
}

func (a *myApp) BindApp(app *tui.App) {
	a.app = app
	a.showSettings.BindApp(app)
	a.textarea.BindApp(app)
}

func (a *myApp) send(text string) {
	if text == "" {
		return
	}
	a.textarea.Clear()
	a.app.PrintAboveln("You: %s", text)
}

func (a *myApp) toggleSettings() {
	if a.showSettings.Get() {
		_ = a.app.ExitAlternateScreen()
		a.showSettings.Set(false)
		return
	}
	a.showSettings.Set(true)
	_ = a.app.EnterAlternateScreen()
}

func (a *myApp) KeyMap() tui.KeyMap {
	if a.showSettings.Get() {
		return tui.KeyMap{
			tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { a.toggleSettings() }),
			tui.On(tui.Rune('c').Ctrl(), func(ke tui.KeyEvent) { ke.App().Stop() }),
		}
	}

	km := a.textarea.KeyMap()
	km = append(km,
		tui.OnStop(tui.Rune('s').Ctrl(), func(ke tui.KeyEvent) { a.toggleSettings() }),
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('c').Ctrl(), func(ke tui.KeyEvent) { ke.App().Stop() }),
	)
	return km
}

func (a *myApp) Watchers() []tui.Watcher {
	return a.textarea.Watchers()
}

func (a *myApp) Render(app *tui.App) *tui.Element {
	var __tui_0 *tui.Element
	if a.showSettings.Get() {
		__tui_1 := tui.New(
			tui.WithDisplay(tui.DisplayFlex), tui.WithDirection(tui.Column),
			tui.WithHeightPercent(100.00),
			tui.WithPadding(1),
			tui.WithBorder(tui.BorderRounded),
			tui.WithBorderStyle(tui.NewStyle().Foreground(tui.Cyan)),
		)
		__tui_2 := tui.New(
			tui.WithText("Settings"),
			tui.WithTextStyle(tui.NewStyle().Bold().Foreground(tui.Cyan)),
		)
		__tui_1.AddChild(__tui_2)
		__tui_3 := tui.New(
			tui.WithText("Press Escape to"),
			tui.WithTextStyle(tui.NewStyle().Dim()),
		)
		__tui_1.AddChild(__tui_3)
		if __tui_0 == nil {
			__tui_0 = __tui_1
		}
	} else {
		__tui_4 := a.textarea.Render(app)
		if __tui_0 == nil {
			__tui_0 = __tui_4
		}
	}

	return __tui_0
}

// updatePropsFields is generated. It copies prop fields from fresh onto
// the receiver. When you override UpdateProps, call this helper instead
// of hand-maintaining the copy list.
func (a *myApp) updatePropsFields(fresh tui.Component) {
	f, ok := fresh.(*myApp)
	if !ok {
		return
	}
	a.app = f.app
	a.textarea = f.textarea
}

func (a *myApp) UpdateProps(fresh tui.Component) {
	a.updatePropsFields(fresh)
}

var _ tui.PropsUpdater = (*myApp)(nil)

// 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 (a *myApp) bindAppFields(app *tui.App) {
	a.app = app
	if a.showSettings != nil {
		a.showSettings.BindApp(app)
	}
	if a.textarea != nil {
		a.textarea.BindApp(app)
	}
}

// unbindAppFields is generated. It detaches topic-based Events
// subscriptions and any component-expression AppUnbinder fields.
// Call this from your UnbindApp if you override it.
func (a *myApp) unbindAppFields() {
	if unbinder, ok := any(a.textarea).(tui.AppUnbinder); ok {
		unbinder.UnbindApp()
	}
}

func (a *myApp) UnbindApp() {
	a.unbindAppFields()
}

var _ tui.AppUnbinder = (*myApp)(nil)

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