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.BorderLabel = "CPU"
cpu.Height = 10
cpu.Width = 50
cpu.Width = colWidth[0]
cpu.X = 0
cpu.Y = 4
cpu.Y = 6
cpu.Data = cpu.hist.Data
cpu.DataLabels = cpu.hist.Labels
cpu.AxesColor = ui.ColorDefault

View File

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

View File

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

View File

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

View File

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

View File

@ -66,6 +66,8 @@ func (cm *DockerContainerSource) refresh(c *Container) {
return
}
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)
}

14
grid.go
View File

@ -52,12 +52,18 @@ func ExpandView(c *Container) {
ui.DefaultEvtStream.ResetHandlers()
defer ui.DefaultEvtStream.ResetHandlers()
expandWidgets := expanded.NewExpanded(c.Id)
c.SetUpdater(expandWidgets)
ex := expanded.NewExpanded(c.Id)
c.SetUpdater(ex)
ui.Render(expandWidgets)
ex.Align()
ui.Render(ex)
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.StopLoop()