Skip to content

Commit 867c4dc

Browse files
committed
Footnotes: new class, extracted from table
1 parent 62e4d81 commit 867c4dc

2 files changed

Lines changed: 56 additions & 41 deletions

File tree

sizes/footnotes.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sizes
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
)
7+
8+
type Footnotes struct {
9+
footnotes []string
10+
indexes map[string]int
11+
}
12+
13+
func NewFootnotes() *Footnotes {
14+
return &Footnotes{
15+
indexes: make(map[string]int),
16+
}
17+
}
18+
19+
func (f *Footnotes) CreateCitation(footnote string) string {
20+
if footnote == "" {
21+
return ""
22+
}
23+
24+
index, ok := f.indexes[footnote]
25+
if !ok {
26+
index = len(f.indexes) + 1
27+
f.footnotes = append(f.footnotes, footnote)
28+
f.indexes[footnote] = index
29+
}
30+
return fmt.Sprintf("[%d]", index)
31+
}
32+
33+
func (f *Footnotes) String() string {
34+
if len(f.footnotes) == 0 {
35+
return ""
36+
}
37+
38+
buf := &bytes.Buffer{}
39+
buf.WriteByte('\n')
40+
for i, footnote := range f.footnotes {
41+
index := i + 1
42+
citation := fmt.Sprintf("[%d]", index)
43+
fmt.Fprintf(buf, "%-4s %s\n", citation, footnote)
44+
}
45+
return buf.String()
46+
}

sizes/output.go

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,10 @@ func (n *NameStyle) Type() string {
317317
}
318318

319319
type table struct {
320-
contents tableContents
321-
threshold Threshold
322-
nameStyle NameStyle
323-
footnotes []string
324-
footnoteIndexes map[string]int
320+
contents tableContents
321+
threshold Threshold
322+
nameStyle NameStyle
323+
footnotes *Footnotes
325324
}
326325

327326
func (s HistorySize) TableString(threshold Threshold, nameStyle NameStyle) string {
@@ -397,18 +396,17 @@ func (s HistorySize) TableString(threshold Threshold, nameStyle NameStyle) strin
397396
I("Number of submodules", s.MaxExpandedSubmoduleCountTree, s.MaxExpandedSubmoduleCount, metric, " ", 100),
398397
),
399398
),
400-
threshold: threshold,
401-
nameStyle: nameStyle,
402-
footnoteIndexes: make(map[string]int),
399+
threshold: threshold,
400+
nameStyle: nameStyle,
401+
footnotes: NewFootnotes(),
403402
}
404403

405404
return t.String()
406405
}
407406

408407
func (t *table) String() string {
409-
lines := t.generateLines()
410-
footnotes := t.generateFootnotes()
411-
return lines + footnotes
408+
linesString := t.generateLines()
409+
return linesString + t.footnotes.String()
412410
}
413411

414412
func (t *table) generateHeader() string {
@@ -436,7 +434,7 @@ func (t *table) formatRow(
436434
if indent != 0 {
437435
prefix = spaces[:2*(indent-1)] + "* "
438436
}
439-
citation := t.createCitation(footnote)
437+
citation := t.footnotes.CreateCitation(footnote)
440438
spacer := ""
441439
l := len(prefix) + len(name) + len(citation)
442440
if l < 28 {
@@ -447,32 +445,3 @@ func (t *table) formatRow(
447445
prefix, name, spacer, citation, valueString, unitString, levelOfConcern,
448446
)
449447
}
450-
451-
func (t *table) createCitation(footnote string) string {
452-
if footnote == "" {
453-
return ""
454-
}
455-
456-
index, ok := t.footnoteIndexes[footnote]
457-
if !ok {
458-
index = len(t.footnoteIndexes) + 1
459-
t.footnotes = append(t.footnotes, footnote)
460-
t.footnoteIndexes[footnote] = index
461-
}
462-
return fmt.Sprintf("[%d]", index)
463-
}
464-
465-
func (t *table) generateFootnotes() string {
466-
if len(t.footnotes) == 0 {
467-
return ""
468-
}
469-
470-
buf := &bytes.Buffer{}
471-
buf.WriteByte('\n')
472-
for i, footnote := range t.footnotes {
473-
index := i + 1
474-
citation := fmt.Sprintf("[%d]", index)
475-
fmt.Fprintf(buf, "%-4s %s\n", citation, footnote)
476-
}
477-
return buf.String()
478-
}

0 commit comments

Comments
 (0)