Skip to content

Commit

Permalink
Merge pull request olekukonko#4 from papadopa/master
Browse files Browse the repository at this point in the history
TH
  • Loading branch information
olekukonko committed Jun 19, 2014
2 parents a45d3e5 + 465bc9f commit 6ff773c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
12 changes: 8 additions & 4 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func (t *table) AppendBulk(rows [][]string) (err error) {
// Print line based on row width
func (t table) printLine(nl bool) {
fmt.Fprint(t.out, t.pCenter)
for _, v := range t.cs {
for i := 0; i < len(t.cs); i++ {
v := t.cs[i]
fmt.Fprintf(t.out, "%s%s%s%s",
t.pRow,
strings.Repeat(string(t.pRow), v),
Expand All @@ -218,7 +219,8 @@ func (t table) printHeading() {
end := len(t.cs) - 1

// Print Heading column
for i, v := range t.cs {
for i := 0; i <= end; i++ {
v := t.cs[i]
pad := ConditionString((i == end && !t.border), SPACE, t.pColumn)
fmt.Fprintf(t.out, " %s %s",
Pad(t.headers[i], SPACE, v),
Expand Down Expand Up @@ -248,7 +250,8 @@ func (t table) printFooter() {
end := len(t.cs) - 1

// Print Heading column
for i, v := range t.cs {
for i := 0; i <= end; i++ {
v := t.cs[i]
pad := ConditionString((i == end && !t.border), SPACE, t.pColumn)

if len(t.footers[i]) == 0 {
Expand All @@ -264,7 +267,8 @@ func (t table) printFooter() {

hasPrinted := false

for i, v := range t.cs {
for i := 0; i <= end; i++ {
v := t.cs[i]
pad := t.pRow
center := t.pCenter
length := len(t.footers[i])
Expand Down
52 changes: 52 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
package tablewriter

import (
"bytes"
"fmt"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -98,3 +101,52 @@ func TestBorder(t *testing.T) {
table.AppendBulk(data) // Add Bulk Data
table.Render()
}

func TestPrintHeading(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.printHeading()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("header rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}

func TestPrintFooter(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.SetFooter([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.printFooter()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("footer rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}

func TestPrintLine(t *testing.T) {
header := make([]string, 12)
val := " "
want := ""
for i := range header {
header[i] = val
want = fmt.Sprintf("%s+-%s-", want, strings.Replace(val, " ", "-", -1))
val = val + " "
}
want = want + "+"
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader(header)
table.printLine(false)
got := buf.String()
if got != want {
t.Errorf("line rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}

0 comments on commit 6ff773c

Please sign in to comment.