@@ -91,28 +91,10 @@ func newSection(name string, contents ...tableContents) *section {
91
91
}
92
92
93
93
func (s * section ) Emit (t * table ) {
94
- empty := true
95
-
96
94
for _ , c := range s .contents {
97
- subTable := t .subTable ()
95
+ subTable := t .subTable (s . name )
98
96
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 ())
115
- }
97
+ t .addSection (subTable )
116
98
}
117
99
}
118
100
@@ -306,11 +288,12 @@ func (n *NameStyle) Type() string {
306
288
}
307
289
308
290
type table struct {
309
- threshold Threshold
310
- nameStyle NameStyle
311
- footnotes * Footnotes
312
- indent int
313
- buf bytes.Buffer
291
+ threshold Threshold
292
+ nameStyle NameStyle
293
+ sectionHeader string
294
+ footnotes * Footnotes
295
+ indent int
296
+ buf bytes.Buffer
314
297
}
315
298
316
299
func TableString (contents tableContents , threshold Threshold , nameStyle NameStyle ) string {
@@ -330,12 +313,29 @@ func TableString(contents tableContents, threshold Threshold, nameStyle NameStyl
330
313
return t .generateHeader () + t .buf .String () + t .footnotes .String ()
331
314
}
332
315
333
- func (t * table ) subTable () * table {
316
+ func (t * table ) subTable (sectionHeader string ) * table {
334
317
return & table {
335
- threshold : t .threshold ,
336
- nameStyle : t .nameStyle ,
337
- footnotes : t .footnotes ,
338
- indent : t .indent + 1 ,
318
+ threshold : t .threshold ,
319
+ nameStyle : t .nameStyle ,
320
+ sectionHeader : sectionHeader ,
321
+ footnotes : t .footnotes ,
322
+ indent : t .indent + 1 ,
323
+ }
324
+ }
325
+
326
+ func (t * table ) addSection (subTable * table ) {
327
+ if subTable .buf .Len () > 0 {
328
+ if t .buf .Len () == 0 {
329
+ // Add the section title:
330
+ if subTable .sectionHeader != "" {
331
+ t .formatSectionHeader (subTable .sectionHeader )
332
+ }
333
+ } else if t .indent == - 1 {
334
+ // The top-level section gets blank lines between its
335
+ // subsections:
336
+ t .emitBlankRow ()
337
+ }
338
+ fmt .Fprint (& t .buf , subTable .buf .String ())
339
339
}
340
340
}
341
341
0 commit comments