diff --git a/cwidgets/expanded/cpu.go b/cwidgets/expanded/cpu.go index a5c1491..4ad4a26 100644 --- a/cwidgets/expanded/cpu.go +++ b/cwidgets/expanded/cpu.go @@ -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 diff --git a/cwidgets/expanded/info.go b/cwidgets/expanded/info.go index 5c918f1..98ae6a3 100644 --- a/cwidgets/expanded/info.go +++ b/cwidgets/expanded/info.go @@ -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 { - w.Rows = append(w.Rows, []string{k, v}) + 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 } diff --git a/cwidgets/expanded/main.go b/cwidgets/expanded/main.go index 0eadbb4..02d67f3 100644 --- a/cwidgets/expanded/main.go +++ b/cwidgets/expanded/main.go @@ -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 + Info *Info + Net *ExpandedNet + Cpu *ExpandedCpu + Mem *ExpandedMem + 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(), + 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 } diff --git a/cwidgets/expanded/mem.go b/cwidgets/expanded/mem.go index 3b2a755..b86ecb4 100644 --- a/cwidgets/expanded/mem.go +++ b/cwidgets/expanded/mem.go @@ -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 diff --git a/cwidgets/expanded/net.go b/cwidgets/expanded/net.go index 7213e0b..2faf478 100644 --- a/cwidgets/expanded/net.go +++ b/cwidgets/expanded/net.go @@ -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 diff --git a/dockersource.go b/dockersource.go index 9ebcb37..1c6eb51 100644 --- a/dockersource.go +++ b/dockersource.go @@ -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) } diff --git a/grid.go b/grid.go index 880f81d..a4a426f 100644 --- a/grid.go +++ b/grid.go @@ -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()