mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
init expanded view, add cpu histogram
This commit is contained in:
10
container.go
10
container.go
@@ -29,8 +29,15 @@ func NewContainer(c docker.APIContainers) *Container {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) Collect(client *docker.Client) {
|
func (c *Container) Expand() {
|
||||||
|
c.widgets = widgets.NewExpanded(c.id, c.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) Collapse() {
|
||||||
|
c.widgets = widgets.NewCompact(c.id, c.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) Collect(client *docker.Client) {
|
||||||
go func() {
|
go func() {
|
||||||
opts := docker.StatsOptions{
|
opts := docker.StatsOptions{
|
||||||
ID: c.id,
|
ID: c.id,
|
||||||
@@ -49,5 +56,4 @@ func (c *Container) Collect(client *docker.Client) {
|
|||||||
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
|
c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
grid.go
18
grid.go
@@ -115,8 +115,18 @@ func (g *Grid) OpenView(v View) {
|
|||||||
v(g)
|
v(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Grid) ExpandView() {
|
||||||
|
ResetView()
|
||||||
|
defer ResetView()
|
||||||
|
container := g.containerMap.Get(g.cursorID)
|
||||||
|
container.Expand()
|
||||||
|
container.widgets.Render()
|
||||||
|
container.Collapse()
|
||||||
|
}
|
||||||
|
|
||||||
func Display(g *Grid) bool {
|
func Display(g *Grid) bool {
|
||||||
var newView View
|
var newView View
|
||||||
|
var expand bool
|
||||||
|
|
||||||
// calculate layout
|
// calculate layout
|
||||||
ui.Body.Align()
|
ui.Body.Align()
|
||||||
@@ -129,6 +139,10 @@ func Display(g *Grid) bool {
|
|||||||
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
||||||
g.cursorDown()
|
g.cursorDown()
|
||||||
})
|
})
|
||||||
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
|
expand = true
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
ui.Handle("/sys/kbd/h", func(ui.Event) {
|
ui.Handle("/sys/kbd/h", func(ui.Event) {
|
||||||
newView = HelpMenu
|
newView = HelpMenu
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
@@ -157,5 +171,9 @@ func Display(g *Grid) bool {
|
|||||||
g.OpenView(newView)
|
g.OpenView(newView)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if expand {
|
||||||
|
g.ExpandView()
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
type ContainerWidgets interface {
|
type ContainerWidgets interface {
|
||||||
Row() *ui.Row
|
Row() *ui.Row
|
||||||
|
Render()
|
||||||
Highlight()
|
Highlight()
|
||||||
UnHighlight()
|
UnHighlight()
|
||||||
SetCPU(int)
|
SetCPU(int)
|
||||||
@@ -34,6 +35,9 @@ func NewCompact(id string, name string) *Compact {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Compact) Render() {
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Compact) Row() *ui.Row {
|
func (w *Compact) Row() *ui.Row {
|
||||||
return ui.NewRow(
|
return ui.NewRow(
|
||||||
ui.NewCol(2, 0, w.Name),
|
ui.NewCol(2, 0, w.Name),
|
||||||
|
|||||||
80
widgets/expanded.go
Normal file
80
widgets/expanded.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package widgets
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
ui "github.com/gizak/termui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Expanded struct {
|
||||||
|
Info *ui.Table
|
||||||
|
Net *ui.Par
|
||||||
|
Cpu *ExpandedCpu
|
||||||
|
Memory *ui.Gauge
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExpanded(id, name string) *Expanded {
|
||||||
|
return &Expanded{
|
||||||
|
Info: NewInfo(id, name),
|
||||||
|
Net: ui.NewPar("-"),
|
||||||
|
Cpu: NewExpandedCpu(),
|
||||||
|
Memory: mkGauge(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInfo(id, name string) *ui.Table {
|
||||||
|
p := ui.NewTable()
|
||||||
|
p.Rows = [][]string{
|
||||||
|
[]string{"name", name},
|
||||||
|
[]string{"id", id},
|
||||||
|
}
|
||||||
|
p.Height = 4
|
||||||
|
p.Width = 40
|
||||||
|
p.FgColor = ui.ColorWhite
|
||||||
|
p.Seperator = false
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) Render() {
|
||||||
|
ui.Render(w.Info, w.Cpu)
|
||||||
|
ui.Handle("/timer/1s", func(ui.Event) {
|
||||||
|
ui.Render(w.Info, w.Cpu)
|
||||||
|
})
|
||||||
|
ui.Handle("/sys/kbd/", func(ui.Event) {
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
|
ui.Loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) Row() *ui.Row {
|
||||||
|
return ui.NewRow(
|
||||||
|
ui.NewCol(2, 0, w.Cpu),
|
||||||
|
ui.NewCol(2, 0, w.Memory),
|
||||||
|
ui.NewCol(2, 0, w.Net),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) Highlight() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) UnHighlight() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) SetCPU(val int) {
|
||||||
|
w.Cpu.Update(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) SetNet(rx int64, tx int64) {
|
||||||
|
w.Net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Expanded) SetMem(val int64, limit int64, percent int) {
|
||||||
|
w.Memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
||||||
|
if percent < 5 {
|
||||||
|
percent = 5
|
||||||
|
w.Memory.BarColor = ui.ColorBlack
|
||||||
|
} else {
|
||||||
|
w.Memory.BarColor = ui.ColorGreen
|
||||||
|
}
|
||||||
|
w.Memory.Percent = percent
|
||||||
|
}
|
||||||
30
widgets/expanded_cpu.go
Normal file
30
widgets/expanded_cpu.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package widgets
|
||||||
|
|
||||||
|
import (
|
||||||
|
ui "github.com/gizak/termui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExpandedCpu struct {
|
||||||
|
*ui.BarChart
|
||||||
|
hist HistData
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExpandedCpu() *ExpandedCpu {
|
||||||
|
cpu := &ExpandedCpu{ui.NewBarChart(), NewHistData(12)}
|
||||||
|
cpu.BorderLabel = "CPU Util"
|
||||||
|
cpu.Height = 10
|
||||||
|
cpu.Width = 50
|
||||||
|
cpu.BarColor = ui.ColorGreen
|
||||||
|
cpu.BarWidth = 3
|
||||||
|
cpu.BarGap = 1
|
||||||
|
cpu.X = 0
|
||||||
|
cpu.Y = 4
|
||||||
|
cpu.Data = cpu.hist.data
|
||||||
|
cpu.DataLabels = cpu.hist.labels
|
||||||
|
return cpu
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ExpandedCpu) Update(val int) {
|
||||||
|
w.hist.Append(val)
|
||||||
|
w.Data = w.hist.data
|
||||||
|
}
|
||||||
22
widgets/hist.go
Normal file
22
widgets/hist.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package widgets
|
||||||
|
|
||||||
|
type HistData struct {
|
||||||
|
data []int
|
||||||
|
labels []string
|
||||||
|
maxSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHistData(max int) HistData {
|
||||||
|
return HistData{
|
||||||
|
data: make([]int, max),
|
||||||
|
labels: make([]string, max),
|
||||||
|
maxSize: max,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h HistData) Append(val int) {
|
||||||
|
if len(h.data) >= h.maxSize {
|
||||||
|
h.data = append(h.data[:0], h.data[1:]...)
|
||||||
|
}
|
||||||
|
h.data = append(h.data, val)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user