Skip to content

Commit

Permalink
Add toggle button for favorite namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
placintaalexandru committed Aug 26, 2024
1 parent 64e46b6 commit cb7796b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
16 changes: 7 additions & 9 deletions internal/config/data/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (n *Namespace) Validate(c client.Connection) {
for _, ns := range n.Favorites {
if !c.IsValidNamespace(ns) {
log.Debug().Msgf("[Namespace] Invalid favorite found '%s' - %t", ns, n.isAllNamespaces())
n.rmFavNS(ns)
n.RmFavNS(ns)
}
}
}
Expand All @@ -85,17 +85,18 @@ func (n *Namespace) SetActive(ns string, ks KubeSettings) error {
n.Active = ns

if ns != "" && !n.LockFavorites {
n.addFavNS(ns)
n.AddFavNS(ns)
}

return nil
}

func (n *Namespace) isAllNamespaces() bool {
return n.Active == client.NamespaceAll || n.Active == ""
return client.IsAllNamespaces(n.Active)
}

func (n *Namespace) addFavNS(ns string) {
// AddFavNS adds a namespace to the list of favorites namespaces
func (n *Namespace) AddFavNS(ns string) {
if InList(n.Favorites, ns) {
return
}
Expand All @@ -110,11 +111,8 @@ func (n *Namespace) addFavNS(ns string) {
n.Favorites = nfv
}

func (n *Namespace) rmFavNS(ns string) {
if n.LockFavorites {
return
}

// RmFavNS removes a namespace from the list of favorites namespaces
func (n *Namespace) RmFavNS(ns string) {
victim := -1
for i, f := range n.Favorites {
if f == ns {
Expand Down
30 changes: 28 additions & 2 deletions internal/view/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ package view

import (
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/config/data"
"github.com/derailed/k9s/internal/model1"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
)

const (
favNSIndicator = "+"
defaultNSIndicator = "(*)"
favNSIndicator = " ❤️ "
defaultNSIndicator = "(*)"
deleteNumericBindingsKey = "delete-ns-numeric-bindings"
)

// Namespace represents a namespace viewer.
Expand All @@ -36,6 +38,8 @@ func (n *Namespace) bindKeys(aa *ui.KeyActions) {
aa.Bulk(ui.KeyMap{
ui.KeyU: ui.NewKeyAction("Use", n.useNsCmd, true),
ui.KeyShiftS: ui.NewKeyAction("Sort Status", n.GetTable().SortColCmd(statusCol, true), false),
// ui.KeyShiftD: ui.NewKeyAction("Delete Binding", n.deleteNamespaceKeyBindings, true),
ui.KeyF: ui.NewKeyAction("Toggle Favorite", n.toggleFavorite, true),
})
}

Expand Down Expand Up @@ -103,3 +107,25 @@ func (n *Namespace) decorate(td *model1.TableData) {
return true
})
}

func (n *Namespace) toggleFavorite(evt *tcell.EventKey) *tcell.EventKey {
_, ns := client.Namespaced(n.GetTable().GetSelectedItem())

if ns == "" {
return nil
}

ctx, err := n.App().Config.K9s.ActiveContext()

if err != nil {
return evt
}

if data.InList(n.App().Config.FavNamespaces(), ns) {
ctx.Namespace.RmFavNS(ns)
} else {
ctx.Namespace.AddFavNS(ns)
}

return nil
}
2 changes: 1 addition & 1 deletion internal/view/ns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func TestNSCleanser(t *testing.T) {

assert.Nil(t, ns.Init(makeCtx()))
assert.Equal(t, "Namespaces", ns.Name())
assert.Equal(t, 7, len(ns.Hints()))
assert.Equal(t, 8, len(ns.Hints()))
}

0 comments on commit cb7796b

Please sign in to comment.