Convert marks from slice to map

mine
Bruno Meneguello 2019-11-07 18:14:11 -03:00
parent 42e5793438
commit 52aa4a122a
No known key found for this signature in database
GPG Key ID: DF8B8EC5053915D5
2 changed files with 12 additions and 22 deletions

View File

@ -66,7 +66,7 @@ type (
Rows RowEvents Rows RowEvents
NumCols map[string]bool NumCols map[string]bool
Namespace string Namespace string
Marks []string Marks map[string]bool
} }
// List protocol to display and update a collection of resources // List protocol to display and update a collection of resources
@ -129,18 +129,13 @@ type (
verbs int verbs int
resource Resource resource Resource
cache RowEvents cache RowEvents
marks []string marks map[string]bool
} }
) )
// IsMarked checks if key is marked. // IsMarked checks if key is marked.
func (t *TableData) IsMarked(sk string) bool { func (t *TableData) IsMarked(sk string) bool {
for _, mark := range t.Marks { return t.Marks[sk]
if mark == sk {
return true
}
}
return false
} }
func newRowEvent(a watch.EventType, f, d Row) *RowEvent { 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, verbs: verbs,
resource: res, resource: res,
cache: RowEvents{}, cache: RowEvents{},
marks: make(map[string]bool),
} }
} }
@ -363,21 +359,11 @@ func (l *list) ensureDeletes(kk []string) {
} }
func (l *list) removeMark(sk string) { func (l *list) removeMark(sk string) {
for index, mark := range l.marks { delete(l.marks, sk)
if mark == sk {
l.marks = append(l.marks[:index], l.marks[index+1:]...)
}
}
} }
func (l *list) ToggleMark(sk string) { func (l *list) ToggleMark(sk string) {
for index, mark := range l.marks { l.marks[sk] = !l.marks[sk]
if mark == sk {
l.marks = append(l.marks[:index], l.marks[index+1:]...)
return
}
}
l.marks = append(l.marks, sk)
} }
// Helpers... // Helpers...

View File

@ -172,8 +172,12 @@ func (v *Table) GetSelectedItem() string {
// GetSelectedItems return currently marked or selected items names. // GetSelectedItems return currently marked or selected items names.
func (v *Table) GetSelectedItems() []string { func (v *Table) GetSelectedItems() []string {
if len(v.data.Marks) > 0 { if len(v.data.Marks) > 0 {
items := make([]string, len(v.data.Marks)) var items []string
copy(items, v.data.Marks) for item, marked := range v.data.Marks {
if marked {
items = append(items, item)
}
}
return items return items
} }
return []string{v.GetSelectedItem()} return []string{v.GetSelectedItem()}