diff --git a/config.go b/config.go index f6cb5f1..5b8e893 100644 --- a/config.go +++ b/config.go @@ -4,20 +4,32 @@ import ( "os" ) -type Config struct { - dockerHost string - sortField string +var configChan = make(chan ConfigMsg) + +type Config map[string]string + +type ConfigMsg struct { + key string + val string } -var DefaultConfig = NewDefaultConfig() +func updateConfig(k, v string) { + configChan <- ConfigMsg{k, v} +} func NewDefaultConfig() Config { docker := os.Getenv("DOCKER_HOST") if docker == "" { docker = "unix:///var/run/docker.sock" } - return Config{ - dockerHost: docker, - sortField: "id", + config := Config{ + "dockerHost": docker, + "sortField": "id", } + go func() { + for m := range configChan { + config[m.key] = m.val + } + }() + return config } diff --git a/container.go b/container.go index b6d61a8..024a68a 100644 --- a/container.go +++ b/container.go @@ -12,7 +12,7 @@ type Container struct { name string done chan bool stats chan *docker.Stats - widgets *widgets.Compact + widgets widgets.ContainerWidgets reader *StatReader } diff --git a/containermap.go b/containermap.go index 709f9e3..7704af3 100644 --- a/containermap.go +++ b/containermap.go @@ -9,10 +9,10 @@ var filters = map[string][]string{ } func NewContainerMap() *ContainerMap { - config := DefaultConfig + config := NewDefaultConfig() // init docker client - client, err := docker.NewClient(config.dockerHost) + client, err := docker.NewClient(config["dockerHost"]) if err != nil { panic(err) } @@ -66,6 +66,6 @@ func (cm *ContainerMap) All() []*Container { for _, c := range cm.containers { containers = append(containers, c) } - SortContainers(cm.config.sortField, containers) + SortContainers(cm.config["sortField"], containers) return containers } diff --git a/grid.go b/grid.go index 8c0faf2..6e21d51 100644 --- a/grid.go +++ b/grid.go @@ -54,11 +54,9 @@ func (g *Grid) cursorDown() { func (g *Grid) redrawCursor() { for _, c := range g.containers { if c.id == g.cursorID { - c.widgets.Name.TextFgColor = ui.ColorDefault - c.widgets.Name.TextBgColor = ui.ColorWhite + c.widgets.Highlight() } else { - c.widgets.Name.TextFgColor = ui.ColorWhite - c.widgets.Name.TextBgColor = ui.ColorDefault + c.widgets.UnHighlight() } ui.Render(ui.Body) } diff --git a/menus.go b/menus.go index c5cf5be..27479ea 100644 --- a/menus.go +++ b/menus.go @@ -32,7 +32,7 @@ func SortMenu(g *Grid) { ui.Render(m) m.NavigationHandlers() ui.Handle("/sys/kbd/", func(ui.Event) { - g.containerMap.config.sortField = m.Items[m.CursorPos] + updateConfig("sortField", m.Items[m.CursorPos]) ui.StopLoop() }) ui.Loop() diff --git a/widgets/compact.go b/widgets/compact.go index adf6f1c..d90df92 100644 --- a/widgets/compact.go +++ b/widgets/compact.go @@ -7,6 +7,15 @@ import ( ui "github.com/gizak/termui" ) +type ContainerWidgets interface { + Row() *ui.Row + Highlight() + UnHighlight() + SetCPU(int) + SetNet(int64, int64) + SetMem(int64, int64, int) +} + type Compact struct { Cid *ui.Par Net *ui.Par @@ -35,6 +44,16 @@ func (w *Compact) Row() *ui.Row { ) } +func (w *Compact) Highlight() { + w.Name.TextFgColor = ui.ColorDefault + w.Name.TextBgColor = ui.ColorWhite +} + +func (w *Compact) UnHighlight() { + w.Name.TextFgColor = ui.ColorWhite + w.Name.TextBgColor = ui.ColorDefault +} + func (w *Compact) SetCPU(val int) { w.Cpu.BarColor = colorScale(val) w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))