2017-08-05 11:28:20 +00:00
|
|
|
package single
|
2017-03-06 08:51:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-06 22:05:04 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2017-03-06 22:05:04 +00:00
|
|
|
var (
|
|
|
|
log = logging.Init()
|
|
|
|
sizeError = termSizeError()
|
2017-03-06 23:58:04 +00:00
|
|
|
colWidth = [2]int{65, 0} // left,right column width
|
2017-03-06 22:05:04 +00:00
|
|
|
)
|
|
|
|
|
2017-08-05 11:28:20 +00:00
|
|
|
type Single struct {
|
2017-03-06 22:05:04 +00:00
|
|
|
Info *Info
|
2017-03-06 23:58:04 +00:00
|
|
|
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
|
2017-03-06 22:05:04 +00:00
|
|
|
Width int
|
2017-03-06 08:51:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 11:28:20 +00:00
|
|
|
func NewSingle(id string) *Single {
|
2017-03-06 22:05:04 +00:00
|
|
|
if len(id) > 12 {
|
|
|
|
id = id[:12]
|
|
|
|
}
|
2017-08-05 11:28:20 +00:00
|
|
|
return &Single{
|
2017-03-06 22:05:04 +00:00
|
|
|
Info: NewInfo(id),
|
2017-03-06 23:58:04 +00:00
|
|
|
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(),
|
2017-03-06 22:05:04 +00:00
|
|
|
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-03-06 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2017-03-06 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:21:24 +00:00
|
|
|
func (e *Single) SetWidth(w int) { e.Width = w }
|
2019-06-08 21:34:43 +00:00
|
|
|
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) {
|
2017-03-06 22:05:04 +00:00
|
|
|
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)
|
2017-03-06 22:05:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:33:29 +00:00
|
|
|
// 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
|
2017-03-06 22:05:04 +00:00
|
|
|
for _, i := range e.all() {
|
|
|
|
i.SetY(y)
|
|
|
|
y += i.GetHeight()
|
|
|
|
}
|
2017-03-12 20:53:17 +00:00
|
|
|
|
2017-03-06 22:05:04 +00:00
|
|
|
if e.Width > colWidth[0] {
|
|
|
|
colWidth[1] = e.Width - (colWidth[0] + 1)
|
|
|
|
}
|
2017-03-06 23:58:04 +00:00
|
|
|
e.Mem.Align()
|
2017-03-06 22:05:04 +00:00
|
|
|
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()
|
2017-03-06 22:05:04 +00:00
|
|
|
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 {
|
2017-03-06 22:05:04 +00:00
|
|
|
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 22:05:04 +00:00
|
|
|
}
|
2017-03-06 08:51:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 22:05:04 +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
|
|
|
}
|