Reworks the -l argument. Combines the layout args into one (smarter) one, and adds the ability to parse layouts from stdin. Re-implements --minimal as a layout.

This commit is contained in:
Sean E. Russell 2020-02-13 13:24:41 -06:00
parent 44b8ac9c1a
commit 4247e9339e
2 changed files with 23 additions and 32 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -31,6 +30,7 @@ const (
graphHorizontalScaleDelta = 3 graphHorizontalScaleDelta = 3
defaultUI = "cpu\ndisk/1 2:mem/2\ntemp\nnet procs" defaultUI = "cpu\ndisk/1 2:mem/2\ntemp\nnet procs"
minimalUI = "cpu\nmem procs"
) )
var ( var (
@ -48,7 +48,7 @@ Usage: gotop [options]
Options: Options:
-c, --color=NAME Set a colorscheme. -c, --color=NAME Set a colorscheme.
-h, --help Show this screen. -h, --help Show this screen.
-m, --minimal Only show CPU, Mem and Process widgets. -m, --minimal Only show CPU, Mem and Process widgets. Overrides -l
-r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1]. -r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1].
-V, --version Print version and exit. -V, --version Print version and exit.
-p, --percpu Show each CPU in the CPU widget. -p, --percpu Show each CPU in the CPU widget.
@ -57,8 +57,8 @@ Options:
-s, --statusbar Show a statusbar with the time. -s, --statusbar Show a statusbar with the time.
-b, --battery Show battery level widget ('minimal' turns off). -b, --battery Show battery level widget ('minimal' turns off).
-i, --interface=NAME Select network interface [default: all]. -i, --interface=NAME Select network interface [default: all].
-l, --layout=NAME Name of layout spec file for the UI -B, --bandwidth=bits Specify the number of bits per seconds.
--layout-file=NAME Path to a layout file -l, --layout=NAME Name of layout spec file for the UI. Looks first in $XDG_CONFIG_HOME/gotop, then as a path. Use "-" to pipe.
Colorschemes: Colorschemes:
default default
@ -80,7 +80,6 @@ Colorschemes:
HelpVisible: false, HelpVisible: false,
Colorscheme: colorschemes.Default, Colorscheme: colorschemes.Default,
UpdateInterval: time.Second, UpdateInterval: time.Second,
MinimalMode: false,
AverageLoad: false, AverageLoad: false,
PercpuLoad: false, PercpuLoad: false,
TempScale: w.Celsius, TempScale: w.Celsius,
@ -95,20 +94,21 @@ Colorschemes:
} }
if val, _ := args["--layout"]; val != nil { if val, _ := args["--layout"]; val != nil {
fp := filepath.Join(cd, val.(string)) s := val.(string)
if _, err := os.Stat(fp); err == nil { if s == "-" {
conf.LayoutFile = fp conf.Layout = os.Stdin
} else { } else {
conf.LayoutFile = "" fp := filepath.Join(cd, s)
} conf.Layout, err = os.Open(fp)
} if err != nil {
if val, _ := args["--layout-file"]; val != nil { conf.Layout, err = os.Open(s)
fp := val.(string) if err != nil {
if _, err := os.Stat(fp); err == nil { stderrLogger.Fatalf("Unable to open layout file %s", fp)
conf.LayoutFile = fp }
} else { }
conf.LayoutFile = ""
} }
} else {
conf.Layout = strings.NewReader(defaultUI)
} }
if val, _ := args["--color"]; val != nil { if val, _ := args["--color"]; val != nil {
cs, err := handleColorscheme(val.(string)) cs, err := handleColorscheme(val.(string))
@ -120,9 +120,11 @@ Colorschemes:
conf.AverageLoad, _ = args["--averagecpu"].(bool) conf.AverageLoad, _ = args["--averagecpu"].(bool)
conf.PercpuLoad, _ = args["--percpu"].(bool) conf.PercpuLoad, _ = args["--percpu"].(bool)
conf.Battery, _ = args["--battery"].(bool) conf.Battery, _ = args["--battery"].(bool)
conf.MinimalMode, _ = args["--minimal"].(bool)
statusbar, _ = args["--statusbar"].(bool) statusbar, _ = args["--statusbar"].(bool)
if args["--minimal"].(bool) {
conf.Layout = strings.NewReader(minimalUI)
}
rateStr, _ := args["--rate"].(string) rateStr, _ := args["--rate"].(string)
rate, err := strconv.ParseFloat(rateStr, 64) rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil { if err != nil {
@ -137,7 +139,6 @@ Colorschemes:
if fahrenheit { if fahrenheit {
conf.TempScale = w.Fahrenheit conf.TempScale = w.Fahrenheit
} }
conf.NetInterface, _ = args["--interface"].(string)
return conf, nil return conf, nil
} }
@ -391,17 +392,7 @@ func main() {
bar = w.NewStatusBar() bar = w.NewStatusBar()
} }
var lin io.Reader ly := layout.ParseLayout(conf.Layout)
lin = strings.NewReader(defaultUI)
if conf.LayoutFile != "" {
fin, err := os.Open(conf.LayoutFile)
defer fin.Close()
if err != nil {
stderrLogger.Fatalf("Layout %s not found.", conf.LayoutFile)
}
lin = fin
}
ly := layout.ParseLayout(lin)
grid, err := layout.Layout(ly, conf) grid, err := layout.Layout(ly, conf)
if err != nil { if err != nil {
stderrLogger.Fatalf("failed to initialize termui: %v", err) stderrLogger.Fatalf("failed to initialize termui: %v", err)

View File

@ -1,6 +1,7 @@
package gotop package gotop
import ( import (
"io"
"time" "time"
"github.com/cjbassi/gotop/colorschemes" "github.com/cjbassi/gotop/colorschemes"
@ -17,12 +18,11 @@ type Config struct {
Colorscheme colorschemes.Colorscheme Colorscheme colorschemes.Colorscheme
UpdateInterval time.Duration UpdateInterval time.Duration
MinimalMode bool
AverageLoad bool AverageLoad bool
PercpuLoad bool PercpuLoad bool
TempScale widgets.TempScale TempScale widgets.TempScale
Battery bool Battery bool
Statusbar bool Statusbar bool
NetInterface string NetInterface string
LayoutFile string Layout io.Reader
} }