From 60a956e5942070d379b1f69f48d9fca7ea22f774 Mon Sep 17 00:00:00 2001 From: JediB0T Date: Tue, 9 Oct 2018 17:37:17 -0700 Subject: [PATCH] table: restore some performance lost due to PR #66 --- README.md | 12 +++--- table/render.go | 93 ++++++++++++++++++++------------------------ table/render_test.go | 1 - table/table.go | 42 +++++++++++++++----- 4 files changed, 81 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index fcfe0d7..d4c7296 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/table/render.go b/table/render.go index 0d19427..7b936cc 100644 --- a/table/render.go +++ b/table/render.go @@ -106,19 +106,23 @@ 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) } @@ -126,29 +130,22 @@ func (t *Table) renderColumnColorized(out *strings.Builder, colIdx int, colStr s 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) } } } @@ -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) } } } diff --git a/table/render_test.go b/table/render_test.go index 4c45e4f..317b6b8 100644 --- a/table/render_test.go +++ b/table/render_test.go @@ -33,7 +33,6 @@ func TestTable_Render(t *testing.T) { [< >|< >||< 10000>|< >] \-----v------------v-----------v--------v-----------------------------/ test-caption` - fmt.Println(tw.Render()) assert.Equal(t, expectedOut, tw.Render()) } diff --git a/table/table.go b/table/table.go index c7c4ce3..064f912 100644 --- a/table/table.go +++ b/table/table.go @@ -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 { @@ -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 {