Skip to content

Commit a95a51b

Browse files
pbarnumbuger
authored andcommitted
Fixes string count
Resolves #35
1 parent 597778c commit a95a51b

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

box.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goterm
33
import (
44
"bytes"
55
"strings"
6+
"unicode/utf8"
67
)
78

89
const DEFAULT_BORDER = "- │ ┌ ┐ └ ┘"
@@ -74,7 +75,6 @@ func (b *Box) String() (out string) {
7475

7576
// Content width without borders and padding
7677
contentWidth := b.Width - (b.PaddingX+1)*2
77-
7878
for y := 0; y < b.Height; y++ {
7979
var line string
8080

@@ -99,20 +99,21 @@ func (b *Box) String() (out string) {
9999
line = ""
100100
}
101101

102-
if len(line) > contentWidth-1 {
102+
r := []rune(line)
103+
if len(r) > contentWidth-1 {
103104
// If line is too large limit it
104-
line = line[0:contentWidth]
105+
line = string(r[0:contentWidth])
105106
} else {
106107
// If line is too small enlarge it by adding spaces
107-
line = line + strings.Repeat(" ", contentWidth-len(line))
108+
line += strings.Repeat(" ", contentWidth-utf8.RuneCountInString(line))
108109
}
109110

110111
line = prefix + line + suffix
111112
}
112113

113114
// Don't add newline for last element
114115
if y != b.Height-1 {
115-
line = line + "\n"
116+
line += "\n"
116117
}
117118

118119
out += line

box_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,21 @@ func TestBox(t *testing.T) {
2222
t.Error(boxSample)
2323
}
2424
}
25+
26+
func TestBox_WithUnicode(t *testing.T) {
27+
boxSample := `
28+
┌--------┐
29+
│ hell☺ │
30+
│ w©rld │
31+
│ test✓✓ │
32+
└--------┘`
33+
34+
box := NewBox(10, 5, 0)
35+
fmt.Fprint(box, "hell☺\nw©rld\ntest✓✓")
36+
37+
if box.String() != boxSample[1:] {
38+
t.Error("\n" + box.String())
39+
t.Error("!=")
40+
t.Error(boxSample)
41+
}
42+
}

plot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math"
66
"strings"
7+
"unicode/utf8"
78
)
89

910
const (
@@ -119,7 +120,7 @@ func (c *LineChart) DrawAxes(maxX, minX, maxY, minY float64, index int) {
119120
c.writeText(ff(minX), c.paddingX, 0)
120121

121122
x_col := c.data.columns[0]
122-
c.writeText(c.data.columns[0], c.Width/2-len(x_col)/2, 1)
123+
c.writeText(c.data.columns[0], c.Width/2-utf8.RuneCountInString(x_col)/2, 1)
123124

124125
if c.Flags&DRAW_INDEPENDENT != 0 || len(c.data.columns) < 3 {
125126
col := c.data.columns[index]

plot_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreateDataTable(t *testing.T) {
3434
}
3535

3636
func TestLineChartIndependent(t *testing.T) {
37-
fmt.Println("Independent charts\n")
37+
fmt.Print("Independent charts\n\n")
3838

3939
chart := NewLineChart(100, 20)
4040
chart.Flags = DRAW_INDEPENDENT //| DRAW_RELATIVE
@@ -51,7 +51,7 @@ func TestLineChartIndependent(t *testing.T) {
5151
dataReversed.AddColumn("Lat")
5252
dataReversed.AddColumn("Count")
5353

54-
//data.AddColumn("x*x")
54+
// data.AddColumn("x*x")
5555

5656
for i := 0; i < 60; i++ {
5757
x := float64(i + 60)
@@ -62,13 +62,13 @@ func TestLineChartIndependent(t *testing.T) {
6262
dataReversed.AddRow(x, y2, y1)
6363
}
6464

65-
// The two charts should look the same, only with inversed axes and colors
65+
// The two charts should look the same, only with inverse axes and colors
6666
fmt.Println(chart.Draw(data))
6767
fmt.Println(chartReversed.Draw(dataReversed))
6868
}
6969

7070
func TestLineChartRelative(t *testing.T) {
71-
fmt.Println("Relative chart\n")
71+
fmt.Print("Relative chart\n\n")
7272

7373
chart := NewLineChart(100, 20)
7474
chart.Flags = DRAW_RELATIVE
@@ -78,7 +78,7 @@ func TestLineChartRelative(t *testing.T) {
7878
data.AddColumn("Sin(x)")
7979
data.AddColumn("Cos(x+1)")
8080

81-
//data.AddColumn("x*x")
81+
// data.AddColumn("x*x")
8282

8383
for i := 0.1; i < 10; i += 0.1 {
8484
data.AddRow(i, math.Sin(i), math.Cos(i+1))
@@ -88,10 +88,10 @@ func TestLineChartRelative(t *testing.T) {
8888
}
8989

9090
func TestLineChart(t *testing.T) {
91-
fmt.Println("Simple chart\n")
91+
fmt.Print("Simple chart\n\n")
9292

9393
chart := NewLineChart(100, 20)
94-
//chart.Flags = /*DRAW_INDEPENDENT // | */// DRAW_RELATIVE
94+
// chart.Flags = /*DRAW_INDEPENDENT // | */// DRAW_RELATIVE
9595

9696
data := new(DataTable)
9797
data.AddColumn("x")

0 commit comments

Comments
 (0)