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 authored and Silthus committed Aug 12, 2022
1 parent 5b212c7 commit ffecab3
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 @@ -156,16 +156,18 @@ 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)
}
t.printHeading()
if t.autoMergeCells {
t.printRowsMergeCells()
} else {
t.printRows()
dataLines = t.printRows()
}
if !t.rowLine && t.borders.Bottom {
t.printLine(true)
Expand All @@ -175,6 +177,8 @@ func (t *Table) Render() {
if t.caption {
t.printCaption()
}

return dataLines
}

const (
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ffecab3

Please sign in to comment.