mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
ResourceUsage Widget @ Support to enable(default)/disable display of CPU, mem and swap
This commit is contained in:
parent
42a1720620
commit
62f4a17459
@ -13,12 +13,18 @@ const (
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
cpuCombined bool
|
||||
showCPU bool
|
||||
showMem bool
|
||||
showSwp bool
|
||||
}
|
||||
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||
cpuCombined: ymlConfig.UBool("cpuCombined", false),
|
||||
showCPU: ymlConfig.UBool("showCPU", true),
|
||||
showMem: ymlConfig.UBool("showMem", true),
|
||||
showSwp: ymlConfig.UBool("showSwp", true),
|
||||
}
|
||||
|
||||
return &settings
|
||||
|
@ -39,76 +39,86 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
|
||||
// MakeGraph - Load the dead drop stats
|
||||
func MakeGraph(widget *Widget) {
|
||||
cpuStats, memInfo := getDataFromSystem(widget)
|
||||
|
||||
cpuStats, err := cpu.Percent(time.Duration(0), !widget.settings.cpuCombined)
|
||||
if err != nil {
|
||||
return
|
||||
var itemsCount = 0
|
||||
if widget.settings.showCPU {
|
||||
itemsCount += len(cpuStats)
|
||||
}
|
||||
|
||||
var stats = make([]view.Bar, len(cpuStats)+2)
|
||||
if widget.settings.showMem {
|
||||
itemsCount++
|
||||
}
|
||||
|
||||
for i, stat := range cpuStats {
|
||||
// Stats sometimes jump outside the 0-100 range, possibly due to timing
|
||||
stat = math.Min(100, stat)
|
||||
stat = math.Max(0, stat)
|
||||
if widget.settings.showSwp {
|
||||
itemsCount++
|
||||
}
|
||||
|
||||
var label string
|
||||
if widget.settings.cpuCombined {
|
||||
label = "CPU"
|
||||
} else {
|
||||
label = fmt.Sprint(i)
|
||||
var stats = make([]view.Bar, itemsCount)
|
||||
var nextIndex = 0
|
||||
|
||||
if widget.settings.showCPU && len(cpuStats) > 0 {
|
||||
for i, stat := range cpuStats {
|
||||
// Stats sometimes jump outside the 0-100 range, possibly due to timing
|
||||
stat = math.Min(100, stat)
|
||||
stat = math.Max(0, stat)
|
||||
|
||||
var label string
|
||||
if widget.settings.cpuCombined {
|
||||
label = "CPU"
|
||||
} else {
|
||||
label = fmt.Sprint(i)
|
||||
}
|
||||
|
||||
bar := view.Bar{
|
||||
Label: label,
|
||||
Percent: int(stat),
|
||||
ValueLabel: fmt.Sprintf("%d%%", int(stat)),
|
||||
LabelColor: "red",
|
||||
}
|
||||
|
||||
stats[nextIndex] = bar
|
||||
nextIndex++
|
||||
}
|
||||
}
|
||||
|
||||
if widget.settings.showMem {
|
||||
usedMemLabel := bytefmt.ByteSize(memInfo.Used)
|
||||
totalMemLabel := bytefmt.ByteSize(memInfo.Total)
|
||||
|
||||
if usedMemLabel[len(usedMemLabel)-1] == totalMemLabel[len(totalMemLabel)-1] {
|
||||
usedMemLabel = usedMemLabel[:len(usedMemLabel)-1]
|
||||
}
|
||||
|
||||
bar := view.Bar{
|
||||
Label: label,
|
||||
Percent: int(stat),
|
||||
ValueLabel: fmt.Sprintf("%d%%", int(stat)),
|
||||
LabelColor: "red",
|
||||
stats[nextIndex] = view.Bar{
|
||||
Label: "Mem",
|
||||
Percent: int(memInfo.UsedPercent),
|
||||
ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel),
|
||||
LabelColor: "green",
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
|
||||
if widget.settings.showSwp {
|
||||
swapUsed := memInfo.SwapTotal - memInfo.SwapFree
|
||||
var swapPercent float64
|
||||
if memInfo.SwapTotal > 0 {
|
||||
swapPercent = float64(swapUsed) / float64(memInfo.SwapTotal)
|
||||
}
|
||||
|
||||
stats[i] = bar
|
||||
}
|
||||
usedSwapLabel := bytefmt.ByteSize(swapUsed)
|
||||
totalSwapLabel := bytefmt.ByteSize(memInfo.SwapTotal)
|
||||
|
||||
memInfo, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if usedSwapLabel[len(usedSwapLabel)-1] == totalSwapLabel[len(totalSwapLabel)-1] {
|
||||
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
|
||||
}
|
||||
|
||||
memIndex := len(cpuStats)
|
||||
|
||||
usedMemLabel := bytefmt.ByteSize(memInfo.Used)
|
||||
totalMemLabel := bytefmt.ByteSize(memInfo.Total)
|
||||
|
||||
if usedMemLabel[len(usedMemLabel)-1] == totalMemLabel[len(totalMemLabel)-1] {
|
||||
usedMemLabel = usedMemLabel[:len(usedMemLabel)-1]
|
||||
}
|
||||
|
||||
stats[memIndex] = view.Bar{
|
||||
Label: "Mem",
|
||||
Percent: int(memInfo.UsedPercent),
|
||||
ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel),
|
||||
LabelColor: "green",
|
||||
}
|
||||
|
||||
swapIndex := len(cpuStats) + 1
|
||||
swapUsed := memInfo.SwapTotal - memInfo.SwapFree
|
||||
var swapPercent float64
|
||||
if memInfo.SwapTotal > 0 {
|
||||
swapPercent = float64(swapUsed) / float64(memInfo.SwapTotal)
|
||||
}
|
||||
|
||||
usedSwapLabel := bytefmt.ByteSize(swapUsed)
|
||||
totalSwapLabel := bytefmt.ByteSize(memInfo.SwapTotal)
|
||||
|
||||
if usedSwapLabel[len(usedSwapLabel)-1] == totalSwapLabel[len(totalSwapLabel)-1] {
|
||||
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
|
||||
}
|
||||
|
||||
stats[swapIndex] = view.Bar{
|
||||
Label: "Swp",
|
||||
Percent: int(swapPercent * 100),
|
||||
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
|
||||
LabelColor: "yellow",
|
||||
stats[nextIndex] = view.Bar{
|
||||
Label: "Swp",
|
||||
Percent: int(swapPercent * 100),
|
||||
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
|
||||
LabelColor: "yellow",
|
||||
}
|
||||
}
|
||||
|
||||
widget.BarGraph.BuildBars(stats[:])
|
||||
@ -132,3 +142,21 @@ func (widget *Widget) Refresh() {
|
||||
func display(widget *Widget) {
|
||||
MakeGraph(widget)
|
||||
}
|
||||
|
||||
func getDataFromSystem(widget *Widget) (cpuStats []float64, memInfo mem.VirtualMemoryStat) {
|
||||
if widget.settings.showCPU {
|
||||
rCPUStats, err := cpu.Percent(time.Duration(0), !widget.settings.cpuCombined)
|
||||
if err == nil {
|
||||
cpuStats = rCPUStats
|
||||
}
|
||||
}
|
||||
|
||||
if widget.settings.showMem || widget.settings.showSwp {
|
||||
rMemInfo, err := mem.VirtualMemory()
|
||||
if err == nil {
|
||||
memInfo = *rMemInfo
|
||||
}
|
||||
}
|
||||
|
||||
return cpuStats, memInfo
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user