Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the number of data rows printed when rendering #1

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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