Skip to content

Commit

Permalink
Correct bargraph alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Aug 19, 2022
1 parent 601fa74 commit ac3dc37
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
59 changes: 59 additions & 0 deletions pkg/multiterm/termrenderers/bargraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import (
"strings"
)

type barGraphPair struct {
name string
vals []int64
}

type BarGraph struct {
writer multiterm.MultilineTerm

maxKeyLength int
subKeys []string
rows []barGraphPair
maxLineVal int64
maxRows int
prefixLines int
Expand Down Expand Up @@ -51,10 +57,63 @@ func (s *BarGraph) SetKeys(keyItems ...string) {

// Writes bar graph values, assuming vals map to the keyItems for each index
func (s *BarGraph) WriteBar(idx int, key string, vals ...int64) {
// Update max key-len
if klen := color.StrLen(key); klen > s.maxKeyLength {
s.maxKeyLength = klen
}

// Save row data for re-draw's
for idx >= len(s.rows) {
s.rows = append(s.rows, barGraphPair{})
}

s.rows[idx] = barGraphPair{
name: key,
vals: vals,
}

// Compute the updated max
redraw := false
{
var max int64
if s.Stacked {
max = sumi64(vals...)
} else {
max = maxi64(vals...)
}
if max > s.maxLineVal {
s.maxLineVal = max
redraw = true
}
}

// Draw or redraw
if redraw {
for idx, row := range s.rows {
s.writeBar(idx, row.name, row.vals...)
}
} else {
s.writeBar(idx, key, vals...)
}
}

func maxi64(vals ...int64) (ret int64) {
for _, v := range vals {
if v > ret {
ret = v
}
}
return
}

func sumi64(vals ...int64) (ret int64) {
for _, v := range vals {
ret += v
}
return
}

func (s *BarGraph) writeBar(idx int, key string, vals ...int64) {
if s.Stacked {
s.writeBarStacked(idx, key, vals...)
} else {
Expand Down
11 changes: 8 additions & 3 deletions pkg/multiterm/termrenderers/bargraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestBargraphRendering(t *testing.T) {
bg.WriteFooter(0, "abc")

assert.Equal(t, " █ a █ b", v.Get(0))
assert.Equal(t, "test █████████████████████████ 1", v.Get(1))
assert.Equal(t, " ██████████████████████████████████████████████████ 2", v.Get(2))
assert.Equal(t, "test ████████████████ 1", v.Get(1))
assert.Equal(t, " █████████████████████████████████ 2", v.Get(2))
assert.Equal(t, "tes2 ████████████████▊ 1", v.Get(3))
assert.Equal(t, " ██████████████████████████████████████████████████ 3", v.Get(4))
assert.Equal(t, "abc", v.Get(5))
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestBargraphBadSubkeys(t *testing.T) {
bg.WriteFooter(0, "abc")

assert.Equal(t, " █ a", v.Get(0))
assert.Equal(t, "test █████████████████████████ 1", v.Get(1))
assert.Equal(t, "test ████████████████ 1", v.Get(1))
assert.Equal(t, "tes2 ████████████████▊ 1", v.Get(2))
assert.Equal(t, " ██████████████████████████████████████████████████ 3", v.Get(3))
assert.Equal(t, "abc", v.Get(4))
Expand All @@ -77,3 +77,8 @@ func TestBargraphUnicode(t *testing.T) {
assert.Equal(t, "✤✥✦a █████████████████████████████████████████████████ 3", v.Get(3))
assert.Equal(t, "abc", v.Get(4))
}

func TestMaxSumUtils(t *testing.T) {
assert.Equal(t, int64(5), maxi64(1, 2, 3, 5, 4))
assert.Equal(t, int64(15), sumi64(1, 2, 3, 5, 4))
}

0 comments on commit ac3dc37

Please sign in to comment.