Add detailed memory usage labels to memory widget

This commit is contained in:
Caleb Bassi 2018-05-23 18:52:35 -07:00
parent ae2fff9655
commit 42a949c5aa
4 changed files with 16 additions and 6 deletions

2
Gopkg.lock generated
View File

@ -17,7 +17,7 @@
branch = "master"
name = "github.com/cjbassi/termui"
packages = ["."]
revision = "87dbce7f69558bede7c7c7a51324ce4d8cfcb87b"
revision = "9d77ad53bd1562d72b66b7ab5965e6495955846e"
[[projects]]
branch = "master"

View File

@ -67,9 +67,11 @@ func (self *CPU) update() {
key := "CPU" + strconv.Itoa(i)
percent := percents[i]
self.Data[key] = append(self.Data[key], percent)
self.Labels[key] = fmt.Sprintf("%3.0f%%", percent)
}
} else {
percent, _ := psCPU.Percent(self.interval, false)
self.Data["Average"] = append(self.Data["Average"], percent[0])
self.Labels["Average"] = fmt.Sprintf("%3.0f%%", percent[0])
}
}

View File

@ -1,8 +1,10 @@
package widgets
import (
"fmt"
"time"
"github.com/cjbassi/gotop/src/utils"
ui "github.com/cjbassi/termui"
psMem "github.com/shirou/gopsutil/mem"
)
@ -39,4 +41,11 @@ func (self *Mem) update() {
swap, _ := psMem.SwapMemory()
self.Data["Main"] = append(self.Data["Main"], main.UsedPercent)
self.Data["Swap"] = append(self.Data["Swap"], swap.UsedPercent)
mainTotalBytes, mainTotalMagnitude := utils.ConvertBytes(main.Total)
swapTotalBytes, swapTotalMagnitude := utils.ConvertBytes(swap.Total)
mainUsedBytes, mainUsedMagnitude := utils.ConvertBytes(main.Used)
swapUsedBytes, swapUsedMagnitude := utils.ConvertBytes(swap.Used)
self.Labels["Main"] = fmt.Sprintf("%3.0f%% %.0f%s/%.0f%s", main.UsedPercent, mainUsedBytes, mainUsedMagnitude, mainTotalBytes, mainTotalMagnitude)
self.Labels["Swap"] = fmt.Sprintf("%3.0f%% %.0f%s/%.0f%s", swap.UsedPercent, swapUsedBytes, swapUsedMagnitude, swapTotalBytes, swapTotalMagnitude)
}

View File

@ -1,7 +1,6 @@
package termui
import (
"fmt"
"sort"
drawille "github.com/cjbassi/drawille-go"
@ -13,6 +12,7 @@ type LineGraph struct {
Data map[string][]float64
LineColor map[string]Color
Zoom int
Labels map[string]string
DefaultLineColor Color
}
@ -23,6 +23,7 @@ func NewLineGraph() *LineGraph {
Block: NewBlock(),
Data: make(map[string][]float64),
LineColor: make(map[string]Color),
Labels: make(map[string]string),
Zoom: 5,
DefaultLineColor: Theme.LineGraph,
@ -103,17 +104,15 @@ func (self *LineGraph) Buffer() *Buffer {
}
}
// renders key ontop
// renders key/label ontop
for j, seriesName := range seriesList {
// sorts lines again
seriesData := self.Data[seriesName]
seriesLineColor, ok := self.LineColor[seriesName]
if !ok {
seriesLineColor = self.DefaultLineColor
}
// render key ontop, but let braille be drawn over space characters
str := fmt.Sprintf("%s %3.0f%%", seriesName, seriesData[len(seriesData)-1])
str := seriesName + " " + self.Labels[seriesName]
for k, char := range str {
if char != ' ' {
buf.SetCell(3+k, j+2, Cell{char, seriesLineColor, self.Bg})