add resize handlers, screen width error to expandedWidgets

This commit is contained in:
Bradley Cicenas 2017-03-06 22:05:04 +00:00
parent 2ee0ae7fcb
commit d94aed1531
7 changed files with 98 additions and 33 deletions

View File

@ -13,9 +13,9 @@ func NewExpandedCpu() *ExpandedCpu {
cpu := &ExpandedCpu{ui.NewLineChart(), NewFloatHist(60)} cpu := &ExpandedCpu{ui.NewLineChart(), NewFloatHist(60)}
cpu.BorderLabel = "CPU" cpu.BorderLabel = "CPU"
cpu.Height = 10 cpu.Height = 10
cpu.Width = 50 cpu.Width = colWidth[0]
cpu.X = 0 cpu.X = 0
cpu.Y = 4 cpu.Y = 6
cpu.Data = cpu.hist.Data cpu.Data = cpu.hist.Data
cpu.DataLabels = cpu.hist.Labels cpu.DataLabels = cpu.hist.Labels
cpu.AxesColor = ui.ColorDefault cpu.AxesColor = ui.ColorDefault

View File

@ -4,6 +4,8 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
var displayInfo = []string{"id", "name", "image", "state"}
type Info struct { type Info struct {
*ui.Table *ui.Table
data map[string]string data map[string]string
@ -12,11 +14,11 @@ type Info struct {
func NewInfo(id string) *Info { func NewInfo(id string) *Info {
p := ui.NewTable() p := ui.NewTable()
p.Height = 4 p.Height = 4
p.Width = 50 p.Width = colWidth[0]
p.FgColor = ui.ColorWhite p.FgColor = ui.ColorWhite
p.Seperator = false p.Seperator = false
i := &Info{p, make(map[string]string)} i := &Info{p, make(map[string]string)}
i.Set("ID", id) i.Set("id", id)
return i return i
} }
@ -24,7 +26,10 @@ func (w *Info) Set(k, v string) {
w.data[k] = v w.data[k] = v
// rebuild rows // rebuild rows
w.Rows = [][]string{} w.Rows = [][]string{}
for k, v := range w.data { for _, k := range displayInfo {
if v, ok := w.data[k]; ok {
w.Rows = append(w.Rows, []string{k, v}) w.Rows = append(w.Rows, []string{k, v})
} }
}
w.Height = len(w.Rows) + 2
} }

View File

@ -1,42 +1,94 @@
package expanded package expanded
import ( import (
"github.com/bcicen/ctop/logging"
"github.com/bcicen/ctop/metrics" "github.com/bcicen/ctop/metrics"
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
var (
log = logging.Init()
sizeError = termSizeError()
colWidth = [2]int{60, 0} // left,right column width
)
type Expanded struct { type Expanded struct {
Info *Info Info *Info
Net *ExpandedNet Net *ExpandedNet
Cpu *ExpandedCpu Cpu *ExpandedCpu
Mem *ExpandedMem Mem *ExpandedMem
infoMap map[string]string Width int
} }
func NewExpanded(id string) *Expanded { func NewExpanded(id string) *Expanded {
if len(id) > 12 {
id = id[:12]
}
return &Expanded{ return &Expanded{
Info: NewInfo(id), Info: NewInfo(id),
Net: NewExpandedNet(), Net: NewExpandedNet(),
Cpu: NewExpandedCpu(), Cpu: NewExpandedCpu(),
Mem: NewExpandedMem(), Mem: NewExpandedMem(),
Width: ui.TermWidth(),
} }
} }
func (w *Expanded) Buffer() ui.Buffer { func (e *Expanded) SetWidth(w int) {
e.Width = w
}
func (e *Expanded) SetMeta(k, v string) {
e.Info.Set(k, v)
}
func (e *Expanded) SetMetrics(m metrics.Metrics) {
e.Cpu.Update(m.CPUUtil)
e.Net.Update(m.NetRx, m.NetTx)
e.Mem.Update(int(m.MemUsage), int(m.MemLimit))
}
func (e *Expanded) Align() {
y := 0
for _, i := range e.all() {
i.SetY(y)
y += i.GetHeight()
}
if e.Width > colWidth[0] {
colWidth[1] = e.Width - (colWidth[0] + 1)
}
log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1])
}
func calcWidth(w int) {
}
func (e *Expanded) Buffer() ui.Buffer {
buf := ui.NewBuffer() buf := ui.NewBuffer()
buf.Merge(w.Info.Buffer()) if e.Width < (colWidth[0] + colWidth[1]) {
buf.Merge(w.Cpu.Buffer()) ui.Clear()
buf.Merge(w.Mem.Buffer()) buf.Merge(sizeError.Buffer())
buf.Merge(w.Net.Buffer()) return buf
}
buf.Merge(e.Info.Buffer())
buf.Merge(e.Cpu.Buffer())
buf.Merge(e.Mem.Buffer())
buf.Merge(e.Net.Buffer())
return buf return buf
} }
func (w *Expanded) SetMeta(k, v string) { func (e *Expanded) all() []ui.GridBufferer {
w.Info.Set(k, v) return []ui.GridBufferer{
e.Info,
e.Cpu,
e.Mem,
e.Net,
}
} }
func (w *Expanded) SetMetrics(m metrics.Metrics) { func termSizeError() *ui.Par {
w.Cpu.Update(m.CPUUtil) p := ui.NewPar("screen too small!")
w.Net.Update(m.NetRx, m.NetTx) p.Height = 1
w.Mem.Update(int(m.MemUsage), int(m.MemLimit)) p.Width = 20
p.Border = false
return p
} }

View File

@ -13,11 +13,11 @@ type ExpandedMem struct {
func NewExpandedMem() *ExpandedMem { func NewExpandedMem() *ExpandedMem {
mem := &ExpandedMem{ mem := &ExpandedMem{
ui.NewBarChart(), ui.NewBarChart(),
NewIntHist(8), NewIntHist(10),
} }
mem.BorderLabel = "MEM" mem.BorderLabel = "MEM"
mem.Height = 10 mem.Height = 10
mem.Width = 50 mem.Width = colWidth[0]
mem.BarWidth = 5 mem.BarWidth = 5
mem.BarGap = 1 mem.BarGap = 1
mem.X = 0 mem.X = 0

View File

@ -18,7 +18,7 @@ func NewExpandedNet() *ExpandedNet {
net := &ExpandedNet{ui.NewSparklines(), NewDiffHist(50), NewDiffHist(50)} net := &ExpandedNet{ui.NewSparklines(), NewDiffHist(50), NewDiffHist(50)}
net.BorderLabel = "NET" net.BorderLabel = "NET"
net.Height = 6 net.Height = 6
net.Width = 50 net.Width = colWidth[0]
net.X = 0 net.X = 0
net.Y = 24 net.Y = 24

View File

@ -66,6 +66,8 @@ func (cm *DockerContainerSource) refresh(c *Container) {
return return
} }
c.SetMeta("name", shortName(insp.Name)) c.SetMeta("name", shortName(insp.Name))
c.SetMeta("image", insp.Config.Image)
c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006"))
c.SetState(insp.State.Status) c.SetState(insp.State.Status)
} }

14
grid.go
View File

@ -52,12 +52,18 @@ func ExpandView(c *Container) {
ui.DefaultEvtStream.ResetHandlers() ui.DefaultEvtStream.ResetHandlers()
defer ui.DefaultEvtStream.ResetHandlers() defer ui.DefaultEvtStream.ResetHandlers()
expandWidgets := expanded.NewExpanded(c.Id) ex := expanded.NewExpanded(c.Id)
c.SetUpdater(expandWidgets) c.SetUpdater(ex)
ui.Render(expandWidgets) ex.Align()
ui.Render(ex)
ui.Handle("/timer/1s", func(ui.Event) { ui.Handle("/timer/1s", func(ui.Event) {
ui.Render(expandWidgets) ui.Render(ex)
})
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ex.SetWidth(ui.TermWidth())
ex.Align()
log.Infof("resize: width=%v max-rows=%v", ex.Width, maxRows())
}) })
ui.Handle("/sys/kbd/", func(ui.Event) { ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop() ui.StopLoop()