add displayitems override, padding updates to menu

This commit is contained in:
Bradley Cicenas
2017-01-09 15:02:34 +00:00
parent ee2b63d21d
commit 2e77e40044
6 changed files with 63 additions and 37 deletions

View File

@@ -12,7 +12,7 @@ type Container struct {
name string name string
done chan bool done chan bool
widgets widgets.ContainerWidgets widgets widgets.ContainerWidgets
reader *MetricsReader metrics *MetricsReader
} }
func NewContainer(c docker.APIContainers) *Container { func NewContainer(c docker.APIContainers) *Container {
@@ -23,7 +23,7 @@ func NewContainer(c docker.APIContainers) *Container {
name: name, name: name,
done: make(chan bool), done: make(chan bool),
widgets: widgets.NewCompact(id, name), widgets: widgets.NewCompact(id, name),
reader: NewMetricsReader(), metrics: NewMetricsReader(),
} }
} }
@@ -49,7 +49,7 @@ func (c *Container) Collect(client *docker.Client) {
}() }()
go func() { go func() {
for metrics := range c.reader.Read(stats) { for metrics := range c.metrics.Read(stats) {
c.widgets.SetCPU(metrics.CPUUtil) c.widgets.SetCPU(metrics.CPUUtil)
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent) c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
c.widgets.SetNet(metrics.NetRx, metrics.NetTx) c.widgets.SetNet(metrics.NetRx, metrics.NetTx)

View File

@@ -14,7 +14,6 @@ func NewContainerMap() *ContainerMap {
if err != nil { if err != nil {
panic(err) panic(err)
} }
cm := &ContainerMap{ cm := &ContainerMap{
client: client, client: client,
containers: make(map[string]*Container), 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 // Return number of containers/rows
func (cm *ContainerMap) Len() uint { func (cm *ContainerMap) Len() uint {
return uint(len(cm.containers)) return uint(len(cm.containers))

12
grid.go
View File

@@ -9,18 +9,18 @@ import (
type Grid struct { type Grid struct {
cursorID string // id of currently selected container cursorID string // id of currently selected container
cmap *ContainerMap
containers []*Container // sorted slice of containers containers []*Container // sorted slice of containers
containerMap *ContainerMap
header *widgets.CTopHeader header *widgets.CTopHeader
} }
func NewGrid() *Grid { func NewGrid() *Grid {
containerMap := NewContainerMap() cmap := NewContainerMap()
containers := containerMap.All() containers := cmap.All()
return &Grid{ return &Grid{
cursorID: containers[0].id, cursorID: containers[0].id,
cmap: cmap,
containers: containers, containers: containers,
containerMap: containerMap,
header: widgets.NewCTopHeader(), header: widgets.NewCTopHeader(),
} }
} }
@@ -116,7 +116,7 @@ func OpenMenu(m func()) {
func (g *Grid) ExpandView() { func (g *Grid) ExpandView() {
ResetView() ResetView()
defer ResetView() defer ResetView()
container := g.containerMap.Get(g.cursorID) container := g.cmap.Get(g.cursorID)
container.Expand() container.Expand()
container.widgets.Render() container.widgets.Render()
container.Collapse() container.Collapse()
@@ -153,7 +153,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.All() // refresh containers for current sort order g.containers = g.cmap.All() // refresh containers for current sort order
g.redrawRows() g.redrawRows()
}) })

View File

@@ -29,6 +29,15 @@ func SortMenu() {
m.TextFgColor = ui.ColorWhite m.TextFgColor = ui.ColorWhite
m.BorderLabel = "Sort Field" m.BorderLabel = "Sort Field"
m.BorderFg = ui.ColorCyan 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) ui.Render(m)
m.NavigationHandlers() m.NavigationHandlers()
ui.Handle("/sys/kbd/<enter>", func(ui.Event) { ui.Handle("/sys/kbd/<enter>", func(ui.Event) {

View File

@@ -40,16 +40,16 @@ type ByCPU []*Container
func (a ByCPU) Len() int { return len(a) } 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) 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 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].metrics.MemUsage < a[j].metrics.MemUsage }
type ByMemPercent []*Container type ByMemPercent []*Container
func (a ByMemPercent) Len() int { return len(a) } 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) 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 }

View File

@@ -5,13 +5,15 @@ import (
) )
var ( var (
padding = 2 x_padding = 4
minWidth = 30 y_padding = 2
minWidth = 8
) )
type Menu struct { type Menu struct {
ui.Block ui.Block
Items []string Items []string
DisplayItems []string
TextFgColor ui.Attribute TextFgColor ui.Attribute
TextBgColor ui.Attribute TextBgColor ui.Attribute
Selectable bool Selectable bool
@@ -22,6 +24,7 @@ func NewMenu(items []string) *Menu {
m := &Menu{ m := &Menu{
Block: *ui.NewBlock(), Block: *ui.NewBlock(),
Items: items, Items: items,
DisplayItems: []string{},
TextFgColor: ui.ThemeAttr("par.text.fg"), TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"), TextBgColor: ui.ThemeAttr("par.text.bg"),
Selectable: false, Selectable: false,
@@ -35,8 +38,14 @@ func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell var cell ui.Cell
buf := m.Block.Buffer() buf := m.Block.Buffer()
for n, item := range m.Items { // override display of items, if given
x := padding 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 { for _, ch := range item {
// invert bg/fg colors on currently selected row // invert bg/fg colors on currently selected row
if m.Selectable && n == m.CursorPos { if m.Selectable && n == m.CursorPos {
@@ -44,7 +53,7 @@ func (m *Menu) Buffer() ui.Buffer {
} else { } else {
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor} 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++ x++
} }
} }
@@ -75,7 +84,7 @@ func (m *Menu) NavigationHandlers() {
// return width and height based on menu items // return width and height based on menu items
func calcSize(items []string) (w, h int) { func calcSize(items []string) (w, h int) {
h = len(items) + (padding * 2) h = len(items) + (y_padding * 2)
w = minWidth w = minWidth
for _, s := range items { for _, s := range items {
@@ -83,7 +92,7 @@ func calcSize(items []string) (w, h int) {
w = len(s) w = len(s)
} }
} }
w += (padding * 2) w += (x_padding * 2)
return w, h return w, h
} }