From 1b8c3ed91889363e6eb60f793b4fd01b7ec7805d Mon Sep 17 00:00:00 2001 From: Caleb Bassi <calebjbassi@gmail.com> Date: Sat, 1 Dec 2018 21:00:17 -0800 Subject: [PATCH] Format cpu names based on cpu count --- src/widgets/cpu.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/widgets/cpu.go b/src/widgets/cpu.go index 15c1b548..cd417334 100644 --- a/src/widgets/cpu.go +++ b/src/widgets/cpu.go @@ -11,20 +11,26 @@ import ( type CPU struct { *ui.LineGraph - Count int // number of cores - Average bool // show average load - PerCPU bool // show per-core load - interval time.Duration + Count int // number of cores + Average bool // show average load + PerCPU bool // show per-core load + interval time.Duration + formatString string } func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU { count, _ := psCPU.Counts(false) + formatString := "CPU%1d" + if count > 10 { + formatString = "CPU%02d" + } self := &CPU{ - LineGraph: ui.NewLineGraph(), - Count: count, - interval: interval, - Average: average, - PerCPU: percpu, + LineGraph: ui.NewLineGraph(), + Count: count, + interval: interval, + Average: average, + PerCPU: percpu, + formatString: formatString, } self.Label = "CPU Usage" self.Zoom = zoom @@ -43,7 +49,7 @@ func NewCPU(interval time.Duration, zoom int, average bool, percpu bool) *CPU { if self.PerCPU { for i := 0; i < self.Count; i++ { - k := fmt.Sprintf("CPU%02d", i) + k := fmt.Sprintf(formatString, i) self.Data[k] = []float64{0} } } @@ -85,7 +91,7 @@ func (self *CPU) update() { )) } for i := 0; i < self.Count; i++ { - k := fmt.Sprintf("CPU%02d", i) + k := fmt.Sprintf(self.formatString, i) self.Data[k] = append(self.Data[k], percents[i]) self.Labels[k] = fmt.Sprintf("%3.0f%%", percents[i]) }