@@ -3,7 +3,6 @@ package sizes
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
- "io"
7
6
"strconv"
8
7
9
8
"github.com/github/git-sizer/counts"
@@ -72,7 +71,7 @@ const (
72
71
73
72
// Zero or more lines in the tabular output.
74
73
type tableContents interface {
75
- Emit (t * table , buf io. Writer )
74
+ Emit (t * table )
76
75
}
77
76
78
77
// A section of lines in the tabular output, consisting of a header
@@ -91,32 +90,30 @@ func newSection(name string, contents ...tableContents) *section {
91
90
}
92
91
}
93
92
94
- func (s * section ) Emit (t * table , buf io.Writer ) {
95
- var linesBuf bytes.Buffer
93
+ func (s * section ) Emit (t * table ) {
94
+ empty := true
95
+
96
96
for _ , c := range s .contents {
97
97
subTable := t .subTable ()
98
- var cBuf bytes.Buffer
99
- c .Emit (subTable , & cBuf )
100
-
101
- if t .indent == - 1 && linesBuf .Len () > 0 && cBuf .Len () > 0 {
102
- // The top-level section emits blank lines between its
103
- // subsections:
104
- subTable .emitBlankRow (& linesBuf )
98
+ c .Emit (subTable )
99
+
100
+ if subTable .buf .Len () > 0 {
101
+ if empty {
102
+ // Add the section title:
103
+ if s .name != "" {
104
+ t .formatSectionHeader (s .name )
105
+ }
106
+ empty = false
107
+ } else {
108
+ // The top-level section emits blank lines between its
109
+ // subsections:
110
+ if t .indent == - 1 {
111
+ t .emitBlankRow ()
112
+ }
113
+ }
114
+ fmt .Fprint (& t .buf , subTable .buf .String ())
105
115
}
106
-
107
- fmt .Fprint (& linesBuf , cBuf .String ())
108
- }
109
-
110
- if linesBuf .Len () == 0 {
111
- return
112
- }
113
-
114
- // There's output, so emit the section header first:
115
- if s .name != "" {
116
- t .formatSectionHeader (buf , s .name )
117
116
}
118
-
119
- fmt .Fprint (buf , linesBuf .String ())
120
117
}
121
118
122
119
// A line containing data in the tabular output.
@@ -147,14 +144,13 @@ func newItem(
147
144
}
148
145
}
149
146
150
- func (l * item ) Emit (t * table , buf io. Writer ) {
147
+ func (l * item ) Emit (t * table ) {
151
148
levelOfConcern , interesting := l .levelOfConcern (t .threshold )
152
149
if ! interesting {
153
150
return
154
151
}
155
152
valueString , unitString := l .value .Human (l .prefixes , l .unit )
156
153
t .formatRow (
157
- buf ,
158
154
l .name , t .footnotes .CreateCitation (l .Footnote (t .nameStyle )),
159
155
valueString , unitString ,
160
156
levelOfConcern ,
@@ -314,6 +310,7 @@ type table struct {
314
310
nameStyle NameStyle
315
311
footnotes * Footnotes
316
312
indent int
313
+ buf bytes.Buffer
317
314
}
318
315
319
316
func TableString (contents tableContents , threshold Threshold , nameStyle NameStyle ) string {
@@ -324,14 +321,13 @@ func TableString(contents tableContents, threshold Threshold, nameStyle NameStyl
324
321
indent : - 1 ,
325
322
}
326
323
327
- buf := & bytes.Buffer {}
328
- contents .Emit (& t , buf )
324
+ contents .Emit (& t )
329
325
330
- if buf .Len () == 0 {
326
+ if t . buf .Len () == 0 {
331
327
return "No problems above the current threshold were found\n "
332
328
}
333
329
334
- return t .generateHeader () + buf .String () + t .footnotes .String ()
330
+ return t .generateHeader () + t . buf .String () + t .footnotes .String ()
335
331
}
336
332
337
333
func (t * table ) subTable () * table {
@@ -350,16 +346,15 @@ func (t *table) generateHeader() string {
350
346
return buf .String ()
351
347
}
352
348
353
- func (t * table ) emitBlankRow (buf io. Writer ) {
354
- fmt .Fprintln (buf , "| | | |" )
349
+ func (t * table ) emitBlankRow () {
350
+ fmt .Fprintln (& t . buf , "| | | |" )
355
351
}
356
352
357
- func (t * table ) formatSectionHeader (buf io. Writer , name string ) {
358
- t .formatRow (buf , name , "" , "" , "" , "" )
353
+ func (t * table ) formatSectionHeader (name string ) {
354
+ t .formatRow (name , "" , "" , "" , "" )
359
355
}
360
356
361
357
func (t * table ) formatRow (
362
- buf io.Writer ,
363
358
name , citation , valueString , unitString , levelOfConcern string ,
364
359
) {
365
360
prefix := ""
@@ -372,7 +367,7 @@ func (t *table) formatRow(
372
367
spacer = spaces [:28 - l ]
373
368
}
374
369
fmt .Fprintf (
375
- buf , "| %s%s%s%s | %5s %-3s | %-30s |\n " ,
370
+ & t . buf , "| %s%s%s%s | %5s %-3s | %-30s |\n " ,
376
371
prefix , name , spacer , citation , valueString , unitString , levelOfConcern ,
377
372
)
378
373
}
0 commit comments