diff options
author | Sean E. Russell <ser@ser1.net> | 2020-04-16 13:28:18 -0500 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-04-16 13:28:18 -0500 |
commit | 9e1b63be3250661124babf270f8a13326ef0d2f0 (patch) | |
tree | f2ae4b5cf45e5797e005ada72d1087f3c50dc4ee /layout | |
parent | d22c36e7195b80c199c25a7a0ed2525555e1de00 (diff) |
Closes #46, option to display network traffic as mbps. This can be set on the command line with --mbps, or toggled while running with `b`
Diffstat (limited to 'layout')
-rw-r--r-- | layout/layout.go | 40 | ||||
-rw-r--r-- | layout/parser.go | 108 |
2 files changed, 82 insertions, 66 deletions
diff --git a/layout/layout.go b/layout/layout.go index 5cd8f24..9fc4452 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -25,6 +25,7 @@ type MyGrid struct { *ui.Grid Lines []widgets.Scalable Proc *widgets.ProcWidget + Net *widgets.NetWidget } var widgetNames []string = []string{"cpu", "disk", "mem", "temp", "net", "procs", "batt"} @@ -48,10 +49,25 @@ func Layout(wl layout, c gotop.Config) (*MyGrid, error) { rh := float64(heights[i]) / float64(maxHeight) rgs = append(rgs, ui.NewRow(rh, ur...)) } - grid := &MyGrid{ui.NewGrid(), nil, nil} + grid := &MyGrid{ui.NewGrid(), nil, nil, nil} grid.Set(rgs...) grid.Lines = deepFindScalable(rgs) - grid.Proc = deepFindProc(uiRows) + res := deepFindWidget(uiRows, func(gs interface{}) interface{} { + p, ok := gs.(*widgets.ProcWidget) + if ok { + return p + } + return nil + }) + grid.Proc, _ = res.(*widgets.ProcWidget) + res = deepFindWidget(uiRows, func(gs interface{}) interface{} { + p, ok := gs.(*widgets.NetWidget) + if ok { + return p + } + return nil + }) + grid.Net, _ = res.(*widgets.NetWidget) return grid, nil } @@ -189,6 +205,7 @@ func makeWidget(c gotop.Config, widRule widgetRule) interface{} { n.Lines[0].TitleColor = ui.Color(c.Colorscheme.BorderLabel) n.Lines[1].LineColor = ui.Color(c.Colorscheme.Sparkline) n.Lines[1].TitleColor = ui.Color(c.Colorscheme.BorderLabel) + n.Mbps = c.Mbps w = n case "procs": p := widgets.NewProcWidget() @@ -267,20 +284,19 @@ func countMaxHeight(rs [][]widgetRule) int { return ttl } -// deepFindProc looks in the UI widget tree for the ProcWidget, -// and returns it if found or nil if not. -func deepFindProc(gs interface{}) *widgets.ProcWidget { +// deepFindWidget looks in the UI widget tree for a widget, and returns it if found or nil if not. +func deepFindWidget(gs interface{}, test func(v interface{}) interface{}) interface{} { // Recursive function #1. Recursion is OK here because the number // of UI elements, even in a very complex UI, is going to be // relatively small. t, ok := gs.(ui.GridItem) if ok { - return deepFindProc(t.Entry) + return deepFindWidget(t.Entry, test) } es, ok := gs.([]ui.GridItem) if ok { for _, g := range es { - v := deepFindProc(g) + v := deepFindWidget(g, test) if v != nil { return v } @@ -289,7 +305,7 @@ func deepFindProc(gs interface{}) *widgets.ProcWidget { fs, ok := gs.([]interface{}) if ok { for _, g := range fs { - v := deepFindProc(g) + v := deepFindWidget(g, test) if v != nil { return v } @@ -298,17 +314,13 @@ func deepFindProc(gs interface{}) *widgets.ProcWidget { fs2, ok := gs.([][]interface{}) if ok { for _, g := range fs2 { - v := deepFindProc(g) + v := deepFindWidget(g, test) if v != nil { return v } } } - p, ok := gs.(*widgets.ProcWidget) - if ok { - return p - } - return nil + return test(gs) } // deepFindScalable looks in the UI widget tree for Scalable widgets, diff --git a/layout/parser.go b/layout/parser.go index 024fabb..f35171e 100644 --- a/layout/parser.go +++ b/layout/parser.go @@ -8,64 +8,68 @@ import ( "strings" ) -// The syntax for the layout specification is: -// ``` -// (rowspan:)?widget(/weight)? -// ``` -// 1. Each line is a row -// 2. Empty lines are skipped -// 3. Spaces are compressed -// 4. Legal widget names are: cpu, disk, mem, temp, batt, net, procs -// 5. Names are not case sensitive -// 4. The simplest row is a single widget, by name, e.g. -// ``` -// cpu -// ``` -// 5. Widgets with no weights have a weight of 1. -// 6. If multiple widgets are put on a row with no weights, they will all have -// the same width. -// 7. Weights are integers -// 8. A widget will have a width proportional to its weight divided by the -// total weight count of the row. E.g., -// ``` -// cpu net -// disk/2 mem/4 -// ``` -// The first row will have two widgets: the CPU and network widgets; each -// will be 50% of the total width wide. The second row will have two -// widgets: disk and memory; the first will be 2/6 ~= 33% wide, and the -// second will be 5/7 ~= 67% wide (or, memory will be twice as wide as disk). -// 9. If prefixed by a number and colon, the widget will span that number of -// rows downward. E.g. -// ``` -// 2:cpu -// mem -// ``` -// The CPU widget will be twice as high as the memory widget. Similarly, -// ``` -// mem 2:cpu -// net -// ``` -// memory and network will be in the same row as CPU, one over the other, -// and each half as high as CPU. -// 10. Negative, 0, or non-integer weights will be recorded as "1". Same for row spans. -// 11. Unrecognized widgets will cause the application to abort. -// 12. In rows with multi-row spanning widgets **and** weights, weights in -// lower rows are ignored. Put the weight on the widgets in that row, not -// in later (spanned) rows. -// 13. Widgets are filled in top down, left-to-right order. -// 14. The larges row span in a row defines the top-level row span; all smaller -// row spans constitude sub-rows in the row. For example, `cpu mem/3 net/5` -// means that net/5 will be 5 rows tall overall, and mem will compose 3 of -// them. If following rows do not have enough widgets to fill the gaps, -// spacers will be used. +/********************************************************************************** +The syntax for the layout specification is: +``` +(rowspan:)?widget(/weight)? +``` +1. Each line is a row +2. Empty lines are skipped +3. Spaces are compressed +4. Legal widget names are: cpu, disk, mem, temp, batt, net, procs +5. Names are not case sensitive +4. The simplest row is a single widget, by name, e.g. + ``` + cpu + ``` +5. Widgets with no weights have a weight of 1. +6. If multiple widgets are put on a row with no weights, they will all have + the same width. +7. Weights are integers +8. A widget will have a width proportional to its weight divided by the + total weight count of the row. E.g., + ``` + cpu net + disk/2 mem/4 + ``` + The first row will have two widgets: the CPU and network widgets; each + will be 50% of the total width wide. The second row will have two + widgets: disk and memory; the first will be 2/6 ~= 33% wide, and the + second will be 5/7 ~= 67% wide (or, memory will be twice as wide as disk). +9. If prefixed by a number and colon, the widget will span that number of + rows downward. E.g. + ``` + 2:cpu + mem + ``` + The CPU widget will be twice as high as the memory widget. Similarly, + ``` + mem 2:cpu + net + ``` + memory and network will be in the same row as CPU, one over the other, + and each half as high as CPU. +10. Negative, 0, or non-integer weights will be recorded as "1". Same for row spans. +11. Unrecognized widgets will cause the application to abort. +12. In rows with multi-row spanning widgets **and** weights, weights in + lower rows are ignored. Put the weight on the widgets in that row, not + in later (spanned) rows. +13. Widgets are filled in top down, left-to-right order. +14. The larges row span in a row defines the top-level row span; all smaller + row spans constitude sub-rows in the row. For example, `cpu mem/3 net/5` + means that net/5 will be 5 rows tall overall, and mem will compose 3 of + them. If following rows do not have enough widgets to fill the gaps, + spacers will be used. +15. Lines beginning with "#" will be ignored. It must be the first character of + the line. +**********************************************************************************/ func ParseLayout(i io.Reader) layout { r := bufio.NewScanner(i) rv := layout{Rows: make([][]widgetRule, 0)} var lineNo int for r.Scan() { l := strings.TrimSpace(r.Text()) - if l == "" { + if l == "" || l[0] == '#' { continue } row := make([]widgetRule, 0) |