statusline/statusline.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-11-21 04:07:18 +01:00
package main
import (
"flag"
2020-11-21 04:07:18 +01:00
"fmt"
"os"
"time"
2021-07-10 02:19:30 +02:00
"go.fanir.de/statusline/config"
"go.fanir.de/statusline/cpu"
"go.fanir.de/statusline/mem"
2020-11-21 04:07:18 +01:00
)
2021-07-10 02:19:30 +02:00
func Print(s string, err error) {
2020-11-21 04:07:18 +01:00
if err != nil {
2021-07-10 02:19:30 +02:00
panic(err) // FIXME
2020-11-21 04:07:18 +01:00
}
2021-07-10 02:19:30 +02:00
fmt.Print(s)
2020-11-21 04:07:18 +01:00
}
2021-07-10 02:19:30 +02:00
func Time(format config.Format, timeFormat string) string {
2020-11-21 04:07:18 +01:00
now := time.Now()
2021-07-10 02:19:30 +02:00
return fmt.Sprint(format.Default, now.Format(timeFormat))
2020-11-21 04:07:18 +01:00
}
func main() {
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()
2021-07-10 02:19:30 +02:00
format := config.DefaultFormat
switch *mode {
case "", "none":
case "tmux":
2021-07-10 02:19:30 +02:00
format = config.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
}
if *cols == 0 || *cols >= 150 {
2021-07-10 02:19:30 +02:00
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 {
2021-07-10 02:19:30 +02:00
Print(cpu.Load(format, 2))
Print(mem.Mem(format, false, true))
fmt.Print(Time(format, " 15:04"))
} else if *cols >= 100 {
2021-07-10 02:19:30 +02:00
Print(cpu.Load(format, 0))
Print(mem.Mem(format, false, true))
fmt.Print(Time(format, " 15:04"))
} else if *cols >= 80 {
2021-07-10 02:19:30 +02:00
fmt.Print(Time(format, "15:04"))
2020-11-21 04:07:18 +01:00
}
}