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,14 +39,25 @@ 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++
|
||||||
|
}
|
||||||
|
|
||||||
|
if widget.settings.showSwp {
|
||||||
|
itemsCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
var stats = make([]view.Bar, itemsCount)
|
||||||
|
var nextIndex = 0
|
||||||
|
|
||||||
|
if widget.settings.showCPU && len(cpuStats) > 0 {
|
||||||
for i, stat := range cpuStats {
|
for i, stat := range cpuStats {
|
||||||
// Stats sometimes jump outside the 0-100 range, possibly due to timing
|
// Stats sometimes jump outside the 0-100 range, possibly due to timing
|
||||||
stat = math.Min(100, stat)
|
stat = math.Min(100, stat)
|
||||||
@ -66,16 +77,12 @@ func MakeGraph(widget *Widget) {
|
|||||||
LabelColor: "red",
|
LabelColor: "red",
|
||||||
}
|
}
|
||||||
|
|
||||||
stats[i] = bar
|
stats[nextIndex] = bar
|
||||||
|
nextIndex++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memInfo, err := mem.VirtualMemory()
|
if widget.settings.showMem {
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
memIndex := len(cpuStats)
|
|
||||||
|
|
||||||
usedMemLabel := bytefmt.ByteSize(memInfo.Used)
|
usedMemLabel := bytefmt.ByteSize(memInfo.Used)
|
||||||
totalMemLabel := bytefmt.ByteSize(memInfo.Total)
|
totalMemLabel := bytefmt.ByteSize(memInfo.Total)
|
||||||
|
|
||||||
@ -83,14 +90,16 @@ func MakeGraph(widget *Widget) {
|
|||||||
usedMemLabel = usedMemLabel[:len(usedMemLabel)-1]
|
usedMemLabel = usedMemLabel[:len(usedMemLabel)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
stats[memIndex] = view.Bar{
|
stats[nextIndex] = view.Bar{
|
||||||
Label: "Mem",
|
Label: "Mem",
|
||||||
Percent: int(memInfo.UsedPercent),
|
Percent: int(memInfo.UsedPercent),
|
||||||
ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel),
|
ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel),
|
||||||
LabelColor: "green",
|
LabelColor: "green",
|
||||||
}
|
}
|
||||||
|
nextIndex++
|
||||||
|
}
|
||||||
|
|
||||||
swapIndex := len(cpuStats) + 1
|
if widget.settings.showSwp {
|
||||||
swapUsed := memInfo.SwapTotal - memInfo.SwapFree
|
swapUsed := memInfo.SwapTotal - memInfo.SwapFree
|
||||||
var swapPercent float64
|
var swapPercent float64
|
||||||
if memInfo.SwapTotal > 0 {
|
if memInfo.SwapTotal > 0 {
|
||||||
@ -104,12 +113,13 @@ func MakeGraph(widget *Widget) {
|
|||||||
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
|
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
stats[swapIndex] = view.Bar{
|
stats[nextIndex] = view.Bar{
|
||||||
Label: "Swp",
|
Label: "Swp",
|
||||||
Percent: int(swapPercent * 100),
|
Percent: int(swapPercent * 100),
|
||||||
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
|
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
|
||||||
LabelColor: "yellow",
|
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