mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
init global config
This commit is contained in:
parent
e1e989c220
commit
062c4298f2
13
config.go
Normal file
13
config.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
sortField string
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultConfig = NewDefaultConfig()
|
||||||
|
|
||||||
|
func NewDefaultConfig() Config {
|
||||||
|
return Config{
|
||||||
|
sortField: "id",
|
||||||
|
}
|
||||||
|
}
|
@ -23,18 +23,18 @@ func NewContainerMap() *ContainerMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cm := &ContainerMap{
|
cm := &ContainerMap{
|
||||||
|
config: DefaultConfig,
|
||||||
client: client,
|
client: client,
|
||||||
containers: make(map[string]*Container),
|
containers: make(map[string]*Container),
|
||||||
sortField: SortFields[0],
|
|
||||||
}
|
}
|
||||||
cm.Refresh()
|
cm.Refresh()
|
||||||
return cm
|
return cm
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContainerMap struct {
|
type ContainerMap struct {
|
||||||
|
config Config
|
||||||
client *docker.Client
|
client *docker.Client
|
||||||
containers map[string]*Container
|
containers map[string]*Container
|
||||||
sortField string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ContainerMap) Refresh() {
|
func (cm *ContainerMap) Refresh() {
|
||||||
@ -76,11 +76,12 @@ func (cm *ContainerMap) Get(id string) *Container {
|
|||||||
return cm.containers[id]
|
return cm.containers[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return array of all containers
|
// Return array of all containers, sorted by field
|
||||||
func (cm *ContainerMap) All() []*Container {
|
func (cm *ContainerMap) All() []*Container {
|
||||||
var containers []*Container
|
var containers []*Container
|
||||||
for _, c := range cm.containers {
|
for _, c := range cm.containers {
|
||||||
containers = append(containers, c)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
|
SortContainers(cm.config.sortField, containers)
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
4
grid.go
4
grid.go
@ -14,7 +14,7 @@ type Grid struct {
|
|||||||
|
|
||||||
func NewGrid() *Grid {
|
func NewGrid() *Grid {
|
||||||
containerMap := NewContainerMap()
|
containerMap := NewContainerMap()
|
||||||
containers := containerMap.Sorted()
|
containers := containerMap.All()
|
||||||
return &Grid{
|
return &Grid{
|
||||||
cursorID: containers[0].id,
|
cursorID: containers[0].id,
|
||||||
containers: containers,
|
containers: containers,
|
||||||
@ -136,7 +136,7 @@ func Display(g *Grid) bool {
|
|||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||||
g.containers = g.containerMap.Sorted() // refresh containers for current sort order
|
g.containers = g.containerMap.All() // refresh containers for current sort order
|
||||||
g.redrawRows()
|
g.redrawRows()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
1
main.go
1
main.go
@ -9,6 +9,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer ui.Close()
|
defer ui.Close()
|
||||||
|
|
||||||
g := NewGrid()
|
g := NewGrid()
|
||||||
for {
|
for {
|
||||||
exit := Display(g)
|
exit := Display(g)
|
||||||
|
11
menu.go
11
menu.go
@ -30,16 +30,9 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
buf := m.Block.Buffer()
|
buf := m.Block.Buffer()
|
||||||
|
|
||||||
for n, item := range m.Items {
|
for n, item := range m.Items {
|
||||||
//if n >= m.innerArea.Dy() {
|
|
||||||
//buf.Set(m.innerArea.Min.X+m.innerArea.Dx()-1,
|
|
||||||
//m.innerArea.Min.Y+m.innerArea.Dy()-1,
|
|
||||||
//ui.Cell{Ch: '…', Fg: m.TextFgColor, Bg: m.TextBgColor})
|
|
||||||
//break
|
|
||||||
//}
|
|
||||||
|
|
||||||
x := 2 // initial offset
|
x := 2 // initial offset
|
||||||
// invert bg/fg colors on currently selected row
|
|
||||||
for _, ch := range item {
|
for _, ch := range item {
|
||||||
|
// invert bg/fg colors on currently selected row
|
||||||
if m.Selectable && n == m.cursorPos {
|
if m.Selectable && n == m.cursorPos {
|
||||||
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
|
cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor}
|
||||||
} else {
|
} else {
|
||||||
@ -100,7 +93,7 @@ func SortMenu(g *Grid) {
|
|||||||
m.Down()
|
m.Down()
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
g.containerMap.sortField = m.Items[m.cursorPos]
|
g.containerMap.config.sortField = m.Items[m.cursorPos]
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Loop()
|
ui.Loop()
|
||||||
|
36
sort.go
36
sort.go
@ -6,6 +6,22 @@ import (
|
|||||||
|
|
||||||
var SortFields = []string{"id", "name", "cpu", "mem"}
|
var SortFields = []string{"id", "name", "cpu", "mem"}
|
||||||
|
|
||||||
|
// Sort array of containers by field
|
||||||
|
func SortContainers(field string, containers []*Container) {
|
||||||
|
switch field {
|
||||||
|
case "id":
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
case "name":
|
||||||
|
sort.Sort(ByName(containers))
|
||||||
|
case "cpu":
|
||||||
|
sort.Sort(sort.Reverse(ByCPU(containers)))
|
||||||
|
case "mem":
|
||||||
|
sort.Sort(sort.Reverse(ByMem(containers)))
|
||||||
|
default:
|
||||||
|
sort.Sort(ByID(containers))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ByID []*Container
|
type ByID []*Container
|
||||||
|
|
||||||
func (a ByID) Len() int { return len(a) }
|
func (a ByID) Len() int { return len(a) }
|
||||||
@ -29,23 +45,3 @@ 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 }
|
||||||
|
|
||||||
// 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(sort.Reverse(ByCPU(containers)))
|
|
||||||
case "mem":
|
|
||||||
sort.Sort(sort.Reverse(ByMem(containers)))
|
|
||||||
default:
|
|
||||||
sort.Sort(ByID(containers))
|
|
||||||
}
|
|
||||||
|
|
||||||
return containers
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user