mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add custom container sorter
This commit is contained in:
parent
d5b35fe01b
commit
12ce9b1874
16
grid.go
16
grid.go
@ -2,13 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
|
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
cursorPos uint
|
cursorPos uint
|
||||||
|
sortField string
|
||||||
containers map[string]*Container
|
containers map[string]*Container
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,10 +24,18 @@ func (g *Grid) Len() uint {
|
|||||||
// Return sorted list of container IDs
|
// Return sorted list of container IDs
|
||||||
func (g *Grid) CIDs() []string {
|
func (g *Grid) CIDs() []string {
|
||||||
var ids []string
|
var ids []string
|
||||||
for id, _ := range g.containers {
|
var containers []*Container
|
||||||
ids = append(ids, id)
|
|
||||||
|
for _, c := range g.containers {
|
||||||
|
containers = append(containers, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
sorter := Sorters[g.sortField]
|
||||||
|
sorter(containers).Sort()
|
||||||
|
|
||||||
|
for _, c := range containers {
|
||||||
|
ids = append(ids, c.id)
|
||||||
}
|
}
|
||||||
sort.Strings(ids)
|
|
||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
main.go
2
main.go
@ -36,7 +36,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
g := &Grid{0, make(map[string]*Container)}
|
g := &Grid{0, "id", make(map[string]*Container)}
|
||||||
for _, c := range getContainers(client) {
|
for _, c := range getContainers(client) {
|
||||||
g.AddContainer(c.ID[:12], parseName(c.Names))
|
g.AddContainer(c.ID[:12], parseName(c.Names))
|
||||||
}
|
}
|
||||||
|
45
sort.go
Normal file
45
sort.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ContainerSorter interface {
|
||||||
|
sort.Interface
|
||||||
|
Sort()
|
||||||
|
}
|
||||||
|
|
||||||
|
var Sorters = map[string][]Container{
|
||||||
|
"id": ByID{},
|
||||||
|
"name": ByName{},
|
||||||
|
"cpu": ByCPU{},
|
||||||
|
"mem": ByMem{},
|
||||||
|
}
|
||||||
|
|
||||||
|
type ByID []*Container
|
||||||
|
|
||||||
|
func (a ByID) Len() int { return len(a) }
|
||||||
|
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByID) Less(i, j int) bool { return a[i].id < a[j].id }
|
||||||
|
func (a ByID) Sort() { sort.Sort(a) } // Sort is a convenience method.
|
||||||
|
|
||||||
|
type ByName []*Container
|
||||||
|
|
||||||
|
func (a ByName) Len() int { return len(a) }
|
||||||
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByName) Less(i, j int) bool { return a[i].id < a[j].id }
|
||||||
|
func (a ByName) Sort() { sort.Sort(a) } // Sort is a convenience method.
|
||||||
|
|
||||||
|
type ByCPU []*Container
|
||||||
|
|
||||||
|
func (a ByCPU) Len() int { return len(a) }
|
||||||
|
func (a ByCPU) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByCPU) Less(i, j int) bool { return a[i].reader.CPUUtil < a[j].reader.CPUUtil }
|
||||||
|
func (a ByCPU) Sort() { sort.Sort(a) } // Sort is a convenience method.
|
||||||
|
|
||||||
|
type ByMem []*Container
|
||||||
|
|
||||||
|
func (a ByMem) Len() int { return len(a) }
|
||||||
|
func (a ByMem) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByMem) Less(i, j int) bool { return a[i].reader.MemUsage < a[j].reader.MemUsage }
|
||||||
|
func (a ByMem) Sort() { sort.Sort(a) } // Sort is a convenience method.
|
Loading…
Reference in New Issue
Block a user