Skip to content

Commit b270dd6

Browse files
committed
table: store the indent in the table itself
Format subsections using subtables that have progressively increasing indents.
1 parent d41f19a commit b270dd6

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

sizes/output.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const (
7272

7373
// Zero or more lines in the tabular output.
7474
type tableContents interface {
75-
Emit(t *table, buf io.Writer, indent int)
75+
Emit(t *table, buf io.Writer)
7676
}
7777

7878
// A section of lines in the tabular output, consisting of a header
@@ -91,16 +91,17 @@ func newSection(name string, contents ...tableContents) *section {
9191
}
9292
}
9393

94-
func (s *section) Emit(t *table, buf io.Writer, indent int) {
94+
func (s *section) Emit(t *table, buf io.Writer) {
9595
var linesBuf bytes.Buffer
9696
for _, c := range s.contents {
97+
subTable := t.subTable()
9798
var cBuf bytes.Buffer
98-
c.Emit(t, &cBuf, indent+1)
99+
c.Emit(subTable, &cBuf)
99100

100-
if indent == -1 && linesBuf.Len() > 0 && cBuf.Len() > 0 {
101+
if t.indent == -1 && linesBuf.Len() > 0 && cBuf.Len() > 0 {
101102
// The top-level section emits blank lines between its
102103
// subsections:
103-
t.emitBlankRow(&linesBuf)
104+
subTable.emitBlankRow(&linesBuf)
104105
}
105106

106107
fmt.Fprint(&linesBuf, cBuf.String())
@@ -112,7 +113,7 @@ func (s *section) Emit(t *table, buf io.Writer, indent int) {
112113

113114
// There's output, so emit the section header first:
114115
if s.name != "" {
115-
t.formatSectionHeader(buf, indent, s.name)
116+
t.formatSectionHeader(buf, s.name)
116117
}
117118

118119
fmt.Fprint(buf, linesBuf.String())
@@ -146,14 +147,14 @@ func newItem(
146147
}
147148
}
148149

149-
func (l *item) Emit(t *table, buf io.Writer, indent int) {
150+
func (l *item) Emit(t *table, buf io.Writer) {
150151
levelOfConcern, interesting := l.levelOfConcern(t.threshold)
151152
if !interesting {
152153
return
153154
}
154155
valueString, unitString := l.value.Human(l.prefixes, l.unit)
155156
t.formatRow(
156-
buf, indent,
157+
buf,
157158
l.name, t.footnotes.CreateCitation(l.Footnote(t.nameStyle)),
158159
valueString, unitString,
159160
levelOfConcern,
@@ -312,17 +313,19 @@ type table struct {
312313
threshold Threshold
313314
nameStyle NameStyle
314315
footnotes *Footnotes
316+
indent int
315317
}
316318

317319
func TableString(contents tableContents, threshold Threshold, nameStyle NameStyle) string {
318320
t := table{
319321
threshold: threshold,
320322
nameStyle: nameStyle,
321323
footnotes: NewFootnotes(),
324+
indent: -1,
322325
}
323326

324327
buf := &bytes.Buffer{}
325-
contents.Emit(&t, buf, -1)
328+
contents.Emit(&t, buf)
326329

327330
if buf.Len() == 0 {
328331
return "No problems above the current threshold were found\n"
@@ -331,6 +334,15 @@ func TableString(contents tableContents, threshold Threshold, nameStyle NameStyl
331334
return t.generateHeader() + buf.String() + t.footnotes.String()
332335
}
333336

337+
func (t *table) subTable() *table {
338+
return &table{
339+
threshold: t.threshold,
340+
nameStyle: t.nameStyle,
341+
footnotes: t.footnotes,
342+
indent: t.indent + 1,
343+
}
344+
}
345+
334346
func (t *table) generateHeader() string {
335347
buf := &bytes.Buffer{}
336348
fmt.Fprintln(buf, "| Name | Value | Level of concern |")
@@ -339,20 +351,20 @@ func (t *table) generateHeader() string {
339351
}
340352

341353
func (t *table) emitBlankRow(buf io.Writer) {
342-
t.formatRow(buf, 0, "", "", "", "", "")
354+
fmt.Fprintln(buf, "| | | |")
343355
}
344356

345-
func (t *table) formatSectionHeader(buf io.Writer, indent int, name string) {
346-
t.formatRow(buf, indent, name, "", "", "", "")
357+
func (t *table) formatSectionHeader(buf io.Writer, name string) {
358+
t.formatRow(buf, name, "", "", "", "")
347359
}
348360

349361
func (t *table) formatRow(
350-
buf io.Writer, indent int,
362+
buf io.Writer,
351363
name, citation, valueString, unitString, levelOfConcern string,
352364
) {
353365
prefix := ""
354-
if indent != 0 {
355-
prefix = spaces[:2*(indent-1)] + "* "
366+
if t.indent != 0 {
367+
prefix = spaces[:2*(t.indent-1)] + "* "
356368
}
357369
spacer := ""
358370
l := len(prefix) + len(name) + len(citation)

0 commit comments

Comments
 (0)