From b302bdefc54817f2c3b5b733541466dacc03442a Mon Sep 17 00:00:00 2001 From: sqshq Date: Wed, 12 Jun 2019 23:35:12 -0400 Subject: [PATCH] enhance delta value formatting --- component/barchart/barchart.go | 2 +- component/runchart/legend.go | 2 +- component/statusbar.go | 3 ++- component/util/format.go | 33 ++++++++++++++++++++++++++++----- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/component/barchart/barchart.go b/component/barchart/barchart.go index 2295709..14913df 100644 --- a/component/barchart/barchart.go +++ b/component/barchart/barchart.go @@ -147,7 +147,7 @@ func (b *BarChart) Draw(buffer *ui.Buffer) { // draw value & delta value := util.FormatValue(bar.value, b.scale) if bar.delta != 0 { - value = fmt.Sprintf("%s / %s", value, util.FormatValueWithSign(bar.delta, b.scale)) + value = fmt.Sprintf("%s/%s", value, util.FormatDelta(bar.delta, b.scale)) } valueXCoordinate := barXCoordinate + int(float64(barWidth)/2) - diff --git a/component/runchart/legend.go b/component/runchart/legend.go index a35b555..d0885b7 100644 --- a/component/runchart/legend.go +++ b/component/runchart/legend.go @@ -72,7 +72,7 @@ func (c *RunChart) renderLegend(buffer *ui.Buffer, rectangle image.Rectangle) { details := [4]string{ fmt.Sprintf("cur %s", util.FormatValue(getCurrentValue(line), c.scale)), - fmt.Sprintf("dlt %s", util.FormatValueWithSign(getDiffWithPreviousValue(line), c.scale)), + fmt.Sprintf("dlt %s", util.FormatDelta(getDiffWithPreviousValue(line), c.scale)), fmt.Sprintf("max %s", util.FormatValue(line.extrema.max, c.scale)), fmt.Sprintf("min %s", util.FormatValue(line.extrema.min, c.scale)), } diff --git a/component/statusbar.go b/component/statusbar.go index a109f86..38fcac4 100644 --- a/component/statusbar.go +++ b/component/statusbar.go @@ -52,7 +52,6 @@ func NewStatusLine(configFileName string, palette console.Palette, license *meta func (s *StatusBar) Draw(buffer *ui.Buffer) { buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorClear, console.MenuColorBackground)), s.GetRect()) - buffer.SetString(s.text, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min) indent := bindingsIndent for _, binding := range s.keyBindings { @@ -60,6 +59,8 @@ func (s *StatusBar) Draw(buffer *ui.Buffer) { indent += bindingsIndent + len(binding) } + buffer.SetString(s.text, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min) + if s.pause { buffer.SetString(pauseText, ui.NewStyle(console.MenuColorBackground, console.MenuColorText), image.Pt(s.Max.X-s.Dx()/2-len(pauseText)/2, s.Min.Y)) } diff --git a/component/util/format.go b/component/util/format.go index 6889bfd..3ea1715 100644 --- a/component/util/format.go +++ b/component/util/format.go @@ -2,6 +2,7 @@ package util import ( "bytes" + "fmt" ui "github.com/gizak/termui/v3" "image" "math" @@ -26,13 +27,35 @@ func FormatValue(value float64, scale int) string { } } -func FormatValueWithSign(value float64, scale int) string { - if value == 0 { +func FormatDelta(value float64, scale int) string { + + abs := math.Abs(value) + val := value + scl := scale + + postfix := "" + + if abs > 1000 && abs < 1000000 { + val = float64(value) / 1000 + postfix = "k" + } else if abs > 1000000 && abs < 1000000000 { + val = float64(value) / 1000000 + postfix = "M" + } else if abs > 1000000000 { + val = float64(value) / 1000000000 + postfix = "B" + } + + if abs > 1000 { + scl = 1 + } + + if val == 0 { return " 0" - } else if value > 0 { - return "+" + FormatValue(value, scale) + } else if val > 0 { + return fmt.Sprintf("+%s%s", FormatValue(val, scl), postfix) } else { - return FormatValue(value, scale) + return fmt.Sprintf("%s%s", FormatValue(val, scl), postfix) } }