From 94d63306d4c536af4e4bfe2b11b5d6208c4855f9 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 4 Aug 2019 21:21:16 -0700 Subject: [PATCH] Move Bargraph functionality into /view --- modules/bargraph/widget.go | 10 +++++----- modules/resourceusage/widget.go | 21 +++++++++++---------- {wtf => view}/bargraph.go | 9 +++++---- {wtf => view}/bargraph_test.go | 2 +- 4 files changed, 22 insertions(+), 20 deletions(-) rename {wtf => view}/bargraph.go (94%) rename {wtf => view}/bargraph_test.go (97%) diff --git a/modules/bargraph/widget.go b/modules/bargraph/widget.go index cb9d4bc8..42e98c72 100644 --- a/modules/bargraph/widget.go +++ b/modules/bargraph/widget.go @@ -9,7 +9,7 @@ import ( "math/rand" "time" - "github.com/wtfutil/wtf/wtf" + "github.com/wtfutil/wtf/view" ) var started = false @@ -17,7 +17,7 @@ var ok = true // Widget define wtf widget to register widget later type Widget struct { - wtf.BarGraph + view.BarGraph app *tview.Application } @@ -25,7 +25,7 @@ type Widget struct { // NewWidget Make new instance of widget func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ - BarGraph: wtf.NewBarGraph(app, "Sample Bar Graph", settings.common, false), + BarGraph: view.NewBarGraph(app, "Sample Bar Graph", settings.common, false), app: app, } @@ -43,13 +43,13 @@ func MakeGraph(widget *Widget) { //this could come from config const lineCount = 8 - var stats [lineCount]wtf.Bar + var stats [lineCount]view.Bar barTime := time.Now() for i := 0; i < lineCount; i++ { barTime = barTime.Add(time.Duration(time.Minute)) - bar := wtf.Bar{ + bar := view.Bar{ Label: barTime.Format("15:04"), Percent: rand.Intn(100-5) + 5, } diff --git a/modules/resourceusage/widget.go b/modules/resourceusage/widget.go index dd676297..bfdf55d9 100644 --- a/modules/resourceusage/widget.go +++ b/modules/resourceusage/widget.go @@ -1,14 +1,15 @@ package resourceusage import ( - "code.cloudfoundry.org/bytefmt" "fmt" + "math" + "time" + + "code.cloudfoundry.org/bytefmt" "github.com/rivo/tview" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/mem" - "github.com/wtfutil/wtf/wtf" - "math" - "time" + "github.com/wtfutil/wtf/view" ) var started = false @@ -16,7 +17,7 @@ var ok = true // Widget define wtf widget to register widget later type Widget struct { - wtf.BarGraph + view.BarGraph app *tview.Application settings *Settings @@ -25,7 +26,7 @@ type Widget struct { // NewWidget Make new instance of widget func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ - BarGraph: wtf.NewBarGraph(app, settings.common.Name, settings.common, false), + BarGraph: view.NewBarGraph(app, settings.common.Name, settings.common, false), app: app, settings: settings, @@ -46,14 +47,14 @@ func MakeGraph(widget *Widget) { return } - var stats = make([]wtf.Bar, len(cpuStats)+2) + var stats = make([]view.Bar, len(cpuStats)+2) for i, stat := range cpuStats { // Stats sometimes jump outside the 0-100 range, possibly due to timing stat = math.Min(100, stat) stat = math.Max(0, stat) - bar := wtf.Bar{ + bar := view.Bar{ Label: fmt.Sprint(i), Percent: int(stat), ValueLabel: fmt.Sprintf("%d%%", int(stat)), @@ -76,7 +77,7 @@ func MakeGraph(widget *Widget) { usedMemLabel = usedMemLabel[:len(usedMemLabel)-1] } - stats[memIndex] = wtf.Bar{ + stats[memIndex] = view.Bar{ Label: "Mem", Percent: int(memInfo.UsedPercent), ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel), @@ -96,7 +97,7 @@ func MakeGraph(widget *Widget) { usedSwapLabel = usedSwapLabel[:len(usedSwapLabel)-1] } - stats[swapIndex] = wtf.Bar{ + stats[swapIndex] = view.Bar{ Label: "Swp", Percent: int(swapPercent * 100), ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel), diff --git a/wtf/bargraph.go b/view/bargraph.go similarity index 94% rename from wtf/bargraph.go rename to view/bargraph.go index ac827524..c2f85548 100644 --- a/wtf/bargraph.go +++ b/view/bargraph.go @@ -1,4 +1,4 @@ -package wtf +package view import ( "bytes" @@ -7,6 +7,7 @@ import ( "github.com/rivo/tview" "github.com/wtfutil/wtf/cfg" + "github.com/wtfutil/wtf/wtf" ) //BarGraph lets make graphs @@ -185,12 +186,12 @@ func BuildStars(data []Bar, maxStars int, starChar string) string { func (widget *BarGraph) addView() *tview.TextView { view := tview.NewTextView() - view.SetBackgroundColor(ColorFor(widget.commonSettings.Colors.Background)) + view.SetBackgroundColor(wtf.ColorFor(widget.commonSettings.Colors.Background)) view.SetBorder(true) - view.SetBorderColor(ColorFor(widget.BorderColor())) + view.SetBorderColor(wtf.ColorFor(widget.BorderColor())) view.SetDynamicColors(true) view.SetTitle(widget.Name()) - view.SetTitleColor(ColorFor(widget.commonSettings.Colors.Title)) + view.SetTitleColor(wtf.ColorFor(widget.commonSettings.Colors.Title)) view.SetWrap(false) return view diff --git a/wtf/bargraph_test.go b/view/bargraph_test.go similarity index 97% rename from wtf/bargraph_test.go rename to view/bargraph_test.go index 6801c286..13f13adc 100644 --- a/wtf/bargraph_test.go +++ b/view/bargraph_test.go @@ -1,4 +1,4 @@ -package wtf +package view import ( "testing"