From 56975bc80a20ab00ae0bbe30233e8e16f2b9e4d0 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Thu, 12 Sep 2019 20:01:08 -0400 Subject: [PATCH 1/3] Improve bargraph view so bar colors are configurable Inspired by #624 Update the view object so that widget developers can configure bar color --- modules/bargraph/widget.go | 5 +++-- modules/resourceusage/widget.go | 3 +++ view/bargraph.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/bargraph/widget.go b/modules/bargraph/widget.go index 42e98c72..506bfa32 100644 --- a/modules/bargraph/widget.go +++ b/modules/bargraph/widget.go @@ -50,8 +50,9 @@ func MakeGraph(widget *Widget) { barTime = barTime.Add(time.Duration(time.Minute)) bar := view.Bar{ - Label: barTime.Format("15:04"), - Percent: rand.Intn(100-5) + 5, + Label: barTime.Format("15:04"), + Percent: rand.Intn(100-5) + 5, + LabelColor: "red", } stats[i] = bar diff --git a/modules/resourceusage/widget.go b/modules/resourceusage/widget.go index bfdf55d9..2b5ebc56 100644 --- a/modules/resourceusage/widget.go +++ b/modules/resourceusage/widget.go @@ -58,6 +58,7 @@ func MakeGraph(widget *Widget) { Label: fmt.Sprint(i), Percent: int(stat), ValueLabel: fmt.Sprintf("%d%%", int(stat)), + LabelColor: "red", } stats[i] = bar @@ -81,6 +82,7 @@ func MakeGraph(widget *Widget) { Label: "Mem", Percent: int(memInfo.UsedPercent), ValueLabel: fmt.Sprintf("%s/%s", usedMemLabel, totalMemLabel), + LabelColor: "green", } swapIndex := len(cpuStats) + 1 @@ -101,6 +103,7 @@ func MakeGraph(widget *Widget) { Label: "Swp", Percent: int(swapPercent * 100), ValueLabel: fmt.Sprintf("%s/%s", usedSwapLabel, totalSwapLabel), + LabelColor: "yellow", } widget.BarGraph.BuildBars(stats[:]) diff --git a/view/bargraph.go b/view/bargraph.go index 74507317..09b20bb4 100644 --- a/view/bargraph.go +++ b/view/bargraph.go @@ -24,6 +24,7 @@ type Bar struct { Label string Percent int ValueLabel string + LabelColor string } // NewBarGraph creates and returns an instance of BarGraph @@ -78,9 +79,10 @@ func BuildStars(data []Bar, maxStars int, starChar string) string { //write the line buffer.WriteString( fmt.Sprintf( - "%s%s[[red]%s[white]%s] %s\n", + "%s%s[[%s]%s[white]%s] %s\n", bar.Label, strings.Repeat(" ", longestLabel-len(bar.Label)), + bar.LabelColor, strings.Repeat(starChar, starCount), strings.Repeat(" ", maxStars-starCount), label, From 9fccd721e06f7005567791a8c511dfdff734511c Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Fri, 13 Sep 2019 19:22:20 -0400 Subject: [PATCH 2/3] Expand and fix test Add an extra color Fix empty case. This shows up as white/whatever color the rest of the widget is like --- view/bargraph_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/view/bargraph_test.go b/view/bargraph_test.go index 13f13adc..c43cbc06 100644 --- a/view/bargraph_test.go +++ b/view/bargraph_test.go @@ -10,7 +10,7 @@ import ( func makeData() []Bar { //this could come from config - const lineCount = 2 + const lineCount = 3 var stats [lineCount]Bar stats[0] = Bar{ @@ -19,8 +19,15 @@ func makeData() []Bar { } stats[1] = Bar{ - Label: "Jul 09, 2018", - Percent: 80, + Label: "Jul 09, 2018", + Percent: 80, + LabelColor: "red", + } + + stats[2] = Bar{ + Label: "Jul 09, 2018", + Percent: 80, + LabelColor: "green", } return stats[:] @@ -33,7 +40,7 @@ func TestOutput(t *testing.T) { result := BuildStars(makeData(), 20, "*") Equal(t, - "Jun 27, 2018[[red]****[white] ] 20\nJul 09, 2018[[red]****************[white] ] 80\n", + "Jun 27, 2018[[]****[white] ] 20\nJul 09, 2018[[red]****************[white] ] 80\nJul 09, 2018[[green]****************[white] ] 80\n", result, ) } From e28bdac0ee0c8f0b9bba2c7e440e3f94f72f6256 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Sat, 14 Sep 2019 14:00:09 -0400 Subject: [PATCH 3/3] Make default color sub. Also use `default` as defaults --- view/bargraph.go | 9 +++++++-- view/bargraph_test.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/view/bargraph.go b/view/bargraph.go index 09b20bb4..cc992b2c 100644 --- a/view/bargraph.go +++ b/view/bargraph.go @@ -76,13 +76,18 @@ func BuildStars(data []Bar, maxStars int, starChar string) string { label = fmt.Sprint(bar.Percent) } + labelColor := bar.LabelColor + if labelColor == "" { + labelColor = "default" + } + //write the line buffer.WriteString( fmt.Sprintf( - "%s%s[[%s]%s[white]%s] %s\n", + "%s%s[[%s]%s[default]%s] %s\n", bar.Label, strings.Repeat(" ", longestLabel-len(bar.Label)), - bar.LabelColor, + labelColor, strings.Repeat(starChar, starCount), strings.Repeat(" ", maxStars-starCount), label, diff --git a/view/bargraph_test.go b/view/bargraph_test.go index c43cbc06..b9ccd270 100644 --- a/view/bargraph_test.go +++ b/view/bargraph_test.go @@ -40,7 +40,7 @@ func TestOutput(t *testing.T) { result := BuildStars(makeData(), 20, "*") Equal(t, - "Jun 27, 2018[[]****[white] ] 20\nJul 09, 2018[[red]****************[white] ] 80\nJul 09, 2018[[green]****************[white] ] 80\n", + "Jun 27, 2018[[default]****[default] ] 20\nJul 09, 2018[[red]****************[default] ] 80\nJul 09, 2018[[green]****************[default] ] 80\n", result, ) }