Skip to content

Commit 20fca62

Browse files
committed
Reuse rowStyles
1 parent 71f7b39 commit 20fca62

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

encode.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type TableEncoder struct {
4343
// lineStyle is the table line style.
4444
lineStyle LineStyle
4545

46+
// rowStyles is the table of row style.
47+
rowStyles rowStyles
48+
4649
// formatter handles formatting values prior to output.
4750
formatter Formatter
4851

@@ -127,6 +130,12 @@ func NewTableEncoder(resultSet ResultSet, opts ...Option) (Encoder, error) {
127130
}
128131
}
129132

133+
enc.rowStyles.Top = enc.lineToRowStyle(enc.lineStyle.Top)
134+
enc.rowStyles.Mid = enc.lineToRowStyle(enc.lineStyle.Mid)
135+
enc.rowStyles.Row = enc.lineToRowStyle(enc.lineStyle.Row)
136+
enc.rowStyles.Wrap = enc.lineToRowStyle(enc.lineStyle.Wrap)
137+
enc.rowStyles.End = enc.lineToRowStyle(enc.lineStyle.End)
138+
130139
// check linestyle runes
131140
// TODO: this check should be removed
132141
for _, l := range [][4]rune{
@@ -256,7 +265,7 @@ func (enc *TableEncoder) Encode(w io.Writer) error {
256265

257266
// draw end border
258267
if enc.border >= 2 {
259-
enc.divider(enc.rowStyle(enc.lineStyle.End))
268+
enc.divider(enc.rowStyles.End)
260269
}
261270
}
262271

@@ -306,7 +315,7 @@ func (enc *TableEncoder) initBuffers() {
306315
}
307316

308317
func (enc *TableEncoder) encodeVals(vals [][]*Value) error {
309-
rs := enc.rowStyle(enc.lineStyle.Row)
318+
rs := enc.rowStyles.Row
310319
// print buffered vals
311320
for i := 0; i < len(vals); i++ {
312321
enc.row(vals[i], rs)
@@ -375,7 +384,7 @@ func (enc *TableEncoder) nextResults() ([][]*Value, error) {
375384
func (enc *TableEncoder) calcWidth(vals [][]*Value) {
376385
// calc offsets and widths for this batch of rows
377386
var offset int
378-
rs := enc.rowStyle(enc.lineStyle.Row)
387+
rs := enc.rowStyles.Row
379388
offset += runewidth.StringWidth(string(rs.left))
380389
for i, h := range enc.headers {
381390
if i != 0 {
@@ -406,7 +415,7 @@ func (enc *TableEncoder) calcWidth(vals [][]*Value) {
406415
}
407416

408417
func (enc *TableEncoder) header() {
409-
rs := enc.rowStyle(enc.lineStyle.Row)
418+
rs := enc.rowStyles.Row
410419

411420
if enc.title != nil && enc.title.Width != 0 {
412421
maxWidth := ((enc.tableWidth() - enc.title.Width) / 2) + enc.title.Width
@@ -415,20 +424,20 @@ func (enc *TableEncoder) header() {
415424
}
416425
// draw top border
417426
if enc.border >= 2 && !enc.inline {
418-
enc.divider(enc.rowStyle(enc.lineStyle.Top))
427+
enc.divider(enc.rowStyles.Top)
419428
}
420429

421430
// draw the header row with top border style
422431
if enc.inline {
423-
rs = enc.rowStyle(enc.lineStyle.Top)
432+
rs = enc.rowStyles.Top
424433
}
425434

426435
// write header
427436
enc.row(enc.headers, rs)
428437

429438
if !enc.inline {
430439
// draw mid divider
431-
enc.divider(enc.rowStyle(enc.lineStyle.Mid))
440+
enc.divider(enc.rowStyles.Mid)
432441
}
433442
}
434443

@@ -438,10 +447,14 @@ type rowStyle struct {
438447
hasWrapping bool
439448
}
440449

450+
type rowStyles struct {
451+
Top, Mid, Row, Wrap, End rowStyle
452+
}
453+
441454
// rowStyle returns the left, right and midle borders.
442455
// It also profides the filler string, and indicates
443456
// if this style uses a wrapping indicator.
444-
func (enc TableEncoder) rowStyle(r [4]rune) rowStyle {
457+
func (enc TableEncoder) lineToRowStyle(r [4]rune) rowStyle {
445458
var left, right, middle, spacer, filler string
446459
spacer = strings.Repeat(string(r[1]), runewidth.RuneWidth(enc.lineStyle.Row[1]))
447460
filler = string(r[1])
@@ -516,7 +529,7 @@ func (enc *TableEncoder) divider(rs rowStyle) {
516529

517530
// tableWidth calculates total table width.
518531
func (enc *TableEncoder) tableWidth() int {
519-
rs := enc.rowStyle(enc.lineStyle.Mid)
532+
rs := enc.rowStyles.Mid
520533
width := runewidth.StringWidth(string(rs.left)) + runewidth.StringWidth(string(rs.right))
521534

522535
for i, w := range enc.maxWidths {
@@ -818,7 +831,7 @@ func (enc *ExpandedEncoder) Encode(w io.Writer) error {
818831
}
819832

820833
func (enc *ExpandedEncoder) encodeVals(vals [][]*Value) error {
821-
rs := enc.rowStyle(enc.lineStyle.Row)
834+
rs := enc.rowStyles.Row
822835
// print buffered vals
823836
for i := 0; i < len(vals); i++ {
824837
enc.record(i, vals[i], rs)
@@ -837,7 +850,7 @@ func (enc *ExpandedEncoder) encodeVals(vals [][]*Value) error {
837850

838851
// draw end border
839852
if enc.border >= 2 && enc.scanCount != 0 {
840-
enc.divider(enc.rowStyle(enc.lineStyle.End))
853+
enc.divider(enc.rowStyles.End)
841854
}
842855
return nil
843856
}
@@ -868,7 +881,7 @@ func (enc *ExpandedEncoder) EncodeAll(w io.Writer) error {
868881
}
869882

870883
func (enc *ExpandedEncoder) calcWidth(vals [][]*Value) {
871-
rs := enc.rowStyle(enc.lineStyle.Row)
884+
rs := enc.rowStyles.Row
872885

873886
offset := runewidth.StringWidth(string(rs.left))
874887

@@ -939,9 +952,9 @@ func (enc *ExpandedEncoder) record(i int, vals []*Value, rs rowStyle) {
939952
headerRS := rs
940953
header := enc.recordHeader(i)
941954
if enc.border != 0 {
942-
headerRS = enc.rowStyle(enc.lineStyle.Top)
955+
headerRS = enc.rowStyles.Top
943956
if i != 0 {
944-
headerRS = enc.rowStyle(enc.lineStyle.Mid)
957+
headerRS = enc.rowStyles.Mid
945958
}
946959
}
947960

tblfmt_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package tblfmt
22

33
import (
44
"bytes"
5+
"encoding/csv"
56
"fmt"
7+
"io"
68
"io/ioutil"
79
"os"
810
"regexp"
@@ -283,6 +285,9 @@ func BenchmarkEncodeFormats(b *testing.B) {
283285
f Builder
284286
opts []Option
285287
}{
288+
{"unaligned", NewCSVEncoder, []Option{WithNewCSVWriter(func(w io.Writer) CSVWriter {
289+
return csv.NewWriter(w)
290+
})}},
286291
{"aligned", NewTableEncoder, nil},
287292
{"aligned-batch10", NewTableEncoder, []Option{WithCount(10)}},
288293
{"aligned-batch100", NewTableEncoder, []Option{WithCount(100)}},

0 commit comments

Comments
 (0)