From afca41929592c485c9c0a791050c9e631e27a5e2 Mon Sep 17 00:00:00 2001 From: sqshq Date: Fri, 21 Jun 2019 22:56:23 -0400 Subject: [PATCH] gauge display enhancements: added ability to show/hide current value --- README.md | 1 + component/gauge/gauge.go | 31 +++++++++++++++++++------------ config/component.go | 1 + config/default.go | 4 ++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ce6fa68..153510a 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ gauges: - title: Minute progress rate-ms: 500 # sampling rate, default = 1000 scale: 2 # number of digits after sample decimal point, default = 1 + percent-only: false # toggle display of the current value, default = false color: 178 # 8-bit color number, default one is chosen from a pre-defined palette cur: sample: date +%S # sample script for current value diff --git a/component/gauge/gauge.go b/component/gauge/gauge.go index 9192822..8712de7 100644 --- a/component/gauge/gauge.go +++ b/component/gauge/gauge.go @@ -20,22 +20,24 @@ const ( type Gauge struct { *ui.Block *data.Consumer - minValue float64 - maxValue float64 - curValue float64 - color ui.Color - scale int - palette console.Palette + minValue float64 + maxValue float64 + curValue float64 + color ui.Color + scale int + percentOnly bool + palette console.Palette } func NewGauge(c config.GaugeConfig, palette console.Palette) *Gauge { g := Gauge{ - Block: component.NewBlock(c.Title, true, palette), - Consumer: data.NewConsumer(), - scale: *c.Scale, - color: *c.Color, - palette: palette, + Block: component.NewBlock(c.Title, true, palette), + Consumer: data.NewConsumer(), + color: *c.Color, + scale: *c.Scale, + percentOnly: *c.PercentOnly, + palette: palette, } go func() { @@ -84,7 +86,12 @@ func (g *Gauge) Draw(buffer *ui.Buffer) { percent = (100 * g.curValue) / (g.maxValue - g.minValue) } - label := fmt.Sprintf("%v%% (%v)", util.FormatValue(percent, g.scale), g.curValue) + var label string + if g.percentOnly { + label = fmt.Sprintf(" %v%% ", util.FormatValue(percent, g.scale)) + } else { + label = fmt.Sprintf(" %v%% (%v) ", util.FormatValue(percent, g.scale), util.FormatValue(g.curValue, g.scale)) + } // draw bar barWidth := int((percent / 100) * float64(g.Inner.Dx())) diff --git a/config/component.go b/config/component.go index b11fa27..6215443 100644 --- a/config/component.go +++ b/config/component.go @@ -58,6 +58,7 @@ type GaugeConfig struct { ComponentConfig `yaml:",inline"` Scale *int `yaml:"scale,omitempty"` Color *ui.Color `yaml:"color,omitempty"` + PercentOnly *bool `yaml:"percent-only,omitempty"` Cur Item `yaml:"cur"` Max Item `yaml:"max"` Min Item `yaml:"min"` diff --git a/config/default.go b/config/default.go index 3fdf7fd..73d1907 100644 --- a/config/default.go +++ b/config/default.go @@ -178,6 +178,7 @@ func (c *Config) setDefaultItemSettings() { palette := console.GetPalette(*c.Theme) colorsCount := len(palette.ContentColors) defaultPty := false + defaultPercentOnly := false for _, ch := range c.RunCharts { for j, item := range ch.Items { @@ -224,6 +225,9 @@ func (c *Config) setDefaultItemSettings() { if g.Color == nil { g.Color = &palette.ContentColors[i%colorsCount] } + if g.PercentOnly == nil { + g.PercentOnly = &defaultPercentOnly + } c.Gauges[i] = g }