ctop/cwidgets/single/main.go

139 lines
2.3 KiB
Go
Raw Normal View History

2017-08-05 11:28:20 +00:00
package single
2017-03-06 08:51:50 +00:00
import (
"github.com/bcicen/ctop/logging"
2017-06-27 15:46:03 +00:00
"github.com/bcicen/ctop/models"
2017-03-06 08:51:50 +00:00
ui "github.com/gizak/termui"
)
var (
log = logging.Init()
sizeError = termSizeError()
colWidth = [2]int{65, 0} // left,right column width
)
2017-08-05 11:28:20 +00:00
type Single struct {
Info *Info
Net *Net
Cpu *Cpu
Mem *Mem
2017-03-12 01:35:40 +00:00
IO *IO
2018-10-05 21:21:24 +00:00
Env *Env
2017-03-12 20:53:17 +00:00
X, Y int
Width int
2017-03-06 08:51:50 +00:00
}
2017-08-05 11:28:20 +00:00
func NewSingle(id string) *Single {
if len(id) > 12 {
id = id[:12]
}
2017-08-05 11:28:20 +00:00
return &Single{
Info: NewInfo(id),
Net: NewNet(),
Cpu: NewCpu(),
Mem: NewMem(),
2017-03-12 01:35:40 +00:00
IO: NewIO(),
2018-10-05 21:21:24 +00:00
Env: NewEnv(),
Width: ui.TermWidth(),
2017-03-06 08:51:50 +00:00
}
}
2017-08-05 11:28:20 +00:00
func (e *Single) Up() {
2017-03-12 20:53:17 +00:00
if e.Y < 0 {
e.Y++
e.Align()
ui.Render(e)
}
}
2017-08-05 11:28:20 +00:00
func (e *Single) Down() {
2017-03-12 20:53:17 +00:00
if e.Y > (ui.TermHeight() - e.GetHeight()) {
e.Y--
e.Align()
ui.Render(e)
}
}
2018-10-05 21:21:24 +00:00
func (e *Single) SetWidth(w int) { e.Width = w }
func (e *Single) SetMeta(m models.Meta) {
for k, v := range m {
if k == "[ENV-VAR]" {
e.Env.Set(k, v)
} else {
e.Info.Set(k, v)
}
2018-10-05 21:21:24 +00:00
}
}
2017-03-12 20:53:17 +00:00
2017-08-05 11:28:20 +00:00
func (e *Single) SetMetrics(m models.Metrics) {
e.Cpu.Update(m.CPUUtil)
e.Net.Update(m.NetRx, m.NetTx)
e.Mem.Update(int(m.MemUsage), int(m.MemLimit))
2017-03-12 01:35:40 +00:00
e.IO.Update(m.IOBytesRead, m.IOBytesWrite)
}
// GetHeight returns total column height
2017-08-05 11:28:20 +00:00
func (e *Single) GetHeight() (h int) {
2017-03-12 20:53:17 +00:00
h += e.Info.Height
h += e.Net.Height
h += e.Cpu.Height
h += e.Mem.Height
h += e.IO.Height
2018-10-05 21:21:24 +00:00
h += e.Env.Height
2017-03-12 20:53:17 +00:00
return h
}
2017-08-05 11:28:20 +00:00
func (e *Single) Align() {
2017-03-12 20:53:17 +00:00
// reset offset if needed
if e.GetHeight() <= ui.TermHeight() {
e.Y = 0
}
y := e.Y
for _, i := range e.all() {
i.SetY(y)
y += i.GetHeight()
}
2017-03-12 20:53:17 +00:00
if e.Width > colWidth[0] {
colWidth[1] = e.Width - (colWidth[0] + 1)
}
e.Mem.Align()
log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1])
}
2017-08-05 11:28:20 +00:00
func (e *Single) Buffer() ui.Buffer {
2017-03-06 08:51:50 +00:00
buf := ui.NewBuffer()
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())
2017-03-12 01:35:40 +00:00
buf.Merge(e.IO.Buffer())
2018-10-05 21:21:24 +00:00
buf.Merge(e.Env.Buffer())
2017-03-06 08:51:50 +00:00
return buf
}
2017-08-05 11:28:20 +00:00
func (e *Single) all() []ui.GridBufferer {
return []ui.GridBufferer{
e.Info,
e.Cpu,
e.Mem,
e.Net,
2017-03-12 01:35:40 +00:00
e.IO,
2018-10-05 21:21:24 +00:00
e.Env,
}
2017-03-06 08:51:50 +00:00
}
func termSizeError() *ui.Par {
p := ui.NewPar("screen too small!")
p.Height = 1
p.Width = 20
p.Border = false
return p
2017-03-06 08:51:50 +00:00
}