-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
table.go
220 lines (179 loc) · 5.42 KB
/
table.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
package view
import (
"context"
"time"
"github.com/atotto/clipboard"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
)
// Table represents a table viewer.
type Table struct {
*ui.Table
app *App
enterFn EnterFunc
envFn EnvFunc
bindKeysFn BindKeysFunc
}
// NewTable returns a new viewer.
func NewTable(gvr client.GVR) *Table {
t := Table{
Table: ui.NewTable(gvr),
}
t.envFn = t.defaultEnv
return &t
}
// Init initializes the component
func (t *Table) Init(ctx context.Context) (err error) {
if t.app, err = extractApp(ctx); err != nil {
return err
}
if t.app.Conn() != nil {
ctx = context.WithValue(ctx, internal.KeyHasMetrics, t.app.Conn().HasMetrics())
}
ctx = context.WithValue(ctx, internal.KeyStyles, t.app.Styles)
ctx = context.WithValue(ctx, internal.KeyViewConfig, t.app.CustomView)
t.Table.Init(ctx)
t.SetInputCapture(t.keyboard)
t.bindKeys()
t.GetModel().SetRefreshRate(time.Duration(t.app.Config.K9s.GetRefreshRate()) * time.Second)
t.CmdBuff().AddListener(t)
return nil
}
// SendKey sends an keyboard event (testing only!).
func (t *Table) SendKey(evt *tcell.EventKey) {
t.app.Prompt().SendKey(evt)
}
func (t *Table) keyboard(evt *tcell.EventKey) *tcell.EventKey {
key := evt.Key()
if key == tcell.KeyUp || key == tcell.KeyDown {
return evt
}
if a, ok := t.Actions()[ui.AsKey(evt)]; ok && !t.app.Content.IsTopDialog() {
return a.Action(evt)
}
return evt
}
// Name returns the table name.
func (t *Table) Name() string { return t.GVR().R() }
// SetBindKeysFn adds additional key bindings.
func (t *Table) SetBindKeysFn(f BindKeysFunc) { t.bindKeysFn = f }
// SetEnvFn sets a function to pull viewer env vars for plugins.
func (t *Table) SetEnvFn(f EnvFunc) { t.envFn = f }
// EnvFn returns an plugin env function if available.
func (t *Table) EnvFn() EnvFunc {
return t.envFn
}
func (t *Table) defaultEnv() Env {
path := t.GetSelectedItem()
row, ok := t.GetSelectedRow(path)
if !ok {
log.Error().Msgf("unable to locate selected row for %q", path)
}
env := defaultEnv(t.app.Conn().Config(), path, t.GetModel().Peek().Header, row)
env["FILTER"] = t.CmdBuff().GetText()
if env["FILTER"] == "" {
env["NAMESPACE"], env["FILTER"] = client.Namespaced(path)
}
return env
}
// App returns the current app handle.
func (t *Table) App() *App {
return t.app
}
// Start runs the component.
func (t *Table) Start() {
t.Stop()
t.CmdBuff().AddListener(t)
t.Styles().AddListener(t.Table)
}
// Stop terminates the component.
func (t *Table) Stop() {
t.CmdBuff().RemoveListener(t)
t.Styles().RemoveListener(t.Table)
}
// SetEnterFn specifies the default enter behavior.
func (t *Table) SetEnterFn(f EnterFunc) {
t.enterFn = f
}
// SetExtraActionsFn specifies custom keyboard behavior.
func (t *Table) SetExtraActionsFn(BoostActionsFunc) {}
// BufferChanged indicates the buffer was changed.
func (t *Table) BufferChanged(s string) {
t.Filter(s)
}
// BufferActive indicates the buff activity changed.
func (t *Table) BufferActive(state bool, k model.BufferKind) {
t.app.BufferActive(state, k)
if !state {
t.app.SetFocus(t)
}
}
func (t *Table) saveCmd(evt *tcell.EventKey) *tcell.EventKey {
if path, err := saveTable(t.app.Config.K9s.CurrentCluster, t.GVR().R(), t.Path, t.GetFilteredData()); err != nil {
t.app.Flash().Err(err)
} else {
t.app.Flash().Infof("File %s saved successfully!", path)
}
return nil
}
func (t *Table) bindKeys() {
t.Actions().Add(ui.KeyActions{
ui.KeyHelp: ui.NewKeyAction("Help", t.App().helpCmd, true),
ui.KeySpace: ui.NewSharedKeyAction("Mark", t.markCmd, false),
tcell.KeyCtrlSpace: ui.NewSharedKeyAction("Mark Range", t.markSpanCmd, false),
tcell.KeyCtrlBackslash: ui.NewSharedKeyAction("Marks Clear", t.clearMarksCmd, false),
tcell.KeyCtrlS: ui.NewSharedKeyAction("Save", t.saveCmd, false),
ui.KeySlash: ui.NewSharedKeyAction("Filter Mode", t.activateCmd, false),
tcell.KeyCtrlZ: ui.NewKeyAction("Toggle Faults", t.toggleFaultCmd, false),
tcell.KeyCtrlW: ui.NewKeyAction("Toggle Wide", t.toggleWideCmd, false),
ui.KeyShiftN: ui.NewKeyAction("Sort Name", t.SortColCmd(nameCol, true), false),
ui.KeyShiftA: ui.NewKeyAction("Sort Age", t.SortColCmd(ageCol, true), false),
})
}
func (t *Table) toggleFaultCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ToggleToast()
return nil
}
func (t *Table) toggleWideCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ToggleWide()
return nil
}
func (t *Table) cpCmd(evt *tcell.EventKey) *tcell.EventKey {
path := t.GetSelectedItem()
if path == "" {
return evt
}
_, n := client.Namespaced(path)
log.Debug().Msgf("Copied selection to clipboard %q", n)
t.app.Flash().Info("Current selection copied to clipboard...")
if err := clipboard.WriteAll(n); err != nil {
t.app.Flash().Err(err)
}
return nil
}
func (t *Table) markCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ToggleMark()
t.Refresh()
return nil
}
func (t *Table) markSpanCmd(evt *tcell.EventKey) *tcell.EventKey {
t.SpanMark()
t.Refresh()
return nil
}
func (t *Table) clearMarksCmd(evt *tcell.EventKey) *tcell.EventKey {
t.ClearMarks()
t.Refresh()
return nil
}
func (t *Table) activateCmd(evt *tcell.EventKey) *tcell.EventKey {
if t.app.InCmdMode() {
return evt
}
t.App().ResetPrompt(t.CmdBuff())
return nil
}