|
|
|
@ -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
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
panic(err) // FIXME
|
|
|
|
|
} |
|
|
|
|
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")) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|