1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

162 lines
3.5 KiB
Go

package resourceusage
import (
"fmt"
"math"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/rivo/tview"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
"github.com/wtfutil/wtf/view"
)
// Widget define wtf widget to register widget later
type Widget struct {
settings *Settings
tviewApp *tview.Application
view.BarGraph
}
// NewWidget Make new instance of widget
func NewWidget(tviewApp *tview.Application, settings *Settings) *Widget {
widget := Widget{
BarGraph: view.NewBarGraph(tviewApp, settings.Name, settings.Common),
tviewApp: tviewApp,
settings: settings,
}
widget.View.SetWrap(false)
widget.View.SetWordWrap(false)
return &widget
}
/* -------------------- Exported Functions -------------------- */
// MakeGraph - Load the dead drop stats
func MakeGraph(widget *Widget) {
cpuStats, memInfo := getDataFromSystem(widget)
var itemsCount = 0
if widget.settings.showCPU {
itemsCount += len(cpuStats)
}
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 {
// 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]
}
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)
}
usedSwapLabel := bytefmt.ByteSize(swapUsed)
totalSwapLabel := bytefmt.ByteSize(memInfo.SwapTotal)
if usedSwapLabel[len(usedSwapLabel)-1] == totalSwapLabel[len(totalSwapLabel)-1] {
usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1]
}
stats[nextIndex] = view.Bar{
Label: "Swp",
Percent: int(swapPercent * 100),
ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel),
LabelColor: "yellow",
}
}
widget.BarGraph.BuildBars(stats)
}
// Refresh & update after interval time
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.tviewApp.QueueUpdateDraw(func() {
widget.View.Clear()
display(widget)
})
}
/* -------------------- Unexported Functions -------------------- */
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
}