forked from shieldproject/shield
/
report.go
48 lines (40 loc) · 840 Bytes
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package tui
import (
"fmt"
"io"
"strings"
)
type Report struct {
values [][]string
width int
}
func NewReport() Report {
return Report{}
}
func (r *Report) Add(key string, value string) {
if r.width < len(key) {
r.width = len(key)
}
v := strings.Split(value, "\n")
r.values = append(r.values, []string{key, v[0]})
for _, s := range v[1:] {
r.values = append(r.values, []string{"", s})
}
}
func (r *Report) Break() {
r.values = append(r.values, []string{"", ""})
}
func (r *Report) Output(out io.Writer) {
keyf := fmt.Sprintf("%%-%ds %%s\n", r.width+1)
blank := strings.Repeat(" ", r.width+2)
for _, p := range r.values {
if len(p) != 2 {
fmt.Printf("got the wrong num values\n")
}
if p[0] != "" {
fmt.Fprintf(out, keyf, p[0]+":", p[1])
} else {
fmt.Fprintf(out, "%s%s\n", blank, p[1])
}
}
}