diff --git a/gotop.go b/gotop.go index 6f67906..4369465 100644 --- a/gotop.go +++ b/gotop.go @@ -167,8 +167,12 @@ func widgetColors() { mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem) LineColor := make(map[string]ui.Color) - for i := 0; i < len(cpu.Data); i++ { - LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i]) + if cpu.Count <= 8 { + for i := 0; i < len(cpu.Data); i++ { + LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i]) + } + } else { + LineColor["Average"] = ui.Color(colorscheme.CPULines[0]) } cpu.LineColor = LineColor diff --git a/widgets/cpu.go b/widgets/cpu.go index 6065279..9d57eba 100644 --- a/widgets/cpu.go +++ b/widgets/cpu.go @@ -10,7 +10,7 @@ import ( type CPU struct { *ui.LineGraph - count int // number of CPUs + Count int // number of CPUs interval time.Duration } @@ -18,14 +18,18 @@ func NewCPU(interval time.Duration, zoom int) *CPU { count, _ := psCPU.Counts(false) c := &CPU{ LineGraph: ui.NewLineGraph(), - count: count, + Count: count, interval: interval, } c.Label = "CPU Usage" c.Zoom = zoom - for i := 0; i < c.count; i++ { - key := "CPU" + strconv.Itoa(i+1) - c.Data[key] = []float64{0} + if c.Count <= 8 { + for i := 0; i < c.Count; i++ { + key := "CPU" + strconv.Itoa(i+1) + c.Data[key] = []float64{0} + } + } else { + c.Data["Average"] = []float64{0} } go c.update() @@ -41,10 +45,14 @@ func NewCPU(interval time.Duration, zoom int) *CPU { func (c *CPU) update() { // psutil calculates the CPU usage over a 1 second interval, therefore it blocks for 1 second - // `true` makes it so psutil doesn't group CPU usage percentages - percent, _ := psCPU.Percent(c.interval, true) - for i := 0; i < c.count; i++ { - key := "CPU" + strconv.Itoa(i+1) - c.Data[key] = append(c.Data[key], percent[i]) + if c.Count <= 8 { + percent, _ := psCPU.Percent(c.interval, true) + for i := 0; i < c.Count; i++ { + key := "CPU" + strconv.Itoa(i+1) + c.Data[key] = append(c.Data[key], percent[i]) + } + } else { + percent, _ := psCPU.Percent(c.interval, false) + c.Data["Average"] = append(c.Data["Average"], percent[0]) } }