From 62f4a17459ec50e843f42f3fec6d4f467c452123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Let=C3=A9rio?= Date: Thu, 5 Mar 2020 01:19:40 -0300 Subject: [PATCH] ResourceUsage Widget @ Support to enable(default)/disable display of CPU, mem and swap --- modules/resourceusage/settings.go | 6 ++ modules/resourceusage/widget.go | 146 ++++++++++++++++++------------ 2 files changed, 93 insertions(+), 59 deletions(-) diff --git a/modules/resourceusage/settings.go b/modules/resourceusage/settings.go index e4af68d8..a524194c 100644 --- a/modules/resourceusage/settings.go +++ b/modules/resourceusage/settings.go @@ -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 diff --git a/modules/resourceusage/widget.go b/modules/resourceusage/widget.go index 9e8c87e6..5fdbef28 100644 --- a/modules/resourceusage/widget.go +++ b/modules/resourceusage/widget.go @@ -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 +}