From 062c4298f22dd6d5567a5a16011b28296f33325e Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Tue, 3 Jan 2017 17:37:09 +0000 Subject: [PATCH] init global config --- config.go | 13 +++++++++++++ containermap.go | 7 ++++--- grid.go | 4 ++-- main.go | 1 + menu.go | 11 ++--------- sort.go | 36 ++++++++++++++++-------------------- 6 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..13e8c4a --- /dev/null +++ b/config.go @@ -0,0 +1,13 @@ +package main + +type Config struct { + sortField string +} + +var DefaultConfig = NewDefaultConfig() + +func NewDefaultConfig() Config { + return Config{ + sortField: "id", + } +} diff --git a/containermap.go b/containermap.go index 8227e45..d7227fc 100644 --- a/containermap.go +++ b/containermap.go @@ -23,18 +23,18 @@ func NewContainerMap() *ContainerMap { } cm := &ContainerMap{ + config: DefaultConfig, client: client, containers: make(map[string]*Container), - sortField: SortFields[0], } cm.Refresh() return cm } type ContainerMap struct { + config Config client *docker.Client containers map[string]*Container - sortField string } func (cm *ContainerMap) Refresh() { @@ -76,11 +76,12 @@ func (cm *ContainerMap) Get(id string) *Container { return cm.containers[id] } -// Return array of all containers +// Return array of all containers, sorted by field func (cm *ContainerMap) All() []*Container { var containers []*Container for _, c := range cm.containers { containers = append(containers, c) } + SortContainers(cm.config.sortField, containers) return containers } diff --git a/grid.go b/grid.go index 0976c8b..70a4d9b 100644 --- a/grid.go +++ b/grid.go @@ -14,7 +14,7 @@ type Grid struct { func NewGrid() *Grid { containerMap := NewContainerMap() - containers := containerMap.Sorted() + containers := containerMap.All() return &Grid{ cursorID: containers[0].id, containers: containers, @@ -136,7 +136,7 @@ func Display(g *Grid) bool { ui.StopLoop() }) 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() }) diff --git a/main.go b/main.go index 5311264..f2dd8ce 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ func main() { panic(err) } defer ui.Close() + g := NewGrid() for { exit := Display(g) diff --git a/menu.go b/menu.go index fb24eca..db0759b 100644 --- a/menu.go +++ b/menu.go @@ -30,16 +30,9 @@ func (m *Menu) Buffer() ui.Buffer { buf := m.Block.Buffer() 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 - // invert bg/fg colors on currently selected row for _, ch := range item { + // invert bg/fg colors on currently selected row if m.Selectable && n == m.cursorPos { cell = ui.Cell{Ch: ch, Fg: m.TextBgColor, Bg: m.TextFgColor} } else { @@ -100,7 +93,7 @@ func SortMenu(g *Grid) { m.Down() }) ui.Handle("/sys/kbd/", func(ui.Event) { - g.containerMap.sortField = m.Items[m.cursorPos] + g.containerMap.config.sortField = m.Items[m.cursorPos] ui.StopLoop() }) ui.Loop() diff --git a/sort.go b/sort.go index af75ad5..4f3b956 100644 --- a/sort.go +++ b/sort.go @@ -6,6 +6,22 @@ import ( 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 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) 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 } - -// 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 -}