2020-11-21 04:07:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-22 00:10:12 +01:00
|
|
|
"flag"
|
2020-11-21 04:07:18 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/load"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
)
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
type Format struct {
|
|
|
|
Default, Warning string
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultFormat = Format{}
|
|
|
|
var tmuxFormat = Format{Default: "#[default]", Warning: "#[bg=colour208]"}
|
2020-11-21 04:07:18 +01:00
|
|
|
|
|
|
|
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], "<window_width>")
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
func printLoad(format Format, precision int) {
|
2020-11-21 04:07:18 +01:00
|
|
|
l, err := load.Avg()
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // fixme
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Load1 > float64(runtime.NumCPU()) {
|
2020-11-22 00:10:12 +01:00
|
|
|
fmt.Print(format.Warning)
|
2020-11-21 04:07:18 +01:00
|
|
|
}
|
|
|
|
fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
|
|
|
|
precision, l.Load1, l.Load5, l.Load15)
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
func printMem(format Format, absolute, percent bool) {
|
2020-11-21 04:07:18 +01:00
|
|
|
if !(absolute || percent) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
panic(err) //fixme
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.UsedPercent > 90 {
|
2020-11-22 00:10:12 +01:00
|
|
|
fmt.Print(format.Warning, "[")
|
2020-11-21 04:07:18 +01:00
|
|
|
} else {
|
2020-11-22 00:10:12 +01:00
|
|
|
fmt.Print(format.Default, "[")
|
2020-11-21 04:07:18 +01:00
|
|
|
}
|
|
|
|
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("]")
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
func printTime(format Format, timeFormat string) {
|
2020-11-21 04:07:18 +01:00
|
|
|
now := time.Now()
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
fmt.Print(format.Default, now.Format(timeFormat))
|
2020-11-21 04:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-11-22 00:10:12 +01:00
|
|
|
cols := flag.Uint("c", 0, "Window width in columns. 0 prints everything.")
|
|
|
|
mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
format := defaultFormat
|
|
|
|
switch *mode {
|
|
|
|
case "", "none":
|
|
|
|
case "tmux":
|
|
|
|
format = tmuxFormat
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
2020-11-21 04:07:18 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 00:10:12 +01:00
|
|
|
if *cols == 0 || *cols >= 150 {
|
|
|
|
printLoad(format, 2)
|
|
|
|
printMem(format, true, true)
|
|
|
|
printTime(format, " Mon 2006-01-02 15:04")
|
|
|
|
} else if *cols >= 120 {
|
|
|
|
printLoad(format, 2)
|
|
|
|
printMem(format, false, true)
|
|
|
|
printTime(format, " 15:04")
|
|
|
|
} else if *cols >= 100 {
|
|
|
|
printLoad(format, 0)
|
|
|
|
printMem(format, false, true)
|
|
|
|
printTime(format, " 15:04")
|
|
|
|
} else if *cols >= 80 {
|
|
|
|
printTime(format, "15:04")
|
2020-11-21 04:07:18 +01:00
|
|
|
}
|
|
|
|
}
|