-
Notifications
You must be signed in to change notification settings - Fork 3
/
printing.go
128 lines (106 loc) · 2.69 KB
/
printing.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package bitflow
import (
"bytes"
"fmt"
"strings"
)
// String is a trivial implementation of the fmt.Stringer interface
type String string
func (s String) String() string {
return string(s)
}
// ====================== Sorting ======================
type SortedStringers []fmt.Stringer
func (t SortedStringers) Len() int {
return len(t)
}
func (t SortedStringers) Less(a, b int) bool {
return t[a].String() < t[b].String()
}
func (t SortedStringers) Swap(a, b int) {
t[a], t[b] = t[b], t[a]
}
type SortedStringPairs struct {
Keys []string
Values []string
}
func (s *SortedStringPairs) FillFromMap(values map[string]string) {
s.Keys = make([]string, 0, len(values))
s.Values = make([]string, 0, len(values))
for key, value := range values {
s.Keys = append(s.Keys, key)
s.Values = append(s.Values, value)
}
}
func (s *SortedStringPairs) Len() int {
return len(s.Keys)
}
func (s *SortedStringPairs) Less(i, j int) bool {
return s.Keys[i] < s.Keys[j]
}
func (s *SortedStringPairs) Swap(i, j int) {
s.Keys[i], s.Keys[j] = s.Keys[j], s.Keys[i]
s.Values[i], s.Values[j] = s.Values[j], s.Values[i]
}
func (s *SortedStringPairs) String() string {
var buf bytes.Buffer
for i, key := range s.Keys {
if i > 0 {
buf.WriteString(" ")
}
fmt.Fprintf(&buf, "%v=%v", key, s.Values[i])
}
return buf.String()
}
// ====================== Printing ======================
type IndentPrinter struct {
OuterIndent string
InnerIndent string
FillerIndent string
CornerIndent string
MarkEmptyContainers bool
}
type StringerContainer interface {
ContainedStringers() []fmt.Stringer
}
func (p IndentPrinter) Print(obj fmt.Stringer) string {
return strings.Join(p.PrintLines(obj), "\n")
}
func (p IndentPrinter) PrintLines(obj fmt.Stringer) []string {
return p.printLines(obj, "", "")
}
func (p IndentPrinter) printLines(obj fmt.Stringer, headerIndent, childIndent string) []string {
str := headerIndent
if obj == nil {
str += "(nil)"
} else {
str += obj.String()
}
res := []string{str}
if container, ok := obj.(StringerContainer); ok {
parts := container.ContainedStringers()
if len(parts) == 0 && p.MarkEmptyContainers {
parts = append(parts, String("empty"))
}
for i, part := range parts {
var nextHeader, nextChild string
if i == len(parts)-1 {
nextHeader = p.CornerIndent
nextChild = p.FillerIndent
} else {
nextHeader = p.InnerIndent
nextChild = p.OuterIndent
}
partLines := p.printLines(part, childIndent+nextHeader, childIndent+nextChild)
res = append(res, partLines...)
}
}
return res
}
type TitledSamplePipeline struct {
*SamplePipeline
Title string
}
func (t *TitledSamplePipeline) String() string {
return t.Title
}