Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tabularize.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func (t *Table) Render(data [][]string) string {
sb.WriteString("|")
}
for col, cell := range rowdata {
sb.WriteString(t.padCell(cell, maxLengths[col]))
paddedCell := t.padCell(cell, maxLengths[col])
if col == len(maxLengths)-1 && !t.useTableBorders {
paddedCell = strings.TrimRight(paddedCell, " ")
}
sb.WriteString(paddedCell)
if t.useColumnSeparators && (col < len(maxLengths)-1 || t.useTableBorders) {
sb.WriteString("|")
} else if col < len(maxLengths)-1 {
Expand Down
28 changes: 23 additions & 5 deletions tabularize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestRender_NoBordersOrSeparators(t *testing.T) {
{"Alice", "30", "New York"},
{"Bob", "25", "Los Angeles"},
}
expected := `Name Age City
Alice 30 New York
expected := `Name Age City
Alice 30 New York
Bob 25 Los Angeles
`
table.useColumnSeparators = false
Expand Down Expand Up @@ -85,10 +85,10 @@ func TestRender_HeaderSeparator(t *testing.T) {
{"Alice", "30", "New York"},
{"Bob", "25", "Los Angeles"},
}
expected := ` Name | Age | City
expected := ` Name | Age | City
-------+-----+-------------
Alice | 30 | New York
Bob | 25 | Los Angeles
Alice | 30 | New York
Bob | 25 | Los Angeles
`
table.useTableBorders = false
table.useHeaderSeparator = true
Expand All @@ -97,3 +97,21 @@ func TestRender_HeaderSeparator(t *testing.T) {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, result)
}
}

func TestRender_UnpaddedFinalColumnWithoutBorders(t *testing.T) {
table := NewTable()
data := [][]string{
{"Name", "Note"},
{"Alice", ""},
{"Bob", "Hello"},
}
expected := ` Name | Note
Alice |
Bob | Hello
`
table.useHeaderSeparator = false
result := table.Render(data)
if result != expected {
t.Errorf("Expected:\n%q\nGot:\n%q", expected, result)
}
}
Loading