-
Notifications
You must be signed in to change notification settings - Fork 2
/
output.go
113 lines (92 loc) · 2.06 KB
/
output.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package prettycli
import (
"bytes"
"fmt"
"sort"
"strings"
"text/tabwriter"
)
type Output interface {
ToPrettyOutput() string
}
type ListOutput struct {
Labels []string
Rows []map[string]string
}
func (o *ListOutput) AddRow(m map[string]string) {
o.Rows = append(o.Rows, m)
}
func (o ListOutput) ToPrettyOutput() string {
b := bytes.NewBuffer([]byte{})
w := tabwriter.NewWriter(b, 0, 8, 2, '\t', 0)
// Print Heading Row
for _, l := range o.Labels {
fmt.Fprintf(w, "%v\t", strings.ToUpper(l))
}
fmt.Fprintln(w)
// Print Rows
for _, r := range o.Rows {
row := make([]string, len(o.Labels))
for i, l := range o.Labels {
row[i] = r[l]
}
fmt.Fprintf(w, "%s\n", strings.Join(row, "\t"))
}
w.Flush()
return strings.TrimSpace(b.String())
}
type PlainOutput struct {
Output string
}
func (o PlainOutput) ToPrettyOutput() string {
return o.Output
}
type DetailOutput struct {
Details map[string]string
Order []string
}
func (o DetailOutput) ToPrettyOutput() string {
b := bytes.NewBuffer([]byte{})
w := tabwriter.NewWriter(b, 0, 8, 2, '\t', 0)
var keys []string
for k := range o.Details {
shouldBeOrdered := true
for _, ok := range o.Order {
if k == ok {
shouldBeOrdered = false
}
}
if shouldBeOrdered {
keys = append(keys, k)
}
}
sort.Strings(keys)
for _, k := range append(o.Order, keys...) {
fmt.Fprintf(w, "%s\t%v\n", k, o.Details[k])
}
w.Flush()
return strings.TrimSpace(b.String())
}
type CombinedOutput struct {
Outputs []sectionedOutput
}
type sectionedOutput struct {
Heading string
Output Output
}
func (o *CombinedOutput) AddOutput(heading string, output Output) {
o.Outputs = append(o.Outputs, sectionedOutput{heading, output})
}
func (o *CombinedOutput) ToPrettyOutput() string {
outputStrs := make([]string, len(o.Outputs))
for i, out := range o.Outputs {
var s string
if out.Heading == "" {
s = out.Output.ToPrettyOutput()
} else {
s = fmt.Sprintf("%s\n%s", strings.ToUpper(out.Heading), out.Output.ToPrettyOutput())
}
outputStrs[i] = s
}
return strings.Join(outputStrs, "\n\n")
}