Skip to content

Commit

Permalink
table: restore some performance lost due to PR #66
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Oct 10, 2018
1 parent 93977ef commit 60a956e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -88,10 +88,10 @@ The unit-tests for each of the above show how these can be used.

Partial output of `make bench`:
```
BenchmarkList_Render-8 500000 2486 ns/op 872 B/op 47 allocs/op
BenchmarkProgress_Render-8 2 551831183 ns/op 7264 B/op 187 allocs/op
BenchmarkTable_Render-8 50000 31286 ns/op 7682 B/op 371 allocs/op
BenchmarkTable_RenderCSV-8 300000 4957 ns/op 2624 B/op 50 allocs/op
BenchmarkTable_RenderHTML-8 200000 6740 ns/op 4081 B/op 49 allocs/op
BenchmarkTable_RenderMarkdown-8 300000 5011 ns/op 2560 B/op 48 allocs/op
BenchmarkList_Render-8 500000 2491 ns/op 872 B/op 47 allocs/op
BenchmarkProgress_Render-8 2 551176831 ns/op 6512 B/op 184 allocs/op
BenchmarkTable_Render-8 100000 21528 ns/op 5698 B/op 193 allocs/op
BenchmarkTable_RenderCSV-8 300000 4828 ns/op 2624 B/op 50 allocs/op
BenchmarkTable_RenderHTML-8 200000 6688 ns/op 4081 B/op 49 allocs/op
BenchmarkTable_RenderMarkdown-8 300000 4972 ns/op 2560 B/op 48 allocs/op
```
93 changes: 43 additions & 50 deletions table/render.go
Expand Up @@ -106,49 +106,46 @@ func (t *Table) renderColumnAutoIndex(out *strings.Builder, hint renderHint) {
}

func (t *Table) renderColumnColorized(out *strings.Builder, colIdx int, colStr string, hint renderHint) {
colors := t.getColors(hint)
colors := t.getRowColors(hint)
if colIdx < len(colors) && colors[colIdx] != nil {
out.WriteString(colors[colIdx].Sprint(colStr))
} else if hint.isHeaderRow {
} else if hint.isHeaderRow && t.style.Color.Header != nil {
out.WriteString(t.style.Color.Header.Sprint(colStr))
} else if hint.isFooterRow {
} else if hint.isFooterRow && t.style.Color.Footer != nil {
out.WriteString(t.style.Color.Footer.Sprint(colStr))
} else if colIdx == t.indexColumn-1 {
out.WriteString(t.style.Color.IndexColumn.Sprint(colStr))
} else if hint.rowNumber%2 == 0 {
out.WriteString(t.style.Color.RowAlternate.Sprint(colStr))
} else if t.style.Color.Row != nil {
out.WriteString(t.style.Color.Row.Sprint(colStr))
} else if hint.isRegularRow() {
if colIdx == t.indexColumn-1 && t.style.Color.IndexColumn != nil {
out.WriteString(t.style.Color.IndexColumn.Sprint(colStr))
} else if hint.rowNumber%2 == 0 && t.style.Color.RowAlternate != nil {
out.WriteString(t.style.Color.RowAlternate.Sprint(colStr))
} else if t.style.Color.Row != nil {
out.WriteString(t.style.Color.Row.Sprint(colStr))
} else {
out.WriteString(colStr)
}
} else {
out.WriteString(colStr)
}
}

func (t *Table) renderColumnSeparator(out *strings.Builder, hint renderHint) {
if t.style.Options.SeparateColumns {
// colorize the separators too
colors := t.style.Color.Row
if hint.isHeaderRow {
colors = t.style.Color.Header
} else if hint.isFooterRow {
colors = t.style.Color.Footer
} else if hint.isAutoIndexColumn {
colors = t.style.Color.IndexColumn
} else if hint.rowNumber > 0 && hint.rowNumber%2 == 0 {
colors = t.style.Color.RowAlternate
}

// type of row determines the character used (top/bottom/separator)
separator := t.style.Box.MiddleVertical
if hint.isSeparatorRow {
if hint.isBorderTop {
out.WriteString(colors.Sprint(t.style.Box.TopSeparator))
separator = t.style.Box.TopSeparator
} else if hint.isBorderBottom {
out.WriteString(colors.Sprint(t.style.Box.BottomSeparator))
separator = t.style.Box.BottomSeparator
} else {
out.WriteString(colors.Sprint(t.style.Box.MiddleSeparator))
separator = t.style.Box.MiddleSeparator
}
}

colors := t.getSeparatorColors(hint)
if colors.EscapeSeq() != "" {
out.WriteString(colors.Sprint(separator))
} else {
out.WriteString(colors.Sprint(t.style.Box.MiddleVertical))
out.WriteString(separator)
}
}
}
Expand Down Expand Up @@ -207,44 +204,40 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {

func (t *Table) renderMarginLeft(out *strings.Builder, hint renderHint) {
if t.style.Options.DrawBorder {
// colorize the separators too
colors := t.style.Color.Header
if hint.isFooterRow {
colors = t.style.Color.Footer
} else if t.autoIndex {
colors = t.style.Color.IndexColumn
}

// type of row determines the character used (top/bottom/separator/etc.)
border := t.style.Box.Left
if hint.isBorderTop {
out.WriteString(colors.Sprint(t.style.Box.TopLeft))
border = t.style.Box.TopLeft
} else if hint.isBorderBottom {
out.WriteString(colors.Sprint(t.style.Box.BottomLeft))
border = t.style.Box.BottomLeft
} else if hint.isSeparatorRow {
out.WriteString(colors.Sprint(t.style.Box.LeftSeparator))
border = t.style.Box.LeftSeparator
}

colors := t.getBorderColors(hint)
if colors.EscapeSeq() != "" {
out.WriteString(colors.Sprint(border))
} else {
out.WriteString(colors.Sprint(t.style.Box.Left))
out.WriteString(border)
}
}
}

func (t *Table) renderMarginRight(out *strings.Builder, hint renderHint) {
if t.style.Options.DrawBorder {
// colorize the separators too
colors := t.style.Color.Header
if hint.isFooterRow {
colors = t.style.Color.Footer
}

// type of row determines the character used (top/bottom/separator/etc.)
border := t.style.Box.Right
if hint.isBorderTop {
out.WriteString(colors.Sprint(t.style.Box.TopRight))
border = t.style.Box.TopRight
} else if hint.isBorderBottom {
out.WriteString(colors.Sprint(t.style.Box.BottomRight))
border = t.style.Box.BottomRight
} else if hint.isSeparatorRow {
out.WriteString(colors.Sprint(t.style.Box.RightSeparator))
border = t.style.Box.RightSeparator
}

colors := t.getBorderColors(hint)
if colors.EscapeSeq() != "" {
out.WriteString(colors.Sprint(border))
} else {
out.WriteString(colors.Sprint(t.style.Box.Right))
out.WriteString(border)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion table/render_test.go
Expand Up @@ -33,7 +33,6 @@ func TestTable_Render(t *testing.T) {
[< >|< >|<TOTAL >|< 10000>|< >]
\-----v------------v-----------v--------v-----------------------------/
test-caption`
fmt.Println(tw.Render())
assert.Equal(t, expectedOut, tw.Render())
}

Expand Down
42 changes: 32 additions & 10 deletions table/table.go
Expand Up @@ -299,26 +299,26 @@ func (t *Table) getAutoIndexColumnIDs() rowStr {
return row
}

func (t *Table) getColors(hint renderHint) []text.Colors {
func (t *Table) getFormat(hint renderHint) text.Format {
if hint.isSeparatorRow {
return nil
return text.FormatDefault
} else if hint.isHeaderRow {
return t.colorsHeader
return t.style.Format.Header
} else if hint.isFooterRow {
return t.colorsFooter
return t.style.Format.Footer
}
return t.colors
return t.style.Format.Row
}

func (t *Table) getFormat(hint renderHint) text.Format {
func (t *Table) getRowColors(hint renderHint) []text.Colors {
if hint.isSeparatorRow {
return text.FormatDefault
return nil
} else if hint.isHeaderRow {
return t.style.Format.Header
return t.colorsHeader
} else if hint.isFooterRow {
return t.style.Format.Footer
return t.colorsFooter
}
return t.style.Format.Row
return t.colors
}

func (t *Table) getRowsSorted() []rowStr {
Expand All @@ -334,6 +334,28 @@ func (t *Table) getRowsSorted() []rowStr {
return sortedRows
}

func (t *Table) getBorderColors(hint renderHint) text.Colors {
if hint.isFooterRow {
return t.style.Color.Footer
} else if t.autoIndex {
return t.style.Color.IndexColumn
}
return t.style.Color.Header
}

func (t *Table) getSeparatorColors(hint renderHint) text.Colors {
if hint.isHeaderRow {
return t.style.Color.Header
} else if hint.isFooterRow {
return t.style.Color.Footer
} else if hint.isAutoIndexColumn {
return t.style.Color.IndexColumn
} else if hint.rowNumber > 0 && hint.rowNumber%2 == 0 {
return t.style.Color.RowAlternate
}
return t.style.Color.Row
}

func (t *Table) getVAlign(colIdx int, hint renderHint) text.VAlign {
vAlign := text.VAlignDefault
if hint.isHeaderRow {
Expand Down

0 comments on commit 60a956e

Please sign in to comment.