diff --git a/container.go b/container.go index cf7677e..68cdbc2 100644 --- a/container.go +++ b/container.go @@ -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 { diff --git a/cursor.go b/cursor.go index 740c858..531d471 100644 --- a/cursor.go +++ b/cursor.go @@ -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 } diff --git a/dockersource.go b/dockersource.go index 1c6eb51..b716d80 100644 --- a/dockersource.go +++ b/dockersource.go @@ -136,6 +136,7 @@ func (cm *DockerContainerSource) All() (containers Containers) { containers = append(containers, c) } sort.Sort(containers) + containers.Filter() return containers } diff --git a/grid.go b/grid.go index 0a65031..affd246 100644 --- a/grid.go +++ b/grid.go @@ -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 } diff --git a/mocksource.go b/mocksource.go index 488ad65..7ac3633 100644 --- a/mocksource.go +++ b/mocksource.go @@ -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 } diff --git a/sort.go b/sort.go index d618d1b..1af108c 100644 --- a/sort.go +++ b/sort.go @@ -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 }