Skip to content

Commit 6a51f33

Browse files
committed
Store buf in table
Now that we have subtables, we can stage output there then only add it to the containing table if the output is non-empty.
1 parent b270dd6 commit 6a51f33

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

sizes/output.go

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sizes
33
import (
44
"bytes"
55
"fmt"
6-
"io"
76
"strconv"
87

98
"github.com/github/git-sizer/counts"
@@ -72,7 +71,7 @@ const (
7271

7372
// Zero or more lines in the tabular output.
7473
type tableContents interface {
75-
Emit(t *table, buf io.Writer)
74+
Emit(t *table)
7675
}
7776

7877
// A section of lines in the tabular output, consisting of a header
@@ -91,32 +90,30 @@ func newSection(name string, contents ...tableContents) *section {
9190
}
9291
}
9392

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+
9696
for _, c := range s.contents {
9797
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())
105115
}
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)
117116
}
118-
119-
fmt.Fprint(buf, linesBuf.String())
120117
}
121118

122119
// A line containing data in the tabular output.
@@ -147,14 +144,13 @@ func newItem(
147144
}
148145
}
149146

150-
func (l *item) Emit(t *table, buf io.Writer) {
147+
func (l *item) Emit(t *table) {
151148
levelOfConcern, interesting := l.levelOfConcern(t.threshold)
152149
if !interesting {
153150
return
154151
}
155152
valueString, unitString := l.value.Human(l.prefixes, l.unit)
156153
t.formatRow(
157-
buf,
158154
l.name, t.footnotes.CreateCitation(l.Footnote(t.nameStyle)),
159155
valueString, unitString,
160156
levelOfConcern,
@@ -314,6 +310,7 @@ type table struct {
314310
nameStyle NameStyle
315311
footnotes *Footnotes
316312
indent int
313+
buf bytes.Buffer
317314
}
318315

319316
func TableString(contents tableContents, threshold Threshold, nameStyle NameStyle) string {
@@ -324,14 +321,13 @@ func TableString(contents tableContents, threshold Threshold, nameStyle NameStyl
324321
indent: -1,
325322
}
326323

327-
buf := &bytes.Buffer{}
328-
contents.Emit(&t, buf)
324+
contents.Emit(&t)
329325

330-
if buf.Len() == 0 {
326+
if t.buf.Len() == 0 {
331327
return "No problems above the current threshold were found\n"
332328
}
333329

334-
return t.generateHeader() + buf.String() + t.footnotes.String()
330+
return t.generateHeader() + t.buf.String() + t.footnotes.String()
335331
}
336332

337333
func (t *table) subTable() *table {
@@ -350,16 +346,15 @@ func (t *table) generateHeader() string {
350346
return buf.String()
351347
}
352348

353-
func (t *table) emitBlankRow(buf io.Writer) {
354-
fmt.Fprintln(buf, "| | | |")
349+
func (t *table) emitBlankRow() {
350+
fmt.Fprintln(&t.buf, "| | | |")
355351
}
356352

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, "", "", "", "")
359355
}
360356

361357
func (t *table) formatRow(
362-
buf io.Writer,
363358
name, citation, valueString, unitString, levelOfConcern string,
364359
) {
365360
prefix := ""
@@ -372,7 +367,7 @@ func (t *table) formatRow(
372367
spacer = spaces[:28-l]
373368
}
374369
fmt.Fprintf(
375-
buf, "| %s%s%s%s | %5s %-3s | %-30s |\n",
370+
&t.buf, "| %s%s%s%s | %5s %-3s | %-30s |\n",
376371
prefix, name, spacer, citation, valueString, unitString, levelOfConcern,
377372
)
378373
}

0 commit comments

Comments
 (0)