Skip to content

Commit

Permalink
Return the number of data rows printed when rendering
Browse files Browse the repository at this point in the history
 - Related to (would fix): olekukonko#191
  • Loading branch information
WestleyR committed Aug 12, 2021
1 parent 74c60be commit 8a85d9b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,18 @@ func NewWriter(writer io.Writer) *Table {
return t
}

// 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)
}
t.printHeading()
if t.autoMergeCells {
t.printRowsMergeCells()
} else {
t.printRows()
dataLines = t.printRows()
}
if !t.rowLine && t.borders.Bottom {
t.printLine(true)
Expand All @@ -142,6 +144,8 @@ func (t *Table) Render() {
if t.caption {
t.printCaption()
}

return dataLines
}

const (
Expand Down Expand Up @@ -764,10 +768,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) {
Expand All @@ -782,7 +789,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)
Expand Down Expand Up @@ -871,6 +878,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
Expand Down

0 comments on commit 8a85d9b

Please sign in to comment.