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:
@ -14,6 +14,7 @@ type Container struct {
|
|||||||
Widgets *compact.Compact
|
Widgets *compact.Compact
|
||||||
updater cwidgets.WidgetUpdater
|
updater cwidgets.WidgetUpdater
|
||||||
collector metrics.Collector
|
collector metrics.Collector
|
||||||
|
display bool // display this container in compact view
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(id string, collector metrics.Collector) *Container {
|
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()] }
|
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
|
// Refresh containers from source
|
||||||
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
|
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
|
||||||
oldLen := gc.Len()
|
oldLen := gc.Len()
|
||||||
gc.containers = gc.cSource.All().Filter()
|
gc.containers = gc.cSource.All()
|
||||||
if oldLen != gc.Len() {
|
if oldLen != gc.Len() {
|
||||||
lenChanged = true
|
lenChanged = true
|
||||||
}
|
}
|
||||||
|
@ -136,6 +136,7 @@ func (cm *DockerContainerSource) All() (containers Containers) {
|
|||||||
containers = append(containers, c)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
sort.Sort(containers)
|
sort.Sort(containers)
|
||||||
|
containers.Filter()
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
grid.go
2
grid.go
@ -25,7 +25,7 @@ func RedrawRows(clr bool) {
|
|||||||
|
|
||||||
var cursorVisible bool
|
var cursorVisible bool
|
||||||
max := maxRows()
|
max := maxRows()
|
||||||
for n, c := range cursor.containers {
|
for n, c := range cursor.Filtered() {
|
||||||
if n >= max {
|
if n >= max {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ func (cs *MockContainerSource) Get(id string) (*Container, bool) {
|
|||||||
// Return array of all containers, sorted by field
|
// Return array of all containers, sorted by field
|
||||||
func (cs *MockContainerSource) All() Containers {
|
func (cs *MockContainerSource) All() Containers {
|
||||||
sort.Sort(cs.containers)
|
sort.Sort(cs.containers)
|
||||||
|
cs.containers.Filter()
|
||||||
return cs.containers
|
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])
|
return f(a[i], a[j])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Containers) Filter() (filtered []*Container) {
|
func (a Containers) Filter() {
|
||||||
filter := config.GetVal("filterStr")
|
filter := config.GetVal("filterStr")
|
||||||
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
|
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
|
||||||
|
|
||||||
for _, c := range a {
|
for _, c := range a {
|
||||||
|
c.display = true
|
||||||
// Apply name filter
|
// Apply name filter
|
||||||
if re.FindAllString(c.GetMeta("name"), 1) == nil {
|
if re.FindAllString(c.GetMeta("name"), 1) == nil {
|
||||||
continue
|
c.display = false
|
||||||
}
|
}
|
||||||
// Apply state filter
|
// Apply state filter
|
||||||
if !config.GetSwitchVal("allContainers") && c.GetMeta("state") != "running" {
|
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 }
|
func sumNet(c *Container) int64 { return c.NetRx + c.NetTx }
|
||||||
|
Reference in New Issue
Block a user