Moved printLoad() into load.go and created load_windows.go to omit it on Windows; removed usage()

This commit is contained in:
fanir 2021-07-09 01:35:15 +02:00
parent d28ad1741d
commit 2a5f3fadee
5 changed files with 36 additions and 21 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
/release
*.exe
/.idea

View File

@ -13,7 +13,7 @@ clean:
.PHONY: build
build: clean
go build -o "${APP}" ${BUILDFLAGS}
go build ${BUILDFLAGS}
.PHONY: install
install:

27
load.go Normal file
View File

@ -0,0 +1,27 @@
//+build !windows
package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/load"
"runtime"
)
func printLoad(format Format, precision int) {
l, err := load.Avg()
if err != nil {
panic(err) // fixme
}
cpus, err := cpu.Counts(true)
if err != nil {
// todo: log error
cpus = runtime.NumCPU()
}
if l.Load1 > float64(cpus) {
fmt.Print(format.Warning)
}
fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
precision, l.Load1, l.Load5, l.Load15)
}

5
load_windows.go Normal file
View File

@ -0,0 +1,5 @@
package main
func printLoad(format Format, precision int) {
// todo: implement printLoad on windows
}

View File

@ -4,10 +4,8 @@ import (
"flag"
"fmt"
"os"
"runtime"
"time"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
)
@ -34,24 +32,6 @@ func HBin(a uint64, precision int) string {
return fmt.Sprintf("%.*f%s", precision, f, suffix)
}
func usage() {
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "<window_width>")
os.Exit(2)
}
func printLoad(format Format, precision int) {
l, err := load.Avg()
if err != nil {
panic(err) // fixme
}
if l.Load1 > float64(runtime.NumCPU()) {
fmt.Print(format.Warning)
}
fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
precision, l.Load1, l.Load5, l.Load15)
}
func printMem(format Format, absolute, percent bool) {
if !(absolute || percent) {
return