1
0
mirror of https://github.com/bcicen/ctop.git synced 2024-08-30 18:23:19 +00:00

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

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

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

24
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()
})

@ -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/<enter>", func(ui.Event) {

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

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