@@ -43,6 +43,9 @@ type TableEncoder struct {
43
43
// lineStyle is the table line style.
44
44
lineStyle LineStyle
45
45
46
+ // rowStyles is the table of row style.
47
+ rowStyles rowStyles
48
+
46
49
// formatter handles formatting values prior to output.
47
50
formatter Formatter
48
51
@@ -127,6 +130,12 @@ func NewTableEncoder(resultSet ResultSet, opts ...Option) (Encoder, error) {
127
130
}
128
131
}
129
132
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
+
130
139
// check linestyle runes
131
140
// TODO: this check should be removed
132
141
for _ , l := range [][4 ]rune {
@@ -256,7 +265,7 @@ func (enc *TableEncoder) Encode(w io.Writer) error {
256
265
257
266
// draw end border
258
267
if enc .border >= 2 {
259
- enc .divider (enc .rowStyle ( enc . lineStyle . End ) )
268
+ enc .divider (enc .rowStyles . End )
260
269
}
261
270
}
262
271
@@ -306,7 +315,7 @@ func (enc *TableEncoder) initBuffers() {
306
315
}
307
316
308
317
func (enc * TableEncoder ) encodeVals (vals [][]* Value ) error {
309
- rs := enc .rowStyle ( enc . lineStyle . Row )
318
+ rs := enc .rowStyles . Row
310
319
// print buffered vals
311
320
for i := 0 ; i < len (vals ); i ++ {
312
321
enc .row (vals [i ], rs )
@@ -375,7 +384,7 @@ func (enc *TableEncoder) nextResults() ([][]*Value, error) {
375
384
func (enc * TableEncoder ) calcWidth (vals [][]* Value ) {
376
385
// calc offsets and widths for this batch of rows
377
386
var offset int
378
- rs := enc .rowStyle ( enc . lineStyle . Row )
387
+ rs := enc .rowStyles . Row
379
388
offset += runewidth .StringWidth (string (rs .left ))
380
389
for i , h := range enc .headers {
381
390
if i != 0 {
@@ -406,7 +415,7 @@ func (enc *TableEncoder) calcWidth(vals [][]*Value) {
406
415
}
407
416
408
417
func (enc * TableEncoder ) header () {
409
- rs := enc .rowStyle ( enc . lineStyle . Row )
418
+ rs := enc .rowStyles . Row
410
419
411
420
if enc .title != nil && enc .title .Width != 0 {
412
421
maxWidth := ((enc .tableWidth () - enc .title .Width ) / 2 ) + enc .title .Width
@@ -415,20 +424,20 @@ func (enc *TableEncoder) header() {
415
424
}
416
425
// draw top border
417
426
if enc .border >= 2 && ! enc .inline {
418
- enc .divider (enc .rowStyle ( enc . lineStyle . Top ) )
427
+ enc .divider (enc .rowStyles . Top )
419
428
}
420
429
421
430
// draw the header row with top border style
422
431
if enc .inline {
423
- rs = enc .rowStyle ( enc . lineStyle . Top )
432
+ rs = enc .rowStyles . Top
424
433
}
425
434
426
435
// write header
427
436
enc .row (enc .headers , rs )
428
437
429
438
if ! enc .inline {
430
439
// draw mid divider
431
- enc .divider (enc .rowStyle ( enc . lineStyle . Mid ) )
440
+ enc .divider (enc .rowStyles . Mid )
432
441
}
433
442
}
434
443
@@ -438,10 +447,14 @@ type rowStyle struct {
438
447
hasWrapping bool
439
448
}
440
449
450
+ type rowStyles struct {
451
+ Top , Mid , Row , Wrap , End rowStyle
452
+ }
453
+
441
454
// rowStyle returns the left, right and midle borders.
442
455
// It also profides the filler string, and indicates
443
456
// 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 {
445
458
var left , right , middle , spacer , filler string
446
459
spacer = strings .Repeat (string (r [1 ]), runewidth .RuneWidth (enc .lineStyle .Row [1 ]))
447
460
filler = string (r [1 ])
@@ -516,7 +529,7 @@ func (enc *TableEncoder) divider(rs rowStyle) {
516
529
517
530
// tableWidth calculates total table width.
518
531
func (enc * TableEncoder ) tableWidth () int {
519
- rs := enc .rowStyle ( enc . lineStyle . Mid )
532
+ rs := enc .rowStyles . Mid
520
533
width := runewidth .StringWidth (string (rs .left )) + runewidth .StringWidth (string (rs .right ))
521
534
522
535
for i , w := range enc .maxWidths {
@@ -818,7 +831,7 @@ func (enc *ExpandedEncoder) Encode(w io.Writer) error {
818
831
}
819
832
820
833
func (enc * ExpandedEncoder ) encodeVals (vals [][]* Value ) error {
821
- rs := enc .rowStyle ( enc . lineStyle . Row )
834
+ rs := enc .rowStyles . Row
822
835
// print buffered vals
823
836
for i := 0 ; i < len (vals ); i ++ {
824
837
enc .record (i , vals [i ], rs )
@@ -837,7 +850,7 @@ func (enc *ExpandedEncoder) encodeVals(vals [][]*Value) error {
837
850
838
851
// draw end border
839
852
if enc .border >= 2 && enc .scanCount != 0 {
840
- enc .divider (enc .rowStyle ( enc . lineStyle . End ) )
853
+ enc .divider (enc .rowStyles . End )
841
854
}
842
855
return nil
843
856
}
@@ -868,7 +881,7 @@ func (enc *ExpandedEncoder) EncodeAll(w io.Writer) error {
868
881
}
869
882
870
883
func (enc * ExpandedEncoder ) calcWidth (vals [][]* Value ) {
871
- rs := enc .rowStyle ( enc . lineStyle . Row )
884
+ rs := enc .rowStyles . Row
872
885
873
886
offset := runewidth .StringWidth (string (rs .left ))
874
887
@@ -939,9 +952,9 @@ func (enc *ExpandedEncoder) record(i int, vals []*Value, rs rowStyle) {
939
952
headerRS := rs
940
953
header := enc .recordHeader (i )
941
954
if enc .border != 0 {
942
- headerRS = enc .rowStyle ( enc . lineStyle . Top )
955
+ headerRS = enc .rowStyles . Top
943
956
if i != 0 {
944
- headerRS = enc .rowStyle ( enc . lineStyle . Mid )
957
+ headerRS = enc .rowStyles . Mid
945
958
}
946
959
}
947
960
0 commit comments