gauge display enhancements: added ability to show/hide current value

This commit is contained in:
sqshq 2019-06-21 22:56:23 -04:00
parent 8d88f85f05
commit afca419295
4 changed files with 25 additions and 12 deletions

View File

@ -105,6 +105,7 @@ gauges:
- title: Minute progress - title: Minute progress
rate-ms: 500 # sampling rate, default = 1000 rate-ms: 500 # sampling rate, default = 1000
scale: 2 # number of digits after sample decimal point, default = 1 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 color: 178 # 8-bit color number, default one is chosen from a pre-defined palette
cur: cur:
sample: date +%S # sample script for current value sample: date +%S # sample script for current value

View File

@ -25,6 +25,7 @@ type Gauge struct {
curValue float64 curValue float64
color ui.Color color ui.Color
scale int scale int
percentOnly bool
palette console.Palette palette console.Palette
} }
@ -33,8 +34,9 @@ func NewGauge(c config.GaugeConfig, palette console.Palette) *Gauge {
g := Gauge{ g := Gauge{
Block: component.NewBlock(c.Title, true, palette), Block: component.NewBlock(c.Title, true, palette),
Consumer: data.NewConsumer(), Consumer: data.NewConsumer(),
scale: *c.Scale,
color: *c.Color, color: *c.Color,
scale: *c.Scale,
percentOnly: *c.PercentOnly,
palette: palette, palette: palette,
} }
@ -84,7 +86,12 @@ func (g *Gauge) Draw(buffer *ui.Buffer) {
percent = (100 * g.curValue) / (g.maxValue - g.minValue) 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 // draw bar
barWidth := int((percent / 100) * float64(g.Inner.Dx())) barWidth := int((percent / 100) * float64(g.Inner.Dx()))

View File

@ -58,6 +58,7 @@ type GaugeConfig struct {
ComponentConfig `yaml:",inline"` ComponentConfig `yaml:",inline"`
Scale *int `yaml:"scale,omitempty"` Scale *int `yaml:"scale,omitempty"`
Color *ui.Color `yaml:"color,omitempty"` Color *ui.Color `yaml:"color,omitempty"`
PercentOnly *bool `yaml:"percent-only,omitempty"`
Cur Item `yaml:"cur"` Cur Item `yaml:"cur"`
Max Item `yaml:"max"` Max Item `yaml:"max"`
Min Item `yaml:"min"` Min Item `yaml:"min"`

View File

@ -178,6 +178,7 @@ func (c *Config) setDefaultItemSettings() {
palette := console.GetPalette(*c.Theme) palette := console.GetPalette(*c.Theme)
colorsCount := len(palette.ContentColors) colorsCount := len(palette.ContentColors)
defaultPty := false defaultPty := false
defaultPercentOnly := false
for _, ch := range c.RunCharts { for _, ch := range c.RunCharts {
for j, item := range ch.Items { for j, item := range ch.Items {
@ -224,6 +225,9 @@ func (c *Config) setDefaultItemSettings() {
if g.Color == nil { if g.Color == nil {
g.Color = &palette.ContentColors[i%colorsCount] g.Color = &palette.ContentColors[i%colorsCount]
} }
if g.PercentOnly == nil {
g.PercentOnly = &defaultPercentOnly
}
c.Gauges[i] = g c.Gauges[i] = g
} }