Skip to content

Commit

Permalink
table: fix column wrapping logic; fixes #170 (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Jul 7, 2021
1 parent 11849e4 commit d245a36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (t *Table) renderRow(out *strings.Builder, row rowStr, hint renderHint) {
// split each column into individual lines and render them one-by-one
if colMaxLines == 1 {
hint.isLastLineOfRow = true
t.renderLine(out, row, hint)
t.renderLine(out, rowWrapped, hint)
} else {
// convert one row into N # of rows based on colMaxLines
rowLines := make([]rowStr, len(row))
Expand Down
26 changes: 26 additions & 0 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,5 +1289,31 @@ func TestTable_Render_SetWidth_Title(t *testing.T) {

assert.Equal(t, strings.Join(expectedOut, "\n"), tw.Render())
})
}

func TestTable_Render_WidthEnforcer(t *testing.T) {
tw := NewWriter()
tw.AppendRows([]Row{
{"U2", "Hey", "2021-04-19 13:37", "Yuh yuh yuh"},
{"S12", "Uhhhh", "2021-04-19 13:37", "Some dummy data here"},
{"R123", "Lobsters", "2021-04-19 13:37", "I like lobsters"},
{"R123", "Some big name here and it's pretty big", "2021-04-19 13:37", "Abcdefghijklmnopqrstuvwxyz"},
{"R123", "Small name", "2021-04-19 13:37", "Abcdefghijklmnopqrstuvwxyz"},
})
tw.SetColumnConfigs([]ColumnConfig{
{Number: 2, WidthMax: 20, WidthMaxEnforcer: text.Trim},
})

expectedOut := `+------+----------------------+------------------+----------------------------+
| U2 | Hey | 2021-04-19 13:37 | Yuh yuh yuh |
| S12 | Uhhhh | 2021-04-19 13:37 | Some dummy data here |
| R123 | Lobsters | 2021-04-19 13:37 | I like lobsters |
| R123 | Some big name here a | 2021-04-19 13:37 | Abcdefghijklmnopqrstuvwxyz |
| R123 | Small name | 2021-04-19 13:37 | Abcdefghijklmnopqrstuvwxyz |
+------+----------------------+------------------+----------------------------+`
actualOut := tw.Render()
assert.Equal(t, expectedOut, actualOut)
if expectedOut != actualOut {
fmt.Println(actualOut)
}
}

0 comments on commit d245a36

Please sign in to comment.