cleaning up

mine
derailed 2019-12-30 23:16:02 -07:00
parent ca600cf322
commit ee01ae7242
4 changed files with 69 additions and 62 deletions

View File

@ -2,8 +2,10 @@ package ui
import ( import (
"github.com/derailed/k9s/internal/client" "github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/tview" "github.com/derailed/tview"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
) )
// App represents an application. // App represents an application.
@ -43,9 +45,45 @@ func (a *App) Init() {
a.bindKeys() a.bindKeys()
a.SetInputCapture(a.keyboard) a.SetInputCapture(a.keyboard)
a.cmdBuff.AddListener(a.Cmd()) a.cmdBuff.AddListener(a.Cmd())
a.Styles.AddListener(a)
a.CmdBuff().AddListener(a)
a.SetRoot(a.Main, true) a.SetRoot(a.Main, true)
} }
// BufferChanged indicates the buffer was changed.
func (a *App) BufferChanged(s string) {}
// BufferActive indicates the buff activity changed.
func (a *App) BufferActive(state bool, _ BufferKind) {
flex, ok := a.Main.GetPrimitive("main").(*tview.Flex)
if !ok {
return
}
if state && flex.ItemAt(1) != a.Cmd() {
flex.AddItemAtIndex(1, a.Cmd(), 3, 1, false)
} else if !state && flex.ItemAt(1) == a.Cmd() {
flex.RemoveItemAtIndex(1)
}
a.Draw()
}
// 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. // ReloadStyles reloads skin file.
func (a *App) ReloadStyles(cluster string) { func (a *App) ReloadStyles(cluster string) {
a.RefreshStyles(cluster) a.RefreshStyles(cluster)
@ -65,6 +103,7 @@ func (a *App) bindKeys() {
tcell.KeyBackspace2: NewKeyAction("Erase", a.eraseCmd, false), tcell.KeyBackspace2: NewKeyAction("Erase", a.eraseCmd, false),
tcell.KeyBackspace: NewKeyAction("Erase", a.eraseCmd, false), tcell.KeyBackspace: NewKeyAction("Erase", a.eraseCmd, false),
tcell.KeyDelete: NewKeyAction("Erase", a.eraseCmd, false), tcell.KeyDelete: NewKeyAction("Erase", a.eraseCmd, false),
tcell.KeyCtrlU: NewSharedKeyAction("Clear Filter", a.clearCmd, false),
} }
} }
@ -146,6 +185,15 @@ func (a *App) keyboard(evt *tcell.EventKey) *tcell.EventKey {
return evt return evt
} }
func (a *App) clearCmd(evt *tcell.EventKey) *tcell.EventKey {
if !a.CmdBuff().IsActive() {
return evt
}
a.CmdBuff().Clear()
return nil
}
func (a *App) activateCmd(evt *tcell.EventKey) *tcell.EventKey { func (a *App) activateCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.InCmdMode() { if a.InCmdMode() {
return evt return evt
@ -206,6 +254,9 @@ func (a *App) Menu() *Menu {
return a.views["menu"].(*Menu) return a.views["menu"].(*Menu)
} }
// ----------------------------------------------------------------------------
// Helpers...
// AsKey converts rune to keyboard key., // AsKey converts rune to keyboard key.,
func asKey(evt *tcell.EventKey) tcell.Key { func asKey(evt *tcell.EventKey) tcell.Key {
key := tcell.Key(evt.Rune()) key := tcell.Key(evt.Rune())

View File

@ -52,7 +52,7 @@ func TestAppGetActions(t *testing.T) {
a.AddActions(ui.KeyActions{ui.KeyZ: ui.KeyAction{Description: "zorg"}}) a.AddActions(ui.KeyActions{ui.KeyZ: ui.KeyAction{Description: "zorg"}})
assert.Equal(t, 8, len(a.GetActions())) assert.Equal(t, 9, len(a.GetActions()))
} }
func TestAppViews(t *testing.T) { func TestAppViews(t *testing.T) {

View File

@ -51,22 +51,10 @@ func NewApp(cfg *config.Config) *App {
return &a return &a
} }
// ActiveView returns the currently active view.
func (a *App) ActiveView() model.Component {
return a.Content.GetPrimitive("main").(model.Component)
}
// PrevCmd pops the command stack.
func (a *App) PrevCmd(evt *tcell.EventKey) *tcell.EventKey {
if !a.Content.IsLast() {
a.Content.Pop()
}
return nil
}
// Init initializes the application. // Init initializes the application.
func (a *App) Init(version string, rate int) error { func (a *App) Init(version string, rate int) error {
a.version = version
ctx := context.WithValue(context.Background(), internal.KeyApp, a) ctx := context.WithValue(context.Background(), internal.KeyApp, a)
if err := a.Content.Init(ctx); err != nil { if err := a.Content.Init(ctx); err != nil {
return err return err
@ -74,9 +62,7 @@ func (a *App) Init(version string, rate int) error {
a.Content.Stack.AddListener(a.Crumbs()) a.Content.Stack.AddListener(a.Crumbs())
a.Content.Stack.AddListener(a.Menu()) a.Content.Stack.AddListener(a.Menu())
a.version = version
a.App.Init() a.App.Init()
a.CmdBuff().AddListener(a)
a.bindKeys() a.bindKeys()
if a.Conn() == nil { if a.Conn() == nil {
return errors.New("No client connection detected") return errors.New("No client connection detected")
@ -94,7 +80,7 @@ func (a *App) Init(version string, rate int) error {
return err return err
} }
a.clusterInfo().init(version) a.clusterInfo().Init(version)
if a.Config.K9s.GetHeadless() { if a.Config.K9s.GetHeadless() {
a.refreshIndicator() a.refreshIndicator()
} }
@ -109,52 +95,21 @@ func (a *App) Init(version string, rate int) error {
a.Main.AddPage("splash", ui.NewSplash(a.Styles, version), true, true) a.Main.AddPage("splash", ui.NewSplash(a.Styles, version), true, true)
a.toggleHeader(!a.Config.K9s.GetHeadless()) a.toggleHeader(!a.Config.K9s.GetHeadless())
a.Styles.AddListener(a)
return nil return nil
} }
// 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")
}
}
func (a *App) bindKeys() { func (a *App) bindKeys() {
a.AddActions(ui.KeyActions{ a.AddActions(ui.KeyActions{
ui.KeyH: ui.NewSharedKeyAction("ToggleHeader", a.toggleHeaderCmd, false), ui.KeyH: ui.NewSharedKeyAction("ToggleHeader", a.toggleHeaderCmd, false),
ui.KeyHelp: ui.NewSharedKeyAction("Help", a.helpCmd, false), ui.KeyHelp: ui.NewSharedKeyAction("Help", a.helpCmd, false),
tcell.KeyCtrlA: ui.NewSharedKeyAction("Aliases", a.aliasCmd, false), tcell.KeyCtrlA: ui.NewSharedKeyAction("Aliases", a.aliasCmd, false),
tcell.KeyEnter: ui.NewKeyAction("Goto", a.gotoCmd, false), tcell.KeyEnter: ui.NewKeyAction("Goto", a.gotoCmd, false),
tcell.KeyCtrlU: ui.NewSharedKeyAction("Clear Filter", a.clearCmd, false),
}) })
} }
// BufferChanged indicates the buffer was changed. // ActiveView returns the currently active view.
func (a *App) BufferChanged(s string) {} func (a *App) ActiveView() model.Component {
return a.Content.GetPrimitive("main").(model.Component)
// BufferActive indicates the buff activity changed.
func (a *App) BufferActive(state bool, _ ui.BufferKind) {
flex, ok := a.Main.GetPrimitive("main").(*tview.Flex)
if !ok {
return
}
if state && flex.ItemAt(1) != a.Cmd() {
flex.AddItemAtIndex(1, a.Cmd(), 3, 1, false)
} else if !state && flex.ItemAt(1) == a.Cmd() {
flex.RemoveItemAtIndex(1)
}
a.Draw()
} }
func (a *App) toggleHeader(flag bool) { func (a *App) toggleHeader(flag bool) {
@ -385,6 +340,15 @@ func (a *App) setIndicator(l ui.FlashLevel, msg string) {
a.Draw() a.Draw()
} }
// PrevCmd pops the command stack.
func (a *App) PrevCmd(evt *tcell.EventKey) *tcell.EventKey {
if !a.Content.IsLast() {
a.Content.Pop()
}
return nil
}
func (a *App) toggleHeaderCmd(evt *tcell.EventKey) *tcell.EventKey { func (a *App) toggleHeaderCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.Cmd().InCmdMode() { if a.Cmd().InCmdMode() {
return evt return evt
@ -397,15 +361,6 @@ func (a *App) toggleHeaderCmd(evt *tcell.EventKey) *tcell.EventKey {
return nil return nil
} }
func (a *App) clearCmd(evt *tcell.EventKey) *tcell.EventKey {
if !a.CmdBuff().IsActive() {
return evt
}
a.CmdBuff().Clear()
return nil
}
func (a *App) gotoCmd(evt *tcell.EventKey) *tcell.EventKey { func (a *App) gotoCmd(evt *tcell.EventKey) *tcell.EventKey {
if a.CmdBuff().IsActive() && !a.CmdBuff().Empty() { if a.CmdBuff().IsActive() && !a.CmdBuff().Empty() {
if err := a.gotoResource(a.GetCmd(), true); err != nil { if err := a.gotoResource(a.GetCmd(), true); err != nil {

View File

@ -35,7 +35,8 @@ func NewClusterInfo(app *App, mx *client.MetricsServer) *ClusterInfo {
} }
} }
func (c *ClusterInfo) init(version string) { // Init initializes the view.
func (c *ClusterInfo) Init(version string) {
cluster := model.NewCluster(c.app.Conn(), c.mxs) cluster := model.NewCluster(c.app.Conn(), c.mxs)
c.app.Styles.AddListener(c) c.app.Styles.AddListener(c)