Skip to content

Commit

Permalink
table: allowed column length should behave like "max" column length (#54
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jedib0t committed Aug 14, 2018
1 parent 9ff3b65 commit 23efe15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
19 changes: 11 additions & 8 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,9 @@ func (t *Table) initForRenderMaxColumnLength() {
var findMaxColumnLengths = func(rows []rowStr) {
for _, row := range rows {
for colIdx, colStr := range row {
allowedColumnLength := t.getAllowedColumnLength(colIdx)
if allowedColumnLength > 0 {
t.maxColumnLengths[colIdx] = allowedColumnLength
} else {
colLongestLineLength := util.GetLongestLineLength(colStr)
if colLongestLineLength > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = colLongestLineLength
}
longestLineLen := util.GetLongestLineLength(colStr)
if longestLineLen > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = longestLineLen
}
}
}
Expand All @@ -384,6 +379,14 @@ func (t *Table) initForRenderMaxColumnLength() {
findMaxColumnLengths(t.rowsHeader)
findMaxColumnLengths(t.rows)
findMaxColumnLengths(t.rowsFooter)

// restrict the column lengths if any are overthe allowed lengths
for colIdx := range t.maxColumnLengths {
allowedLen := t.getAllowedColumnLength(colIdx)
if allowedLen > 0 && t.maxColumnLengths[colIdx] > allowedLen {
t.maxColumnLengths[colIdx] = allowedLen
}
}
}

func (t *Table) initForRenderRowSeparator() {
Expand Down
12 changes: 12 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ func TestTable_SetAllowedColumnLengths(t *testing.T) {
\-----v---v----v-----v---------/`
assert.Equal(t, []int{0, 1, 2, 3, 7}, table.allowedColumnLengths)
assert.Equal(t, expectedOut, table.Render())

table.SetAllowedColumnLengths([]int{100, 100, 100, 100, 7})
expectedOut = `(-----^--------^-----------^------^---------)
[< 1>|<Arya >|<Stark >|<3000>|< >]
[< 20>|<Jon >|<Snow >|<2000>|<You kno>]
[< >|< >|< >|< >|<w nothi>]
[< >|< >|< >|< >|<ng, Jon>]
[< >|< >|< >|< >|< Snow! >]
[<300>|<Tyrion>|<Lannister>|<5000>|< >]
\-----v--------v-----------v------v---------/`
assert.Equal(t, []int{100, 100, 100, 100, 7}, table.allowedColumnLengths)
assert.Equal(t, expectedOut, table.Render())
}

func TestTable_SetAllowedRowLength(t *testing.T) {
Expand Down

0 comments on commit 23efe15

Please sign in to comment.