-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
app.go
288 lines (239 loc) · 6.33 KB
/
app.go
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package ui
import (
"os"
"sync"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
"github.com/rs/zerolog/log"
)
// App represents an application.
type App struct {
*tview.Application
Configurator
Main *Pages
flash *model.Flash
actions KeyActions
views map[string]tview.Primitive
cmdBuff *model.FishBuff
running bool
mx sync.RWMutex
}
// NewApp returns a new app.
func NewApp(cfg *config.Config, context string) *App {
a := App{
Application: tview.NewApplication(),
actions: make(KeyActions),
Configurator: Configurator{Config: cfg},
Main: NewPages(),
flash: model.NewFlash(model.DefaultFlashDelay),
cmdBuff: model.NewFishBuff(':', model.CommandBuffer),
}
a.ReloadStyles(context)
a.views = map[string]tview.Primitive{
"menu": NewMenu(a.Styles),
"logo": NewLogo(a.Styles),
"prompt": NewPrompt(a.Config.K9s.NoIcons, a.Styles),
"crumbs": NewCrumbs(a.Styles),
}
return &a
}
// Init initializes the application.
func (a *App) Init() {
a.bindKeys()
a.Prompt().SetModel(a.cmdBuff)
a.cmdBuff.AddListener(a)
a.Styles.AddListener(a)
a.SetRoot(a.Main, true).EnableMouse(a.Config.K9s.EnableMouse)
}
// QueueUpdate queues up a ui action.
func (a *App) QueueUpdate(f func()) {
go func() {
a.Application.QueueUpdate(f)
}()
}
// QueueUpdateDraw queues up a ui action and redraw the ui.
func (a *App) QueueUpdateDraw(f func()) {
go func() {
a.Application.QueueUpdateDraw(f)
}()
}
// IsRunning checks if app is actually running.
func (a *App) IsRunning() bool {
a.mx.RLock()
defer a.mx.RUnlock()
return a.running
}
// SetRunning sets the app run state.
func (a *App) SetRunning(f bool) {
a.mx.Lock()
defer a.mx.Unlock()
a.running = f
}
// BufferCompleted indicates input was accepted.
func (a *App) BufferCompleted(s string) {}
// BufferChanged indicates the buffer was changed.
func (a *App) BufferChanged(s string) {}
// BufferActive indicates the buff activity changed.
func (a *App) BufferActive(state bool, kind model.BufferKind) {
flex, ok := a.Main.GetPrimitive("main").(*tview.Flex)
if !ok {
return
}
if state && flex.ItemAt(1) != a.Prompt() {
flex.AddItemAtIndex(1, a.Prompt(), 3, 1, false)
} else if !state && flex.ItemAt(1) == a.Prompt() {
flex.RemoveItemAtIndex(1)
a.SetFocus(flex)
}
}
// SuggestionChanged notifies of update to command suggestions.
func (a *App) SuggestionChanged(ss []string) {}
// StylesChanged notifies the skin changed.
func (a *App) StylesChanged(s *config.Styles) {
a.Main.SetBackgroundColor(s.BgColor())
if f, ok := a.Main.GetPrimitive("main").(*tview.Flex); ok {
f.SetBackgroundColor(s.BgColor())
if h, ok := f.ItemAt(0).(*tview.Flex); ok {
h.SetBackgroundColor(s.BgColor())
} else {
log.Error().Msgf("Header not found")
}
} else {
log.Error().Msgf("Main not found")
}
}
// ReloadStyles reloads skin file.
func (a *App) ReloadStyles(context string) {
a.RefreshStyles(context)
}
// Conn returns an api server connection.
func (a *App) Conn() client.Connection {
return a.Config.GetConnection()
}
func (a *App) bindKeys() {
a.actions = KeyActions{
KeyColon: NewKeyAction("Cmd", a.activateCmd, false),
tcell.KeyCtrlR: NewKeyAction("Redraw", a.redrawCmd, false),
tcell.KeyCtrlC: NewKeyAction("Quit", a.quitCmd, false),
tcell.KeyCtrlU: NewSharedKeyAction("Clear Filter", a.clearCmd, false),
tcell.KeyCtrlQ: NewSharedKeyAction("Clear Filter", a.clearCmd, false),
}
}
// BailOut exists the application.
func (a *App) BailOut() {
a.Stop()
os.Exit(0)
}
// ResetPrompt reset the prompt model and marks buffer as active.
func (a *App) ResetPrompt(m PromptModel) {
a.Prompt().SetModel(m)
a.SetFocus(a.Prompt())
m.SetActive(true)
}
// ResetCmd clear out user command.
func (a *App) ResetCmd() {
a.cmdBuff.Reset()
}
// ActivateCmd toggle command mode.
func (a *App) ActivateCmd(b bool) {
a.cmdBuff.SetActive(b)
}
// GetCmd retrieves user command.
func (a *App) GetCmd() string {
return a.cmdBuff.GetText()
}
// CmdBuff returns the app cmd model.
func (a *App) CmdBuff() *model.FishBuff {
return a.cmdBuff
}
// HasCmd check if cmd buffer is active and has a command.
func (a *App) HasCmd() bool {
return a.cmdBuff.IsActive() && !a.cmdBuff.Empty()
}
func (a *App) quitCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.InCmdMode() {
return evt
}
a.BailOut()
return nil
}
// InCmdMode check if command mode is active.
func (a *App) InCmdMode() bool {
return a.Prompt().InCmdMode()
}
// HasAction checks if key matches a registered binding.
func (a *App) HasAction(key tcell.Key) (KeyAction, bool) {
act, ok := a.actions[key]
return act, ok
}
// GetActions returns a collection of actiona.
func (a *App) GetActions() KeyActions {
return a.actions
}
// AddActions returns the application actiona.
func (a *App) AddActions(aa KeyActions) {
for k, v := range aa {
a.actions[k] = v
}
}
// Views return the application root viewa.
func (a *App) Views() map[string]tview.Primitive {
return a.views
}
func (a *App) clearCmd(evt *tcell.EventKey) *tcell.EventKey {
if !a.cmdBuff.IsActive() {
return evt
}
a.cmdBuff.ClearText(true)
return nil
}
func (a *App) activateCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.InCmdMode() {
return evt
}
a.ResetPrompt(a.cmdBuff)
a.cmdBuff.ClearText(true)
return nil
}
// RedrawCmd forces a redraw.
func (a *App) redrawCmd(evt *tcell.EventKey) *tcell.EventKey {
a.QueueUpdateDraw(func() {})
return evt
}
// View Accessors...
// Crumbs return app crumba.
func (a *App) Crumbs() *Crumbs {
return a.views["crumbs"].(*Crumbs)
}
// Logo return the app logo.
func (a *App) Logo() *Logo {
return a.views["logo"].(*Logo)
}
// Prompt returns command prompt.
func (a *App) Prompt() *Prompt {
return a.views["prompt"].(*Prompt)
}
// Menu returns app menu.
func (a *App) Menu() *Menu {
return a.views["menu"].(*Menu)
}
// Flash returns a flash model.
func (a *App) Flash() *model.Flash {
return a.flash
}
// ----------------------------------------------------------------------------
// Helpers...
// AsKey converts rune to keyboard key.,
func AsKey(evt *tcell.EventKey) tcell.Key {
if evt.Key() != tcell.KeyRune {
return evt.Key()
}
key := tcell.Key(evt.Rune())
if evt.Modifiers() == tcell.ModAlt {
key = tcell.Key(int16(evt.Rune()) * int16(evt.Modifiers()))
}
return key
}