From 368b8a8f36852d9721f3083207aeee5d7f4aeec2 Mon Sep 17 00:00:00 2001 From: Fanir Date: Sat, 10 Jul 2021 02:19:30 +0200 Subject: [PATCH] restructuring --- .gitignore | 1 + config/config.go | 8 +++++ cpu/load.go | 33 ++++++++++++++++++ cpu/load_windows.go | 8 +++++ load.go | 27 --------------- load_windows.go | 5 --- mem/mem.go | 56 +++++++++++++++++++++++++++++++ statusline.go | 81 +++++++++++---------------------------------- 8 files changed, 126 insertions(+), 93 deletions(-) create mode 100644 config/config.go create mode 100644 cpu/load.go create mode 100644 cpu/load_windows.go delete mode 100644 load.go delete mode 100644 load_windows.go create mode 100644 mem/mem.go diff --git a/.gitignore b/.gitignore index 2d8fcb4..dffb613 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.exe /.idea +/.vscode \ No newline at end of file diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..9a36150 --- /dev/null +++ b/config/config.go @@ -0,0 +1,8 @@ +package config + +type Format struct { + Default, Warning string +} + +var DefaultFormat = Format{} +var TmuxFormat = Format{Default: "#[default]", Warning: "#[bg=colour208]"} diff --git a/cpu/load.go b/cpu/load.go new file mode 100644 index 0000000..97451e0 --- /dev/null +++ b/cpu/load.go @@ -0,0 +1,33 @@ +//+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 +} diff --git a/cpu/load_windows.go b/cpu/load_windows.go new file mode 100644 index 0000000..9794df0 --- /dev/null +++ b/cpu/load_windows.go @@ -0,0 +1,8 @@ +package cpu + +import "go.fanir.de/statusline/config" + +func Load(format config.Format, precision int) (string, error) { + // todo: implement printLoad on windows + return "", nil +} diff --git a/load.go b/load.go deleted file mode 100644 index aff1209..0000000 --- a/load.go +++ /dev/null @@ -1,27 +0,0 @@ -//+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 deleted file mode 100644 index 48ccdf4..0000000 --- a/load_windows.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -func printLoad(format Format, precision int) { - // todo: implement printLoad on windows -} diff --git a/mem/mem.go b/mem/mem.go new file mode 100644 index 0000000..0bd5d4f --- /dev/null +++ b/mem/mem.go @@ -0,0 +1,56 @@ +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 +} diff --git a/statusline.go b/statusline.go index f0728c3..b50c304 100644 --- a/statusline.go +++ b/statusline.go @@ -6,63 +6,22 @@ import ( "os" "time" - "github.com/shirou/gopsutil/v3/mem" + "go.fanir.de/statusline/config" + "go.fanir.de/statusline/cpu" + "go.fanir.de/statusline/mem" ) -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"} - -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 printMem(format Format, absolute, percent bool) { - if !(absolute || percent) { - return - } - - v, err := mem.VirtualMemory() +func Print(s string, err error) { if err != nil { - panic(err) //fixme + 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("]") + fmt.Print(s) } -func printTime(format Format, timeFormat string) { +func Time(format config.Format, timeFormat string) string { now := time.Now() - fmt.Print(format.Default, now.Format(timeFormat)) + return fmt.Sprint(format.Default, now.Format(timeFormat)) } func main() { @@ -70,11 +29,11 @@ func main() { mode := flag.String("style", "none", "Include style directives in output. Valid values: none tmux") flag.Parse() - format := defaultFormat + format := config.DefaultFormat switch *mode { case "", "none": case "tmux": - format = tmuxFormat + format = config.TmuxFormat default: fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() @@ -82,18 +41,18 @@ func main() { } if *cols == 0 || *cols >= 150 { - printLoad(format, 2) - printMem(format, true, true) - printTime(format, " Mon 2006-01-02 15:04") + Print(cpu.Load(format, 2)) + Print(mem.Mem(format, true, true)) + fmt.Print(Time(format, " Mon 2006-01-02 15:04")) } else if *cols >= 120 { - printLoad(format, 2) - printMem(format, false, true) - printTime(format, " 15:04") + Print(cpu.Load(format, 2)) + Print(mem.Mem(format, false, true)) + fmt.Print(Time(format, " 15:04")) } else if *cols >= 100 { - printLoad(format, 0) - printMem(format, false, true) - printTime(format, " 15:04") + Print(cpu.Load(format, 0)) + Print(mem.Mem(format, false, true)) + fmt.Print(Time(format, " 15:04")) } else if *cols >= 80 { - printTime(format, "15:04") + fmt.Print(Time(format, "15:04")) } }