/
alias.go
75 lines (61 loc) 路 2.03 KB
/
alias.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package view
import (
"context"
"strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
)
const aliasTitle = "Aliases"
// Alias represents a command alias view.
type Alias struct {
ResourceViewer
}
// NewAlias returns a new alias view.
func NewAlias(gvr client.GVR) ResourceViewer {
a := Alias{
ResourceViewer: NewBrowser(gvr),
}
a.GetTable().SetBorderFocusColor(tcell.ColorAliceBlue)
a.GetTable().SetSelectedStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorAliceBlue).Attributes(tcell.AttrNone))
a.AddBindKeysFn(a.bindKeys)
a.SetContextFn(a.aliasContext)
return &a
}
// Init initializes the view.
func (a *Alias) Init(ctx context.Context) error {
if err := a.ResourceViewer.Init(ctx); err != nil {
return err
}
a.GetTable().GetModel().SetNamespace(client.NotNamespaced)
return nil
}
func (a *Alias) aliasContext(ctx context.Context) context.Context {
return context.WithValue(ctx, internal.KeyAliases, a.App().command.alias)
}
func (a *Alias) bindKeys(aa *ui.KeyActions) {
aa.Delete(ui.KeyShiftA, ui.KeyShiftN, tcell.KeyCtrlS, tcell.KeyCtrlSpace, ui.KeySpace)
aa.Delete(tcell.KeyCtrlW, tcell.KeyCtrlL)
aa.Bulk(ui.KeyMap{
tcell.KeyEnter: ui.NewKeyAction("Goto", a.gotoCmd, true),
ui.KeyShiftR: ui.NewKeyAction("Sort Resource", a.GetTable().SortColCmd("RESOURCE", true), false),
ui.KeyShiftC: ui.NewKeyAction("Sort Command", a.GetTable().SortColCmd("COMMAND", true), false),
ui.KeyShiftA: ui.NewKeyAction("Sort ApiGroup", a.GetTable().SortColCmd("API-GROUP", true), false),
})
}
func (a *Alias) gotoCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.GetTable().CmdBuff().IsActive() {
return a.GetTable().activateCmd(evt)
}
r, _ := a.GetTable().GetSelection()
if r != 0 {
s := ui.TrimCell(a.GetTable().SelectTable, r, 1)
tokens := strings.Split(s, ",")
a.App().gotoResource(tokens[0], "", true)
return nil
}
return evt
}