diff --git a/internal/resource/list.go b/internal/resource/list.go index ba645cf6..0fa03995 100644 --- a/internal/resource/list.go +++ b/internal/resource/list.go @@ -66,7 +66,7 @@ type ( Rows RowEvents NumCols map[string]bool Namespace string - Marks []string + Marks map[string]bool } // List protocol to display and update a collection of resources @@ -129,18 +129,13 @@ type ( verbs int resource Resource cache RowEvents - marks []string + marks map[string]bool } ) // IsMarked checks if key is marked. func (t *TableData) IsMarked(sk string) bool { - for _, mark := range t.Marks { - if mark == sk { - return true - } - } - return false + return t.Marks[sk] } func newRowEvent(a watch.EventType, f, d Row) *RowEvent { @@ -155,6 +150,7 @@ func NewList(ns, name string, res Resource, verbs int) *list { verbs: verbs, resource: res, cache: RowEvents{}, + marks: make(map[string]bool), } } @@ -363,21 +359,11 @@ func (l *list) ensureDeletes(kk []string) { } func (l *list) removeMark(sk string) { - for index, mark := range l.marks { - if mark == sk { - l.marks = append(l.marks[:index], l.marks[index+1:]...) - } - } + delete(l.marks, sk) } func (l *list) ToggleMark(sk string) { - for index, mark := range l.marks { - if mark == sk { - l.marks = append(l.marks[:index], l.marks[index+1:]...) - return - } - } - l.marks = append(l.marks, sk) + l.marks[sk] = !l.marks[sk] } // Helpers... diff --git a/internal/ui/table.go b/internal/ui/table.go index c4a349c9..5815acb0 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -172,8 +172,12 @@ func (v *Table) GetSelectedItem() string { // GetSelectedItems return currently marked or selected items names. func (v *Table) GetSelectedItems() []string { if len(v.data.Marks) > 0 { - items := make([]string, len(v.data.Marks)) - copy(items, v.data.Marks) + var items []string + for item, marked := range v.data.Marks { + if marked { + items = append(items, item) + } + } return items } return []string{v.GetSelectedItem()}