command line flags and no `make release` for Windows.

Added command line flags for styling and width.
Printing load averages doesn't work on windows.
This commit is contained in:
fanir 2020-11-22 00:10:12 +01:00
parent 52ccdce51f
commit d28ad1741d
3 changed files with 42 additions and 31 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/release

View File

@ -29,5 +29,4 @@ release:
GOOS=linux GOARCH=amd64 go build -o "release/${APP}_linux_amd64" -trimpath ${BUILDFLAGS} GOOS=linux GOARCH=amd64 go build -o "release/${APP}_linux_amd64" -trimpath ${BUILDFLAGS}
GOOS=linux GOARCH=arm64 go build -o "release/${APP}_linux_arm64" -trimpath ${BUILDFLAGS} GOOS=linux GOARCH=arm64 go build -o "release/${APP}_linux_arm64" -trimpath ${BUILDFLAGS}
GOOS=linux GOARCH=386 go build -o "release/${APP}_linux_386" -trimpath ${BUILDFLAGS} GOOS=linux GOARCH=386 go build -o "release/${APP}_linux_386" -trimpath ${BUILDFLAGS}
GOOS=windows GOARCH=amd64 go build -o "release/${APP}_windows_amd64.exe" -trimpath ${BUILDFLAGS}

View File

@ -1,17 +1,22 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
"strconv"
"time" "time"
"github.com/shirou/gopsutil/v3/load" "github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem" "github.com/shirou/gopsutil/v3/mem"
) )
const warnFormat = "#[bg=colour208]" type Format struct {
Default, Warning string
}
var defaultFormat = Format{}
var tmuxFormat = Format{Default: "#[default]", Warning: "#[bg=colour208]"}
var binarySuffixes = []string{"B", "KiB", "MiB", "GiB", "TiB"} var binarySuffixes = []string{"B", "KiB", "MiB", "GiB", "TiB"}
@ -34,20 +39,20 @@ func usage() {
os.Exit(2) os.Exit(2)
} }
func printLoad(precision int) { func printLoad(format Format, precision int) {
l, err := load.Avg() l, err := load.Avg()
if err != nil { if err != nil {
panic(err) // fixme panic(err) // fixme
} }
if l.Load1 > float64(runtime.NumCPU()) { if l.Load1 > float64(runtime.NumCPU()) {
fmt.Print(warnFormat) fmt.Print(format.Warning)
} }
fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]", fmt.Printf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
precision, l.Load1, l.Load5, l.Load15) precision, l.Load1, l.Load5, l.Load15)
} }
func printMem(absolute, percent bool) { func printMem(format Format, absolute, percent bool) {
if !(absolute || percent) { if !(absolute || percent) {
return return
} }
@ -58,9 +63,9 @@ func printMem(absolute, percent bool) {
} }
if v.UsedPercent > 90 { if v.UsedPercent > 90 {
fmt.Print(warnFormat, "[") fmt.Print(format.Warning, "[")
} else { } else {
fmt.Print("#[default][") fmt.Print(format.Default, "[")
} }
if absolute { if absolute {
fmt.Printf("%s/%s", HBin(v.Used, 1), HBin(v.Total, 1)) fmt.Printf("%s/%s", HBin(v.Used, 1), HBin(v.Total, 1))
@ -74,35 +79,41 @@ func printMem(absolute, percent bool) {
fmt.Print("]") fmt.Print("]")
} }
func printTime(format string) { func printTime(format Format, timeFormat string) {
now := time.Now() now := time.Now()
fmt.Print(now.Format("#[default]" + format)) fmt.Print(format.Default, now.Format(timeFormat))
} }
func main() { func main() {
if len(os.Args) != 2 { cols := flag.Uint("c", 0, "Window width in columns. 0 prints everything.")
usage() mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux")
} flag.Parse()
cols, err := strconv.Atoi(os.Args[1])
if err != nil { format := defaultFormat
fmt.Fprintln(os.Stderr, err) switch *mode {
usage() case "", "none":
case "tmux":
format = tmuxFormat
default:
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
} }
if cols >= 150 { if *cols == 0 || *cols >= 150 {
printLoad(2) printLoad(format, 2)
printMem(true, true) printMem(format, true, true)
printTime(" Mon 2006-01-02 15:04") printTime(format, " Mon 2006-01-02 15:04")
} else if cols >= 120 { } else if *cols >= 120 {
printLoad(2) printLoad(format, 2)
printMem(false, true) printMem(format, false, true)
printTime(" 15:04") printTime(format, " 15:04")
} else if cols >= 100 { } else if *cols >= 100 {
printLoad(0) printLoad(format, 0)
printMem(false, true) printMem(format, false, true)
printTime(" 15:04") printTime(format, " 15:04")
} else if cols >= 80 { } else if *cols >= 80 {
printTime("15:04") printTime(format, "15:04")
} }
} }