From ee01ae7242520d0315b56976c7b134dc1f99665a Mon Sep 17 00:00:00 2001 From: derailed Date: Mon, 30 Dec 2019 23:16:02 -0700 Subject: [PATCH] cleaning up --- internal/ui/app.go | 51 ++++++++++++++++++++++++ internal/ui/app_test.go | 2 +- internal/view/app.go | 75 +++++++---------------------------- internal/view/cluster_info.go | 3 +- 4 files changed, 69 insertions(+), 62 deletions(-) diff --git a/internal/ui/app.go b/internal/ui/app.go index 80b711e0..1ae8cd7d 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -2,8 +2,10 @@ package ui import ( "github.com/derailed/k9s/internal/client" + "github.com/derailed/k9s/internal/config" "github.com/derailed/tview" "github.com/gdamore/tcell" + "github.com/rs/zerolog/log" ) // App represents an application. @@ -43,9 +45,45 @@ func (a *App) Init() { a.bindKeys() a.SetInputCapture(a.keyboard) a.cmdBuff.AddListener(a.Cmd()) + a.Styles.AddListener(a) + a.CmdBuff().AddListener(a) + 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. func (a *App) ReloadStyles(cluster string) { a.RefreshStyles(cluster) @@ -65,6 +103,7 @@ func (a *App) bindKeys() { tcell.KeyBackspace2: NewKeyAction("Erase", a.eraseCmd, false), tcell.KeyBackspace: 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 } +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 { if a.InCmdMode() { return evt @@ -206,6 +254,9 @@ func (a *App) Menu() *Menu { return a.views["menu"].(*Menu) } +// ---------------------------------------------------------------------------- +// Helpers... + // AsKey converts rune to keyboard key., func asKey(evt *tcell.EventKey) tcell.Key { key := tcell.Key(evt.Rune()) diff --git a/internal/ui/app_test.go b/internal/ui/app_test.go index db0047f8..4e189c59 100644 --- a/internal/ui/app_test.go +++ b/internal/ui/app_test.go @@ -52,7 +52,7 @@ func TestAppGetActions(t *testing.T) { 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) { diff --git a/internal/view/app.go b/internal/view/app.go index ef5f8540..23a4aafd 100644 --- a/internal/view/app.go +++ b/internal/view/app.go @@ -51,22 +51,10 @@ func NewApp(cfg *config.Config) *App { 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. func (a *App) Init(version string, rate int) error { + a.version = version + ctx := context.WithValue(context.Background(), internal.KeyApp, a) if err := a.Content.Init(ctx); err != nil { 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.Menu()) - a.version = version a.App.Init() - a.CmdBuff().AddListener(a) a.bindKeys() if a.Conn() == nil { return errors.New("No client connection detected") @@ -94,7 +80,7 @@ func (a *App) Init(version string, rate int) error { return err } - a.clusterInfo().init(version) + a.clusterInfo().Init(version) if a.Config.K9s.GetHeadless() { 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.toggleHeader(!a.Config.K9s.GetHeadless()) - a.Styles.AddListener(a) - 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() { a.AddActions(ui.KeyActions{ ui.KeyH: ui.NewSharedKeyAction("ToggleHeader", a.toggleHeaderCmd, false), ui.KeyHelp: ui.NewSharedKeyAction("Help", a.helpCmd, false), tcell.KeyCtrlA: ui.NewSharedKeyAction("Aliases", a.aliasCmd, false), tcell.KeyEnter: ui.NewKeyAction("Goto", a.gotoCmd, false), - tcell.KeyCtrlU: ui.NewSharedKeyAction("Clear Filter", a.clearCmd, false), }) } -// BufferChanged indicates the buffer was changed. -func (a *App) BufferChanged(s string) {} - -// 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() +// ActiveView returns the currently active view. +func (a *App) ActiveView() model.Component { + return a.Content.GetPrimitive("main").(model.Component) } func (a *App) toggleHeader(flag bool) { @@ -385,6 +340,15 @@ func (a *App) setIndicator(l ui.FlashLevel, msg string) { 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 { if a.Cmd().InCmdMode() { return evt @@ -397,15 +361,6 @@ func (a *App) toggleHeaderCmd(evt *tcell.EventKey) *tcell.EventKey { 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 { if a.CmdBuff().IsActive() && !a.CmdBuff().Empty() { if err := a.gotoResource(a.GetCmd(), true); err != nil { diff --git a/internal/view/cluster_info.go b/internal/view/cluster_info.go index 65e11784..dbf116c8 100644 --- a/internal/view/cluster_info.go +++ b/internal/view/cluster_info.go @@ -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) c.app.Styles.AddListener(c)