From ffecab3d8ff2333895d2ae3a07874246f496d912 Mon Sep 17 00:00:00 2001 From: WestleyR Date: Thu, 12 Aug 2021 11:22:33 -0700 Subject: [PATCH] Return the number of data rows printed when rendering - Related to (would fix): https://github.com/olekukonko/tablewriter/issues/191 --- table.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/table.go b/table.go index a6b8a51..693bb6d 100644 --- a/table.go +++ b/table.go @@ -156,8 +156,10 @@ func (t *Table) RenderWithoutHeaders() { } } -// Render table output -func (t *Table) Render() { +// Render table output, returns the number of lines of data (includes any newline that the table wraps). +func (t *Table) Render() int { + dataLines := 0 + if t.borders.Top { t.printLine(true) } @@ -165,7 +167,7 @@ func (t *Table) Render() { if t.autoMergeCells { t.printRowsMergeCells() } else { - t.printRows() + dataLines = t.printRows() } if !t.rowLine && t.borders.Bottom { t.printLine(true) @@ -175,6 +177,8 @@ func (t *Table) Render() { if t.caption { t.printCaption() } + + return dataLines } const ( @@ -797,10 +801,13 @@ func (t Table) getTableWidth() int { return (chars + (3 * t.colSize) + 2) } -func (t Table) printRows() { +func (t Table) printRows() int { + rows := 0 for i, lines := range t.lines { - t.printRow(lines, i) + rows += t.printRow(lines, i) } + + return rows } func (t *Table) fillAlignment(num int) { @@ -815,7 +822,7 @@ func (t *Table) fillAlignment(num int) { // Print Row Information // Adjust column alignment based on type -func (t *Table) printRow(columns [][]string, rowIdx int) { +func (t *Table) printRow(columns [][]string, rowIdx int) int { // Get Maximum Height max := t.rs[rowIdx] total := len(columns) @@ -904,6 +911,8 @@ func (t *Table) printRow(columns [][]string, rowIdx int) { if t.rowLine { t.printLine(true) } + + return max } // Print the rows of the table and merge the cells that are identical