@@ -91,28 +91,10 @@ func newSection(name string, contents ...tableContents) *section {
9191}
9292
9393func (s * section ) Emit (t * table ) {
94- empty := true
95-
9694 for _ , c := range s .contents {
97- subTable := t .subTable ()
95+ subTable := t .subTable (s . name )
9896 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 )
11698 }
11799}
118100
@@ -306,11 +288,12 @@ func (n *NameStyle) Type() string {
306288}
307289
308290type 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
314297}
315298
316299func TableString (contents tableContents , threshold Threshold , nameStyle NameStyle ) string {
@@ -330,12 +313,29 @@ func TableString(contents tableContents, threshold Threshold, nameStyle NameStyl
330313 return t .generateHeader () + t .buf .String () + t .footnotes .String ()
331314}
332315
333- func (t * table ) subTable () * table {
316+ func (t * table ) subTable (sectionHeader string ) * table {
334317 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 ())
339339 }
340340}
341341
0 commit comments