cpu.go (588B)
1 package cpu 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "runtime" 8 "strings" 9 ) 10 11 const path = "/proc/loadavg" 12 13 type Block struct{} 14 15 func New() *Block { 16 return &Block{} 17 } 18 19 func (b *Block) Name() string { 20 return "cpu" 21 } 22 23 func (b *Block) Render(ctx context.Context) (string, error) { 24 out, err := os.ReadFile(path) 25 if err != nil { 26 return "", fmt.Errorf("failed to read %s: %v", path, err) 27 } 28 29 if fields := strings.Fields(string(out)); len(fields) >= 1 { 30 return fmt.Sprintf(" %s/%d", fields[0], runtime.NumCPU()), nil 31 } else { 32 return fmt.Sprintf(" /%d", runtime.NumCPU()), nil 33 } 34 }