commit ccf383db094311553ab534b13bf7f54ffb173d35 Author: Fanir Date: Sat Nov 21 04:07:18 2020 +0100 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..70f6bb3 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: build + +build: statusline.go + go build statusline.go + +run: + go run statusline.go + +install: + go build -o ~/bin/statusline statusline.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e7c4a9c --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.fanir.de/statusline + +go 1.15 + +require github.com/shirou/gopsutil/v3 v3.20.10 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b6389b2 --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk= +github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI= +github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/shirou/gopsutil/v3 v3.20.10 h1:7zomV9HJv6UGk225YtvEa5+camNLpbua3MAz/GqiVJY= +github.com/shirou/gopsutil/v3 v3.20.10/go.mod h1:igHnfak0qnw1biGeI2qKQvu0ZkwvEkUcCLlYhZzdr/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 h1:iCaAy5bMeEvwANu3YnJfWwI0kWAGkEa2RXPdweI/ysk= +golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/statusline.go b/statusline.go new file mode 100644 index 0000000..dd7f32d --- /dev/null +++ b/statusline.go @@ -0,0 +1,108 @@ +package main + +import ( + "fmt" + "os" + "runtime" + "strconv" + "time" + + "github.com/shirou/gopsutil/v3/load" + "github.com/shirou/gopsutil/v3/mem" +) + +const warnFormat = "#[bg=colour208]" + +var binarySuffixes = []string{"B", "KiB", "MiB", "GiB", "TiB"} + +const binaryFactor = 1024 + +func HBin(a uint64, precision int) string { + f := float64(a) + var suffix string + for _, suffix = range binarySuffixes { + if f < binaryFactor { + break + } + f /= binaryFactor + } + return fmt.Sprintf("%.*f%s", precision, f, suffix) +} + +func usage() { + fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "") + os.Exit(2) +} + +func printLoad(precision int) { + l, err := load.Avg() + if err != nil { + panic(err) // fixme + } + + if l.Load1 > float64(runtime.NumCPU()) { + fmt.Print(warnFormat) + } + fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]", + precision, l.Load1, l.Load5, l.Load15) +} + +func printMem(absolute, percent bool) { + if !(absolute || percent) { + return + } + + v, err := mem.VirtualMemory() + if err != nil { + panic(err) //fixme + } + + if v.UsedPercent > 90 { + fmt.Print(warnFormat, "[") + } else { + fmt.Print("#[default][") + } + if absolute { + fmt.Printf("%s/%s", HBin(v.Used, 1), HBin(v.Total, 1)) + if percent { + fmt.Print(" ") + } + } + if percent { + fmt.Printf("%.0f%%", v.UsedPercent) + } + fmt.Print("]") +} + +func printTime(format string) { + now := time.Now() + + fmt.Print(now.Format("#[default]" + format)) +} + +func main() { + if len(os.Args) != 2 { + usage() + } + cols, err := strconv.Atoi(os.Args[1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + usage() + } + + if cols >= 150 { + printLoad(2) + printMem(true, true) + printTime(" Mon 2006-01-02 15:04") + } else if cols >= 120 { + printLoad(2) + printMem(false, true) + printTime(" 15:04") + } else if cols >= 100 { + printLoad(0) + printMem(false, true) + printTime(" 15:04") + } else if cols >= 80 { + printTime("15:04") + } +}