filter Containers in place

This commit is contained in:
Bradley Cicenas 2017-03-08 11:26:22 +11:00
parent bf4d59c251
commit 2d2d58d47f
6 changed files with 21 additions and 9 deletions

View File

@ -14,6 +14,7 @@ type Container struct {
Widgets *compact.Compact
updater cwidgets.WidgetUpdater
collector metrics.Collector
display bool // display this container in compact view
}
func NewContainer(id string, collector metrics.Collector) *Container {

View File

@ -16,13 +16,24 @@ func NewGridCursor() *GridCursor {
}
}
func (gc *GridCursor) Len() int { return len(gc.containers) }
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().Filter()
gc.containers = gc.cSource.All()
if oldLen != gc.Len() {
lenChanged = true
}

View File

@ -136,6 +136,7 @@ func (cm *DockerContainerSource) All() (containers Containers) {
containers = append(containers, c)
}
sort.Sort(containers)
containers.Filter()
return containers
}

View File

@ -25,7 +25,7 @@ func RedrawRows(clr bool) {
var cursorVisible bool
max := maxRows()
for n, c := range cursor.containers {
for n, c := range cursor.Filtered() {
if n >= max {
break
}

View File

@ -66,6 +66,7 @@ func (cs *MockContainerSource) Get(id string) (*Container, bool) {
// Return array of all containers, sorted by field
func (cs *MockContainerSource) All() Containers {
sort.Sort(cs.containers)
cs.containers.Filter()
return cs.containers
}

10
sort.go
View File

@ -83,23 +83,21 @@ func (a Containers) Less(i, j int) bool {
return f(a[i], a[j])
}
func (a Containers) Filter() (filtered []*Container) {
func (a Containers) Filter() {
filter := config.GetVal("filterStr")
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
for _, c := range a {
c.display = true
// Apply name filter
if re.FindAllString(c.GetMeta("name"), 1) == nil {
continue
c.display = false
}
// Apply state filter
if !config.GetSwitchVal("allContainers") && c.GetMeta("state") != "running" {
continue
c.display = false
}
filtered = append(filtered, c)
}
return filtered
}
func sumNet(c *Container) int64 { return c.NetRx + c.NetTx }