statusline/cpu/load.go

34 lines
599 B
Go
Raw Permalink Normal View History

2021-07-10 02:19:30 +02:00
//+build !windows
package cpu
import (
"fmt"
"runtime"
cpuUtil "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/load"
"go.fanir.de/statusline/config"
)
func Load(format config.Format, precision int) (string, error) {
l, err := load.Avg()
if err != nil {
return "", err // FIXME
}
cpus, err := cpuUtil.Counts(true)
if err != nil {
// TODO: log error
cpus = runtime.NumCPU()
}
if l.Load1 > float64(cpus) {
return fmt.Sprintf(format.Warning), nil
}
return fmt.Sprintf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
precision, l.Load1, l.Load5, l.Load15), nil
}