mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor sort into containermap struct
This commit is contained in:
parent
12ce9b1874
commit
995285ebb9
32
grid.go
32
grid.go
@ -8,32 +8,20 @@ import (
|
|||||||
|
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
cursorPos uint
|
cursorPos uint
|
||||||
sortField string
|
containers *ContainerMap
|
||||||
containers map[string]*Container
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grid) AddContainer(id, names string) {
|
func NewGrid() *Grid {
|
||||||
g.containers[id] = NewContainer(id, names)
|
return &Grid{
|
||||||
|
cursorPos: 0,
|
||||||
|
containers: NewContainerMap(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return number of containers/rows
|
|
||||||
func (g *Grid) Len() uint {
|
|
||||||
return uint(len(g.containers))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
var containers []*Container
|
for _, c := range g.containers.Sorted() {
|
||||||
|
|
||||||
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)
|
ids = append(ids, c.id)
|
||||||
}
|
}
|
||||||
return ids
|
return ids
|
||||||
@ -41,8 +29,7 @@ func (g *Grid) CIDs() []string {
|
|||||||
|
|
||||||
// Redraw the cursor with the currently selected row
|
// Redraw the cursor with the currently selected row
|
||||||
func (g *Grid) Cursor() {
|
func (g *Grid) Cursor() {
|
||||||
for n, id := range g.CIDs() {
|
for n, c := range g.containers.Sorted() {
|
||||||
c := g.containers[id]
|
|
||||||
if uint(n) == g.cursorPos {
|
if uint(n) == g.cursorPos {
|
||||||
c.widgets.name.TextFgColor = ui.ColorDefault
|
c.widgets.name.TextFgColor = ui.ColorDefault
|
||||||
c.widgets.name.TextBgColor = ui.ColorWhite
|
c.widgets.name.TextBgColor = ui.ColorWhite
|
||||||
@ -55,8 +42,7 @@ func (g *Grid) Cursor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grid) Rows() (rows []*ui.Row) {
|
func (g *Grid) Rows() (rows []*ui.Row) {
|
||||||
for _, cid := range g.CIDs() {
|
for _, c := range g.containers.Sorted() {
|
||||||
c := g.containers[cid]
|
|
||||||
rows = append(rows, c.widgets.MakeRow())
|
rows = append(rows, c.widgets.MakeRow())
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
@ -106,7 +92,7 @@ func Display(g *Grid) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
||||||
if g.cursorPos < (g.Len() - 1) {
|
if g.cursorPos < (g.containers.Len() - 1) {
|
||||||
g.cursorPos += 1
|
g.cursorPos += 1
|
||||||
g.Cursor()
|
g.Cursor()
|
||||||
}
|
}
|
||||||
|
12
main.go
12
main.go
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
@ -20,11 +19,6 @@ func getContainers(client *docker.Client) []docker.APIContainers {
|
|||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return primary container name
|
|
||||||
func parseName(names []string) string {
|
|
||||||
return strings.Replace(names[0], "/", "", -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dockerhost := os.Getenv("DOCKER_HOST")
|
dockerhost := os.Getenv("DOCKER_HOST")
|
||||||
if dockerhost == "" {
|
if dockerhost == "" {
|
||||||
@ -36,12 +30,12 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
g := &Grid{0, "id", make(map[string]*Container)}
|
g := NewGrid()
|
||||||
for _, c := range getContainers(client) {
|
for _, c := range getContainers(client) {
|
||||||
g.AddContainer(c.ID[:12], parseName(c.Names))
|
g.containers.Add(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range g.containers {
|
for _, c := range g.containers.All() {
|
||||||
c.Collect(client)
|
c.Collect(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
68
sort.go
68
sort.go
@ -2,18 +2,66 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContainerSorter interface {
|
func NewContainerMap() *ContainerMap {
|
||||||
sort.Interface
|
return &ContainerMap{
|
||||||
Sort()
|
containers: make(map[string]*Container),
|
||||||
|
sortField: "id",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var Sorters = map[string][]Container{
|
type ContainerMap struct {
|
||||||
"id": ByID{},
|
containers map[string]*Container
|
||||||
"name": ByName{},
|
sortField string
|
||||||
"cpu": ByCPU{},
|
}
|
||||||
"mem": ByMem{},
|
|
||||||
|
// Return number of containers/rows
|
||||||
|
func (cm *ContainerMap) Len() uint {
|
||||||
|
return uint(len(cm.containers))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cm *ContainerMap) Add(c docker.APIContainers) {
|
||||||
|
id := c.ID[:12]
|
||||||
|
name := strings.Replace(c.Names[0], "/", "", 1) // use primary container name
|
||||||
|
cm.containers[id] = NewContainer(id, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a single container, by ID
|
||||||
|
func (cm *ContainerMap) Get(id string) *Container {
|
||||||
|
return cm.containers[id]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return array of all containers
|
||||||
|
func (cm *ContainerMap) All() []*Container {
|
||||||
|
var containers []*Container
|
||||||
|
for _, c := range cm.containers {
|
||||||
|
containers = append(containers, c)
|
||||||
|
}
|
||||||
|
return containers
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return array of containers, sorted by field
|
||||||
|
func (cm *ContainerMap) Sorted() []*Container {
|
||||||
|
containers := cm.All()
|
||||||
|
|
||||||
|
switch cm.sortField {
|
||||||
|
case "id":
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
case "name":
|
||||||
|
sort.Sort(ByName(containers))
|
||||||
|
case "cpu":
|
||||||
|
sort.Sort(ByCPU(containers))
|
||||||
|
case "mem":
|
||||||
|
sort.Sort(ByMem(containers))
|
||||||
|
default:
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
}
|
||||||
|
|
||||||
|
return containers
|
||||||
}
|
}
|
||||||
|
|
||||||
type ByID []*Container
|
type ByID []*Container
|
||||||
@ -21,25 +69,21 @@ type ByID []*Container
|
|||||||
func (a ByID) Len() int { return len(a) }
|
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) 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) 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
|
type ByName []*Container
|
||||||
|
|
||||||
func (a ByName) Len() int { return len(a) }
|
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) 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) 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
|
type ByCPU []*Container
|
||||||
|
|
||||||
func (a ByCPU) Len() int { return len(a) }
|
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) 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) 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
|
type ByMem []*Container
|
||||||
|
|
||||||
func (a ByMem) Len() int { return len(a) }
|
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) 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) 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