From a95a51b6865dd622962f00b8817d9a770e07f5b5 Mon Sep 17 00:00:00 2001 From: Patrick Barnum Date: Sun, 18 Apr 2021 18:05:32 -0700 Subject: [PATCH] Fixes string count Resolves #35 --- box.go | 11 ++++++----- box_test.go | 18 ++++++++++++++++++ plot.go | 3 ++- plot_test.go | 14 +++++++------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/box.go b/box.go index 58e9bad..81881ca 100644 --- a/box.go +++ b/box.go @@ -3,6 +3,7 @@ package goterm import ( "bytes" "strings" + "unicode/utf8" ) const DEFAULT_BORDER = "- │ ┌ ┐ └ ┘" @@ -74,7 +75,6 @@ func (b *Box) String() (out string) { // Content width without borders and padding contentWidth := b.Width - (b.PaddingX+1)*2 - for y := 0; y < b.Height; y++ { var line string @@ -99,12 +99,13 @@ func (b *Box) String() (out string) { line = "" } - if len(line) > contentWidth-1 { + r := []rune(line) + if len(r) > contentWidth-1 { // If line is too large limit it - line = line[0:contentWidth] + line = string(r[0:contentWidth]) } else { // If line is too small enlarge it by adding spaces - line = line + strings.Repeat(" ", contentWidth-len(line)) + line += strings.Repeat(" ", contentWidth-utf8.RuneCountInString(line)) } line = prefix + line + suffix @@ -112,7 +113,7 @@ func (b *Box) String() (out string) { // Don't add newline for last element if y != b.Height-1 { - line = line + "\n" + line += "\n" } out += line diff --git a/box_test.go b/box_test.go index 18cd8fc..dd4dea9 100644 --- a/box_test.go +++ b/box_test.go @@ -22,3 +22,21 @@ func TestBox(t *testing.T) { t.Error(boxSample) } } + +func TestBox_WithUnicode(t *testing.T) { + boxSample := ` +┌--------┐ +│ hell☺ │ +│ w©rld │ +│ test✓✓ │ +└--------┘` + + box := NewBox(10, 5, 0) + fmt.Fprint(box, "hell☺\nw©rld\ntest✓✓") + + if box.String() != boxSample[1:] { + t.Error("\n" + box.String()) + t.Error("!=") + t.Error(boxSample) + } +} diff --git a/plot.go b/plot.go index 1201476..504358c 100644 --- a/plot.go +++ b/plot.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "strings" + "unicode/utf8" ) const ( @@ -119,7 +120,7 @@ func (c *LineChart) DrawAxes(maxX, minX, maxY, minY float64, index int) { c.writeText(ff(minX), c.paddingX, 0) x_col := c.data.columns[0] - c.writeText(c.data.columns[0], c.Width/2-len(x_col)/2, 1) + c.writeText(c.data.columns[0], c.Width/2-utf8.RuneCountInString(x_col)/2, 1) if c.Flags&DRAW_INDEPENDENT != 0 || len(c.data.columns) < 3 { col := c.data.columns[index] diff --git a/plot_test.go b/plot_test.go index fa0d63a..bdac81e 100644 --- a/plot_test.go +++ b/plot_test.go @@ -34,7 +34,7 @@ func TestCreateDataTable(t *testing.T) { } func TestLineChartIndependent(t *testing.T) { - fmt.Println("Independent charts\n") + fmt.Print("Independent charts\n\n") chart := NewLineChart(100, 20) chart.Flags = DRAW_INDEPENDENT //| DRAW_RELATIVE @@ -51,7 +51,7 @@ func TestLineChartIndependent(t *testing.T) { dataReversed.AddColumn("Lat") dataReversed.AddColumn("Count") - //data.AddColumn("x*x") + // data.AddColumn("x*x") for i := 0; i < 60; i++ { x := float64(i + 60) @@ -62,13 +62,13 @@ func TestLineChartIndependent(t *testing.T) { dataReversed.AddRow(x, y2, y1) } - // The two charts should look the same, only with inversed axes and colors + // The two charts should look the same, only with inverse axes and colors fmt.Println(chart.Draw(data)) fmt.Println(chartReversed.Draw(dataReversed)) } func TestLineChartRelative(t *testing.T) { - fmt.Println("Relative chart\n") + fmt.Print("Relative chart\n\n") chart := NewLineChart(100, 20) chart.Flags = DRAW_RELATIVE @@ -78,7 +78,7 @@ func TestLineChartRelative(t *testing.T) { data.AddColumn("Sin(x)") data.AddColumn("Cos(x+1)") - //data.AddColumn("x*x") + // data.AddColumn("x*x") for i := 0.1; i < 10; i += 0.1 { data.AddRow(i, math.Sin(i), math.Cos(i+1)) @@ -88,10 +88,10 @@ func TestLineChartRelative(t *testing.T) { } func TestLineChart(t *testing.T) { - fmt.Println("Simple chart\n") + fmt.Print("Simple chart\n\n") chart := NewLineChart(100, 20) - //chart.Flags = /*DRAW_INDEPENDENT // | */// DRAW_RELATIVE + // chart.Flags = /*DRAW_INDEPENDENT // | */// DRAW_RELATIVE data := new(DataTable) data.AddColumn("x")