From 2e77e400446387cb9704a312109abd912bcb03a5 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Mon, 9 Jan 2017 15:02:34 +0000 Subject: [PATCH] add displayitems override, padding updates to menu --- container.go | 6 +++--- containermap.go | 10 +++++++++- grid.go | 24 ++++++++++++------------ menus.go | 9 +++++++++ sort.go | 6 +++--- widgets/menu.go | 45 +++++++++++++++++++++++++++------------------ 6 files changed, 63 insertions(+), 37 deletions(-) diff --git a/container.go b/container.go index 74ba9a8..c0e7d2f 100644 --- a/container.go +++ b/container.go @@ -12,7 +12,7 @@ type Container struct { name string done chan bool widgets widgets.ContainerWidgets - reader *MetricsReader + metrics *MetricsReader } func NewContainer(c docker.APIContainers) *Container { @@ -23,7 +23,7 @@ func NewContainer(c docker.APIContainers) *Container { name: name, done: make(chan bool), widgets: widgets.NewCompact(id, name), - reader: NewMetricsReader(), + metrics: NewMetricsReader(), } } @@ -49,7 +49,7 @@ func (c *Container) Collect(client *docker.Client) { }() go func() { - for metrics := range c.reader.Read(stats) { + for metrics := range c.metrics.Read(stats) { c.widgets.SetCPU(metrics.CPUUtil) c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent) c.widgets.SetNet(metrics.NetRx, metrics.NetTx) diff --git a/containermap.go b/containermap.go index 89bf63d..ded926a 100644 --- a/containermap.go +++ b/containermap.go @@ -14,7 +14,6 @@ func NewContainerMap() *ContainerMap { if err != nil { panic(err) } - cm := &ContainerMap{ client: client, containers: make(map[string]*Container), @@ -46,6 +45,15 @@ func (cm *ContainerMap) Refresh() { } } +// Kill a container by ID +func (cm *ContainerMap) Kill(id string, sig docker.Signal) error { + opts := docker.KillContainerOptions{ + ID: id, + Signal: sig, + } + return cm.client.KillContainer(opts) +} + // Return number of containers/rows func (cm *ContainerMap) Len() uint { return uint(len(cm.containers)) diff --git a/grid.go b/grid.go index 1f0cfa6..e543737 100644 --- a/grid.go +++ b/grid.go @@ -8,20 +8,20 @@ import ( ) type Grid struct { - cursorID string // id of currently selected container - containers []*Container // sorted slice of containers - containerMap *ContainerMap - header *widgets.CTopHeader + cursorID string // id of currently selected container + cmap *ContainerMap + containers []*Container // sorted slice of containers + header *widgets.CTopHeader } func NewGrid() *Grid { - containerMap := NewContainerMap() - containers := containerMap.All() + cmap := NewContainerMap() + containers := cmap.All() return &Grid{ - cursorID: containers[0].id, - containers: containers, - containerMap: containerMap, - header: widgets.NewCTopHeader(), + cursorID: containers[0].id, + cmap: cmap, + containers: containers, + header: widgets.NewCTopHeader(), } } @@ -116,7 +116,7 @@ func OpenMenu(m func()) { func (g *Grid) ExpandView() { ResetView() defer ResetView() - container := g.containerMap.Get(g.cursorID) + container := g.cmap.Get(g.cursorID) container.Expand() container.widgets.Render() container.Collapse() @@ -153,7 +153,7 @@ func Display(g *Grid) bool { ui.StopLoop() }) ui.Handle("/timer/1s", func(e ui.Event) { - g.containers = g.containerMap.All() // refresh containers for current sort order + g.containers = g.cmap.All() // refresh containers for current sort order g.redrawRows() }) diff --git a/menus.go b/menus.go index cbb80f6..c231bb8 100644 --- a/menus.go +++ b/menus.go @@ -29,6 +29,15 @@ func SortMenu() { m.TextFgColor = ui.ColorWhite m.BorderLabel = "Sort Field" m.BorderFg = ui.ColorCyan + + // set cursor position to current sort field + current := GlobalConfig["sortField"] + for n, field := range m.Items { + if field == current { + m.CursorPos = n + } + } + ui.Render(m) m.NavigationHandlers() ui.Handle("/sys/kbd/", func(ui.Event) { diff --git a/sort.go b/sort.go index 6fb7988..b0a9cc7 100644 --- a/sort.go +++ b/sort.go @@ -40,16 +40,16 @@ type ByCPU []*Container func (a ByCPU) Len() int { return len(a) } func (a ByCPU) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByCPU) Less(i, j int) bool { return a[i].reader.CPUUtil < a[j].reader.CPUUtil } +func (a ByCPU) Less(i, j int) bool { return a[i].metrics.CPUUtil < a[j].metrics.CPUUtil } 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 } +func (a ByMem) Less(i, j int) bool { return a[i].metrics.MemUsage < a[j].metrics.MemUsage } type ByMemPercent []*Container func (a ByMemPercent) Len() int { return len(a) } func (a ByMemPercent) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByMemPercent) Less(i, j int) bool { return a[i].reader.MemPercent < a[j].reader.MemPercent } +func (a ByMemPercent) Less(i, j int) bool { return a[i].metrics.MemPercent < a[j].metrics.MemPercent } diff --git a/widgets/menu.go b/widgets/menu.go index 24890ec..77bdb47 100644 --- a/widgets/menu.go +++ b/widgets/menu.go @@ -5,27 +5,30 @@ import ( ) var ( - padding = 2 - minWidth = 30 + x_padding = 4 + y_padding = 2 + minWidth = 8 ) type Menu struct { ui.Block - Items []string - TextFgColor ui.Attribute - TextBgColor ui.Attribute - Selectable bool - CursorPos int + Items []string + DisplayItems []string + TextFgColor ui.Attribute + TextBgColor ui.Attribute + Selectable bool + CursorPos int } func NewMenu(items []string) *Menu { m := &Menu{ - Block: *ui.NewBlock(), - Items: items, - TextFgColor: ui.ThemeAttr("par.text.fg"), - TextBgColor: ui.ThemeAttr("par.text.bg"), - Selectable: false, - CursorPos: 0, + Block: *ui.NewBlock(), + Items: items, + DisplayItems: []string{}, + TextFgColor: ui.ThemeAttr("par.text.fg"), + TextBgColor: ui.ThemeAttr("par.text.bg"), + Selectable: false, + CursorPos: 0, } m.Width, m.Height = calcSize(items) return m @@ -35,8 +38,14 @@ func (m *Menu) Buffer() ui.Buffer { var cell ui.Cell buf := m.Block.Buffer() - for n, item := range m.Items { - x := padding + // override display of items, if given + items := m.Items + if len(m.DisplayItems) == len(m.Items) { + items = m.DisplayItems + } + + for n, item := range items { + x := x_padding for _, ch := range item { // invert bg/fg colors on currently selected row if m.Selectable && n == m.CursorPos { @@ -44,7 +53,7 @@ func (m *Menu) Buffer() ui.Buffer { } else { cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor} } - buf.Set(x, n+padding, cell) + buf.Set(x, n+y_padding, cell) x++ } } @@ -75,7 +84,7 @@ func (m *Menu) NavigationHandlers() { // return width and height based on menu items func calcSize(items []string) (w, h int) { - h = len(items) + (padding * 2) + h = len(items) + (y_padding * 2) w = minWidth for _, s := range items { @@ -83,7 +92,7 @@ func calcSize(items []string) (w, h int) { w = len(s) } } - w += (padding * 2) + w += (x_padding * 2) return w, h }