enhance delta value formatting

This commit is contained in:
sqshq 2019-06-12 23:35:12 -04:00
parent a148f4250d
commit b302bdefc5
4 changed files with 32 additions and 8 deletions

View File

@ -147,7 +147,7 @@ func (b *BarChart) Draw(buffer *ui.Buffer) {
// draw value & delta // draw value & delta
value := util.FormatValue(bar.value, b.scale) value := util.FormatValue(bar.value, b.scale)
if bar.delta != 0 { 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 + valueXCoordinate := barXCoordinate +
int(float64(barWidth)/2) - int(float64(barWidth)/2) -

View File

@ -72,7 +72,7 @@ func (c *RunChart) renderLegend(buffer *ui.Buffer, rectangle image.Rectangle) {
details := [4]string{ details := [4]string{
fmt.Sprintf("cur %s", util.FormatValue(getCurrentValue(line), c.scale)), 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("max %s", util.FormatValue(line.extrema.max, c.scale)),
fmt.Sprintf("min %s", util.FormatValue(line.extrema.min, c.scale)), fmt.Sprintf("min %s", util.FormatValue(line.extrema.min, c.scale)),
} }

View File

@ -52,7 +52,6 @@ func NewStatusLine(configFileName string, palette console.Palette, license *meta
func (s *StatusBar) Draw(buffer *ui.Buffer) { func (s *StatusBar) Draw(buffer *ui.Buffer) {
buffer.Fill(ui.NewCell(' ', ui.NewStyle(console.ColorClear, console.MenuColorBackground)), s.GetRect()) 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 indent := bindingsIndent
for _, binding := range s.keyBindings { for _, binding := range s.keyBindings {
@ -60,6 +59,8 @@ func (s *StatusBar) Draw(buffer *ui.Buffer) {
indent += bindingsIndent + len(binding) indent += bindingsIndent + len(binding)
} }
buffer.SetString(s.text, ui.NewStyle(console.MenuColorText, console.MenuColorBackground), s.Min)
if s.pause { 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)) buffer.SetString(pauseText, ui.NewStyle(console.MenuColorBackground, console.MenuColorText), image.Pt(s.Max.X-s.Dx()/2-len(pauseText)/2, s.Min.Y))
} }

View File

@ -2,6 +2,7 @@ package util
import ( import (
"bytes" "bytes"
"fmt"
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
"image" "image"
"math" "math"
@ -26,13 +27,35 @@ func FormatValue(value float64, scale int) string {
} }
} }
func FormatValueWithSign(value float64, scale int) string { func FormatDelta(value float64, scale int) string {
if value == 0 {
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" return " 0"
} else if value > 0 { } else if val > 0 {
return "+" + FormatValue(value, scale) return fmt.Sprintf("+%s%s", FormatValue(val, scl), postfix)
} else { } else {
return FormatValue(value, scale) return fmt.Sprintf("%s%s", FormatValue(val, scl), postfix)
} }
} }