mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
add displayitems override, padding updates to menu
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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))
|
||||||
|
|||||||
24
grid.go
24
grid.go
@@ -8,20 +8,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
cursorID string // id of currently selected container
|
cursorID string // id of currently selected container
|
||||||
containers []*Container // sorted slice of containers
|
cmap *ContainerMap
|
||||||
containerMap *ContainerMap
|
containers []*Container // sorted slice of containers
|
||||||
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,
|
||||||
containers: containers,
|
cmap: cmap,
|
||||||
containerMap: containerMap,
|
containers: containers,
|
||||||
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()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
9
menus.go
9
menus.go
@@ -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) {
|
||||||
|
|||||||
6
sort.go
6
sort.go
@@ -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 }
|
||||||
|
|||||||
@@ -5,27 +5,30 @@ 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
|
||||||
TextFgColor ui.Attribute
|
DisplayItems []string
|
||||||
TextBgColor ui.Attribute
|
TextFgColor ui.Attribute
|
||||||
Selectable bool
|
TextBgColor ui.Attribute
|
||||||
CursorPos int
|
Selectable bool
|
||||||
|
CursorPos int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMenu(items []string) *Menu {
|
func NewMenu(items []string) *Menu {
|
||||||
m := &Menu{
|
m := &Menu{
|
||||||
Block: *ui.NewBlock(),
|
Block: *ui.NewBlock(),
|
||||||
Items: items,
|
Items: items,
|
||||||
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
DisplayItems: []string{},
|
||||||
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
||||||
Selectable: false,
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
||||||
CursorPos: 0,
|
Selectable: false,
|
||||||
|
CursorPos: 0,
|
||||||
}
|
}
|
||||||
m.Width, m.Height = calcSize(items)
|
m.Width, m.Height = calcSize(items)
|
||||||
return m
|
return m
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user