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 {
|
||||
cursorPos uint
|
||||
sortField string
|
||||
containers map[string]*Container
|
||||
containers *ContainerMap
|
||||
}
|
||||
|
||||
func (g *Grid) AddContainer(id, names string) {
|
||||
g.containers[id] = NewContainer(id, names)
|
||||
func NewGrid() *Grid {
|
||||
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
|
||||
func (g *Grid) CIDs() []string {
|
||||
var ids []string
|
||||
var containers []*Container
|
||||
|
||||
for _, c := range g.containers {
|
||||
containers = append(containers, c)
|
||||
}
|
||||
|
||||
sorter := Sorters[g.sortField]
|
||||
sorter(containers).Sort()
|
||||
|
||||
for _, c := range containers {
|
||||
for _, c := range g.containers.Sorted() {
|
||||
ids = append(ids, c.id)
|
||||
}
|
||||
return ids
|
||||
@ -41,8 +29,7 @@ func (g *Grid) CIDs() []string {
|
||||
|
||||
// Redraw the cursor with the currently selected row
|
||||
func (g *Grid) Cursor() {
|
||||
for n, id := range g.CIDs() {
|
||||
c := g.containers[id]
|
||||
for n, c := range g.containers.Sorted() {
|
||||
if uint(n) == g.cursorPos {
|
||||
c.widgets.name.TextFgColor = ui.ColorDefault
|
||||
c.widgets.name.TextBgColor = ui.ColorWhite
|
||||
@ -55,8 +42,7 @@ func (g *Grid) Cursor() {
|
||||
}
|
||||
|
||||
func (g *Grid) Rows() (rows []*ui.Row) {
|
||||
for _, cid := range g.CIDs() {
|
||||
c := g.containers[cid]
|
||||
for _, c := range g.containers.Sorted() {
|
||||
rows = append(rows, c.widgets.MakeRow())
|
||||
}
|
||||
return rows
|
||||
@ -106,7 +92,7 @@ func Display(g *Grid) {
|
||||
}
|
||||
})
|
||||
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.Cursor()
|
||||
}
|
||||
|
12
main.go
12
main.go
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
)
|
||||
@ -20,11 +19,6 @@ func getContainers(client *docker.Client) []docker.APIContainers {
|
||||
return containers
|
||||
}
|
||||
|
||||
// Return primary container name
|
||||
func parseName(names []string) string {
|
||||
return strings.Replace(names[0], "/", "", -1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
dockerhost := os.Getenv("DOCKER_HOST")
|
||||
if dockerhost == "" {
|
||||
@ -36,12 +30,12 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g := &Grid{0, "id", make(map[string]*Container)}
|
||||
g := NewGrid()
|
||||
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)
|
||||
}
|
||||
|
||||
|
68
sort.go
68
sort.go
@ -2,18 +2,66 @@ package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/fsouza/go-dockerclient"
|
||||
)
|
||||
|
||||
type ContainerSorter interface {
|
||||
sort.Interface
|
||||
Sort()
|
||||
func NewContainerMap() *ContainerMap {
|
||||
return &ContainerMap{
|
||||
containers: make(map[string]*Container),
|
||||
sortField: "id",
|
||||
}
|
||||
}
|
||||
|
||||
var Sorters = map[string][]Container{
|
||||
"id": ByID{},
|
||||
"name": ByName{},
|
||||
"cpu": ByCPU{},
|
||||
"mem": ByMem{},
|
||||
type ContainerMap struct {
|
||||
containers map[string]*Container
|
||||
sortField string
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -21,25 +69,21 @@ 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