Skip to content

Commit

Permalink
fix cursor highlighting for newly filtered containers
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Mar 8, 2017
1 parent 2d2d58d commit d7f9f71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type GridCursor struct {
selectedID string // id of currently selected container
filtered Containers
containers Containers
cSource ContainerSource
}
Expand All @@ -16,24 +17,13 @@ func NewGridCursor() *GridCursor {
}
}

func (gc *GridCursor) Len() int { return len(gc.Filtered()) }
func (gc *GridCursor) Len() int { return len(gc.filtered) }
func (gc *GridCursor) Selected() *Container { return gc.containers[gc.Idx()] }

// Return Containers filtered by display bool
func (gc *GridCursor) Filtered() Containers {
var filtered Containers
for _, c := range gc.containers {
if c.display {
filtered = append(filtered, c)
}
}
return filtered
}

// Refresh containers from source
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
oldLen := gc.Len()
gc.containers = gc.cSource.All()
gc.setContainers(gc.cSource.All())
if oldLen != gc.Len() {
lenChanged = true
}
Expand All @@ -43,8 +33,22 @@ func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
return lenChanged
}

func (gc *GridCursor) setContainers(c Containers) {
gc.containers = c
// Containers filtered by display bool
gc.filtered = Containers{}
for _, c := range gc.containers {
if c.display {
gc.filtered = append(gc.filtered, c)
}
}
}

// Set an initial cursor position, if possible
func (gc *GridCursor) Reset() {
for _, c := range gc.containers {
c.Widgets.Name.UnHighlight()
}
if gc.Len() > 0 {
gc.selectedID = gc.containers[0].Id
gc.containers[0].Widgets.Name.Highlight()
Expand Down
2 changes: 1 addition & 1 deletion grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func RedrawRows(clr bool) {

var cursorVisible bool
max := maxRows()
for n, c := range cursor.Filtered() {
for n, c := range cursor.filtered {
if n >= max {
break
}
Expand Down

0 comments on commit d7f9f71

Please sign in to comment.