Compare commits

..

No commits in common. "master" and "v0.1" have entirely different histories.
master ... v0.1

7 changed files with 88 additions and 137 deletions

4
.gitignore vendored
View file

@ -1,5 +1 @@
/release /release
*.exe
/.idea
/.vscode

View file

@ -13,7 +13,7 @@ clean:
.PHONY: build .PHONY: build
build: clean build: clean
go build ${BUILDFLAGS} go build -o "${APP}" ${BUILDFLAGS}
.PHONY: install .PHONY: install
install: install:
@ -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}
GOOS=windows GOARCH=386 go build -o "release/${APP}_windows_386.exe" -trimpath ${BUILDFLAGS}

View file

@ -1,8 +0,0 @@
package config
type Format struct {
Default, Warning string
}
var DefaultFormat = Format{}
var TmuxFormat = Format{Default: "#[default]", Warning: "#[bg=colour208]"}

View file

@ -1,33 +0,0 @@
//+build !windows
package cpu
import (
"fmt"
"runtime"
cpuUtil "github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/load"
"go.fanir.de/statusline/config"
)
func Load(format config.Format, precision int) (string, error) {
l, err := load.Avg()
if err != nil {
return "", err // FIXME
}
cpus, err := cpuUtil.Counts(true)
if err != nil {
// TODO: log error
cpus = runtime.NumCPU()
}
if l.Load1 > float64(cpus) {
return fmt.Sprintf(format.Warning), nil
}
return fmt.Sprintf("[%.[1]*[2]f %.[1]*[3]f %.[1]*[4]f]",
precision, l.Load1, l.Load5, l.Load15), nil
}

View file

@ -1,8 +0,0 @@
package cpu
import "go.fanir.de/statusline/config"
func Load(format config.Format, precision int) (string, error) {
// todo: implement printLoad on windows
return "", nil
}

View file

@ -1,56 +0,0 @@
package mem
import (
"fmt"
"strings"
"github.com/shirou/gopsutil/v3/mem"
"go.fanir.de/statusline/config"
)
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 Mem(format config.Format, absolute, percent bool) (string, error) {
if !(absolute || percent) {
return "", nil
}
v, err := mem.VirtualMemory()
if err != nil {
return "", err // FIXME
}
s := &strings.Builder{}
if v.UsedPercent > 90 {
fmt.Fprint(s, format.Warning, "[")
} else {
fmt.Fprint(s, format.Default, "[")
}
if absolute {
fmt.Fprintf(s, "%s/%s", HBin(v.Used, 1), HBin(v.Total, 1))
if percent {
fmt.Fprint(s, " ")
}
}
if percent {
fmt.Fprintf(s, "%.0f%%", v.UsedPercent)
}
fmt.Fprint(s, "]")
return s.String(), nil
}

View file

@ -4,24 +4,85 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"runtime"
"time" "time"
"go.fanir.de/statusline/config" "github.com/shirou/gopsutil/v3/load"
"go.fanir.de/statusline/cpu" "github.com/shirou/gopsutil/v3/mem"
"go.fanir.de/statusline/mem"
) )
func Print(s string, err error) { type Format struct {
if err != nil { Default, Warning string
panic(err) // FIXME
}
fmt.Print(s)
} }
func Time(format config.Format, timeFormat string) string { var defaultFormat = Format{}
var tmuxFormat = Format{Default: "#[default]", Warning: "#[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], "<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
}
v, err := mem.VirtualMemory()
if err != nil {
panic(err) //fixme
}
if v.UsedPercent > 90 {
fmt.Print(format.Warning, "[")
} else {
fmt.Print(format.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 Format, timeFormat string) {
now := time.Now() now := time.Now()
return fmt.Sprint(format.Default, now.Format(timeFormat)) fmt.Print(format.Default, now.Format(timeFormat))
} }
func main() { func main() {
@ -29,11 +90,11 @@ func main() {
mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux") mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux")
flag.Parse() flag.Parse()
format := config.DefaultFormat format := defaultFormat
switch *mode { switch *mode {
case "", "none": case "", "none":
case "tmux": case "tmux":
format = config.TmuxFormat format = tmuxFormat
default: default:
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
@ -41,18 +102,18 @@ func main() {
} }
if *cols == 0 || *cols >= 150 { if *cols == 0 || *cols >= 150 {
Print(cpu.Load(format, 2)) printLoad(format, 2)
Print(mem.Mem(format, true, true)) printMem(format, true, true)
fmt.Print(Time(format, " Mon 2006-01-02 15:04")) printTime(format, " Mon 2006-01-02 15:04")
} else if *cols >= 120 { } else if *cols >= 120 {
Print(cpu.Load(format, 2)) printLoad(format, 2)
Print(mem.Mem(format, false, true)) printMem(format, false, true)
fmt.Print(Time(format, " 15:04")) printTime(format, " 15:04")
} else if *cols >= 100 { } else if *cols >= 100 {
Print(cpu.Load(format, 0)) printLoad(format, 0)
Print(mem.Mem(format, false, true)) printMem(format, false, true)
fmt.Print(Time(format, " 15:04")) printTime(format, " 15:04")
} else if *cols >= 80 { } else if *cols >= 80 {
fmt.Print(Time(format, "15:04")) printTime(format, "15:04")
} }
} }