Skip to content

Commit

Permalink
Fixes string count
Browse files Browse the repository at this point in the history
Resolves #35
  • Loading branch information
pbarnum authored and buger committed Jul 11, 2021
1 parent 597778c commit a95a51b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
11 changes: 6 additions & 5 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goterm
import (
"bytes"
"strings"
"unicode/utf8"
)

const DEFAULT_BORDER = "- │ ┌ ┐ └ ┘"
Expand Down Expand Up @@ -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

Expand All @@ -99,20 +99,21 @@ 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
}

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

out += line
Expand Down
18 changes: 18 additions & 0 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"strings"
"unicode/utf8"
)

const (
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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")
Expand Down

0 comments on commit a95a51b

Please sign in to comment.