init global config

This commit is contained in:
Bradley Cicenas 2017-01-03 17:37:09 +00:00
parent e1e989c220
commit 062c4298f2
6 changed files with 38 additions and 34 deletions

13
config.go Normal file
View File

@ -0,0 +1,13 @@
package main
type Config struct {
sortField string
}
var DefaultConfig = NewDefaultConfig()
func NewDefaultConfig() Config {
return Config{
sortField: "id",
}
}

View File

@ -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
} }

View File

@ -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()
}) })

View File

@ -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
View File

@ -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
View File

@ -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
}