mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
refactor compact widgets into subpackage
This commit is contained in:
63
widgets/compact.go
Normal file
63
widgets/compact.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
type Compact struct {
|
||||
Cid *ui.Par
|
||||
Net *ui.Par
|
||||
Name *ui.Par
|
||||
Cpu *ui.Gauge
|
||||
Memory *ui.Gauge
|
||||
}
|
||||
|
||||
func NewCompact(id string, name string) *Compact {
|
||||
return &Compact{
|
||||
Cid: compactPar(id),
|
||||
Net: compactPar("-"),
|
||||
Name: compactPar(name),
|
||||
Cpu: mkGauge(),
|
||||
Memory: mkGauge(),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Compact) Row() *ui.Row {
|
||||
return ui.NewRow(
|
||||
ui.NewCol(2, 0, w.Name),
|
||||
ui.NewCol(2, 0, w.Cid),
|
||||
ui.NewCol(2, 0, w.Cpu),
|
||||
ui.NewCol(2, 0, w.Memory),
|
||||
ui.NewCol(2, 0, w.Net),
|
||||
)
|
||||
}
|
||||
|
||||
func (w *Compact) SetCPU(val float64) {
|
||||
intVal := round(val)
|
||||
w.Cpu.BarColor = colorScale(intVal)
|
||||
w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(intVal))
|
||||
if intVal < 5 {
|
||||
intVal = 5
|
||||
w.Cpu.BarColor = ui.ColorBlack
|
||||
}
|
||||
w.Cpu.Percent = intVal
|
||||
}
|
||||
|
||||
func (w *Compact) SetNet(rx int64, tx int64) {
|
||||
w.Net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
|
||||
}
|
||||
|
||||
func (w *Compact) SetMem(val int64, limit int64) {
|
||||
percent := round((float64(val) / float64(limit)) * 100)
|
||||
w.Memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
||||
if percent < 5 {
|
||||
percent = 5
|
||||
w.Memory.BarColor = ui.ColorBlack
|
||||
} else {
|
||||
w.Memory.BarColor = ui.ColorGreen
|
||||
}
|
||||
w.Memory.Percent = percent
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
type CPU struct {
|
||||
*ui.Gauge
|
||||
}
|
||||
|
||||
func NewCPU() *CPU {
|
||||
return &CPU{mkGauge()}
|
||||
}
|
||||
|
||||
func (c *CPU) Set(val int) {
|
||||
c.BarColor = colorScale(val)
|
||||
c.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
||||
if val < 5 {
|
||||
val = 5
|
||||
c.BarColor = ui.ColorBlack
|
||||
}
|
||||
c.Percent = val
|
||||
}
|
||||
@@ -1,9 +1,48 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
const (
|
||||
kb = 1024
|
||||
mb = kb * 1024
|
||||
gb = mb * 1024
|
||||
)
|
||||
|
||||
func byteFormat(n int64) string {
|
||||
if n < kb {
|
||||
return fmt.Sprintf("%sB", strconv.FormatInt(n, 10))
|
||||
}
|
||||
if n < mb {
|
||||
n = n / kb
|
||||
return fmt.Sprintf("%sK", strconv.FormatInt(n, 10))
|
||||
}
|
||||
if n < gb {
|
||||
n = n / mb
|
||||
return fmt.Sprintf("%sM", strconv.FormatInt(n, 10))
|
||||
}
|
||||
n = n / gb
|
||||
return fmt.Sprintf("%sG", strconv.FormatInt(n, 10))
|
||||
}
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
func compactPar(s string) *ui.Par {
|
||||
p := ui.NewPar(s)
|
||||
p.Border = false
|
||||
p.Height = 1
|
||||
p.Width = 20
|
||||
p.TextFgColor = ui.ColorWhite
|
||||
return p
|
||||
}
|
||||
|
||||
func mkGauge() *ui.Gauge {
|
||||
g := ui.NewGauge()
|
||||
g.Height = 1
|
||||
|
||||
Reference in New Issue
Block a user