From 2a5f3fadee22a2240069a30926ff826bdfffb69f Mon Sep 17 00:00:00 2001 From: Fanir Date: Fri, 9 Jul 2021 01:35:15 +0200 Subject: [PATCH] Moved printLoad() into load.go and created load_windows.go to omit it on Windows; removed usage() --- .gitignore | 3 +++ Makefile | 2 +- load.go | 27 +++++++++++++++++++++++++++ load_windows.go | 5 +++++ statusline.go | 20 -------------------- 5 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 load.go create mode 100644 load_windows.go diff --git a/.gitignore b/.gitignore index 4f062d6..2d8fcb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /release +*.exe + +/.idea diff --git a/Makefile b/Makefile index 38609e4..5624965 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ clean: .PHONY: build build: clean - go build -o "${APP}" ${BUILDFLAGS} + go build ${BUILDFLAGS} .PHONY: install install: diff --git a/load.go b/load.go new file mode 100644 index 0000000..aff1209 --- /dev/null +++ b/load.go @@ -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) +} diff --git a/load_windows.go b/load_windows.go new file mode 100644 index 0000000..48ccdf4 --- /dev/null +++ b/load_windows.go @@ -0,0 +1,5 @@ +package main + +func printLoad(format Format, precision int) { + // todo: implement printLoad on windows +} diff --git a/statusline.go b/statusline.go index 350a076..f0728c3 100644 --- a/statusline.go +++ b/statusline.go @@ -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], "") - 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