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 {
|
type Settings struct {
|
||||||
common *cfg.Common
|
common *cfg.Common
|
||||||
cpuCombined bool
|
cpuCombined bool
|
||||||
|
showCPU bool
|
||||||
|
showMem bool
|
||||||
|
showSwp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||||
settings := Settings{
|
settings := Settings{
|
||||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||||
cpuCombined: ymlConfig.UBool("cpuCombined", false),
|
cpuCombined: ymlConfig.UBool("cpuCombined", false),
|
||||||
|
showCPU: ymlConfig.UBool("showCPU", true),
|
||||||
|
showMem: ymlConfig.UBool("showMem", true),
|
||||||
|
showSwp: ymlConfig.UBool("showSwp", true),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &settings
|
return &settings
|
||||||
|
@ -39,76 +39,86 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
|||||||
|
|
||||||
// MakeGraph - Load the dead drop stats
|
// MakeGraph - Load the dead drop stats
|
||||||
func MakeGraph(widget *Widget) {
|
func MakeGraph(widget *Widget) {
|
||||||
|
cpuStats, memInfo := getDataFromSystem(widget)
|
||||||
|
|
||||||
cpuStats, err := cpu.Percent(time.Duration(0), !widget.settings.cpuCombined)
|
var itemsCount = 0
|
||||||
if err != nil {
|
if widget.settings.showCPU {
|
||||||
return
|
itemsCount += len(cpuStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
var stats = make([]view.Bar, len(cpuStats)+2)
|
if widget.settings.showMem {
|
||||||
|
itemsCount++
|
||||||
|
}
|
||||||
|
|
||||||
for i, stat := range cpuStats {
|
if widget.settings.showSwp {
|
||||||
// Stats sometimes jump outside the 0-100 range, possibly due to timing
|
itemsCount++
|
||||||
stat = math.Min(100, stat)
|
}
|
||||||
stat = math.Max(0, stat)
|
|
||||||
|
|
||||||
var label string
|
var stats = make([]view.Bar, itemsCount)
|
||||||
if widget.settings.cpuCombined {
|
var nextIndex = 0
|
||||||
label = "CPU"
|
|
||||||
} else {
|
if widget.settings.showCPU && len(cpuStats) > 0 {
|
||||||
label = fmt.Sprint(i)
|
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{
|
stats[nextIndex] = view.Bar{
|
||||||
Label: label,
|
Label: "Mem",
|
||||||
Percent: int(stat),
|
Percent: int(memInfo.UsedPercent),
|
||||||
ValueLabel: fmt.Sprintf("%d%%", int(stat)),
|
ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel),
|
||||||
LabelColor: "red",
|
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 usedSwapLabel[len(usedSwapLabel)-1] == totalSwapLabel[len(totalSwapLabel)-1] {
|
||||||
if err != nil {
|
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
memIndex := len(cpuStats)
|
stats[nextIndex] = view.Bar{
|
||||||
|
Label: "Swp",
|
||||||
usedMemLabel := bytefmt.ByteSize(memInfo.Used)
|
Percent: int(swapPercent * 100),
|
||||||
totalMemLabel := bytefmt.ByteSize(memInfo.Total)
|
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
|
||||||
|
LabelColor: "yellow",
|
||||||
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",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.BarGraph.BuildBars(stats[:])
|
widget.BarGraph.BuildBars(stats[:])
|
||||||
@ -132,3 +142,21 @@ func (widget *Widget) Refresh() {
|
|||||||
func display(widget *Widget) {
|
func display(widget *Widget) {
|
||||||
MakeGraph(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