add ContainerWidgets interface, config channel

This commit is contained in:
Bradley Cicenas 2017-01-06 12:59:45 +00:00
parent 6d63d09c83
commit f311aad105
6 changed files with 45 additions and 16 deletions

View File

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

View File

@ -12,7 +12,7 @@ type Container struct {
name string
done chan bool
stats chan *docker.Stats
widgets *widgets.Compact
widgets widgets.ContainerWidgets
reader *StatReader
}

View File

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

View File

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

View File

@ -32,7 +32,7 @@ func SortMenu(g *Grid) {
ui.Render(m)
m.NavigationHandlers()
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
g.containerMap.config.sortField = m.Items[m.CursorPos]
updateConfig("sortField", m.Items[m.CursorPos])
ui.StopLoop()
})
ui.Loop()

View File

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