mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
filter Containers in place
This commit is contained in:
parent
bf4d59c251
commit
2d2d58d47f
@ -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 {
|
||||
|
15
cursor.go
15
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
|
||||
}
|
||||
|
@ -136,6 +136,7 @@ func (cm *DockerContainerSource) All() (containers Containers) {
|
||||
containers = append(containers, c)
|
||||
}
|
||||
sort.Sort(containers)
|
||||
containers.Filter()
|
||||
return containers
|
||||
}
|
||||
|
||||
|
2
grid.go
2
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
|
||||
}
|
||||
|
@ -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
10
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 }
|
||||
|
Loading…
x
Reference in New Issue
Block a user