-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.go
221 lines (203 loc) · 5.69 KB
/
text.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package report
import (
"fmt"
"strconv"
"strings"
"github.com/rothskeller/wppsvr/english"
)
const spaces = " "
// RenderPlainText renders a report in plain text format.
func (r *Report) RenderPlainText() string {
var sb strings.Builder
r.plainTextTitle(&sb)
r.plainTextExpectsResults(&sb)
r.plainTextMessages(&sb)
r.plainTextStatistics(&sb)
r.plainTextGenInfo(&sb)
return sb.String()
}
func (r *Report) plainTextTitle(sb *strings.Builder) {
fmt.Fprintf(sb, "==== SCCo ARES/RACES Packet Practice Report\n==== for %s on %s", r.SessionName, r.SessionDate)
if r.Preliminary {
sb.WriteString(" (PRELIMINARY)")
}
if r.UniqueCallSignsWeek != 0 {
fmt.Fprintf(sb, "\n\n%d unique call signs (%d for the week)\n\n", r.UniqueCallSigns, r.UniqueCallSignsWeek)
} else if r.UniqueCallSigns != 0 {
fmt.Fprintf(sb, "\n\n%d unique call signs\n\n", r.UniqueCallSigns)
} else {
sb.WriteString("\n\n")
}
}
func (r *Report) plainTextExpectsResults(sb *strings.Builder) {
var wrapper = english.NewWrapper(sb)
var hasModel string
if r.HasModel {
hasModel = "copy of model "
}
fmt.Fprintf(wrapper, "EXPECTATIONS: %s%s sent to %s between %s and %s",
hasModel, english.Conjoin(r.MessageTypes, "or"), r.SentTo, r.SentAfter, r.SentBefore)
if r.NotSentFrom != "" {
fmt.Fprintf(wrapper, "; not sent from %s", r.NotSentFrom)
}
wrapper.WriteString(".")
if r.Modified {
wrapper.WriteString(" Expectations were modified during session; some early messages may have been evaluated against different expectations.")
}
wrapper.WriteString("\n\n")
wrapper.Close()
sb.WriteString("---- RESULTS\n")
if r.ValidCount+r.InvalidCount+r.ReplacedCount+r.DroppedCount != 0 {
var lines, col1, col2 []string
if r.ValidCount != 0 {
col1 = append(col1, strconv.Itoa(r.AverageValidScore), strconv.Itoa(r.ValidCount))
col2 = append(col2, "Average Score", "Counted")
}
if r.InvalidCount != 0 {
col1 = append(col1, strconv.Itoa(r.InvalidCount))
col2 = append(col2, "Not Counted")
}
if r.ReplacedCount != 0 {
col1 = append(col1, strconv.Itoa(r.ReplacedCount))
col2 = append(col2, "Duplicate")
}
if r.DroppedCount != 0 {
col1 = append(col1, strconv.Itoa(r.DroppedCount))
col2 = append(col2, "Delivery receipt")
}
rightAlign(col1)
if r.ValidCount != 0 {
col1[0] += "%"
}
lines = sideBySide(col1, col2, 1)
for _, line := range lines {
sb.WriteString(line)
sb.WriteByte('\n')
}
} else {
sb.WriteString("0 Messages")
}
sb.WriteByte('\n')
}
func (r *Report) plainTextMessages(sb *strings.Builder) {
var col1, col2, col3, col4, col5 []string
var hasMultiple bool
if len(r.Messages) == 0 {
return
}
sb.WriteString("---- MESSAGES\n")
for _, m := range r.Messages {
var multiple string
col1 = append(col1, m.Prefix)
col2 = append(col2, m.Suffix)
if m.Multiple {
multiple, hasMultiple = `*`, true
}
col3 = append(col3, "@"+m.Source+multiple)
col4 = append(col4, "("+m.Jurisdiction+")")
col5 = append(col5, fmt.Sprintf("%3d%% %s", m.Score, m.Summary))
}
rightAlign(col1)
col1 = sideBySide(col1, col2, 0)
col1 = sideBySide(col1, col3, 2)
col1 = sideBySide(col1, col4, 2)
col1 = sideBySide(col1, col5, 2)
for _, line := range col1 {
sb.WriteString(line)
sb.WriteByte('\n')
}
if hasMultiple {
sb.WriteString("* multiple messages from this address; only the last one counts\n")
}
sb.WriteString("\n")
}
func (r *Report) plainTextStatistics(sb *strings.Builder) {
if len(r.Sources) != 0 {
var lines, col1, col2 []string
for _, source := range r.Sources {
var down string
if source.SimulatedDown {
down = " (simulated outage)"
}
col1 = append(col1, strconv.Itoa(source.Count))
col2 = append(col2, source.Name+down)
}
rightAlign(col1)
lines = sideBySide(col1, col2, 2)
sb.WriteString("---- SENT FROM\n")
for _, line := range lines {
sb.WriteString(line)
sb.WriteByte('\n')
}
sb.WriteString("\n")
}
if len(r.Jurisdictions) != 0 {
var lines, col1, col2 []string
for _, juris := range r.Jurisdictions {
col1 = append(col1, strconv.Itoa(juris.Count))
col2 = append(col2, juris.Name)
}
rightAlign(col1)
lines = sideBySide(col1, col2, 2)
sb.WriteString("---- JURISDICTION\n")
for _, line := range lines {
sb.WriteString(line)
sb.WriteByte('\n')
}
sb.WriteString("\n")
}
if len(r.MTypeCounts) != 0 {
var lines, col1, col2 []string
for _, mtype := range r.MTypeCounts {
col1 = append(col1, strconv.Itoa(mtype.Count))
col2 = append(col2, mtype.Name)
}
rightAlign(col1)
lines = sideBySide(col1, col2, 2)
sb.WriteString("---- MESSAGE TYPE\n")
for _, line := range lines {
sb.WriteString(line)
sb.WriteByte('\n')
}
sb.WriteString("\n")
}
}
func (r *Report) plainTextGenInfo(sb *strings.Builder) {
var wr = english.NewWrapper(sb)
wr.WriteString(r.GenerationInfo)
wr.WriteString("\n")
wr.Close()
}
func rightAlign(ss []string) {
var maxlen = maxlength(ss)
for i, s := range ss {
if len(s) < maxlen {
ss[i] = spaces[:maxlen-len(s)] + s
}
}
}
func sideBySide(block1, block2 []string, gap int) (combined []string) {
var (
maxlen int
linenum int
)
maxlen = maxlength(block1)
for linenum = 0; linenum < len(block1) && linenum < len(block2); linenum++ {
combined = append(combined, fmt.Sprintf("%-*s%s", maxlen+gap, block1[linenum], block2[linenum]))
}
for ; linenum < len(block1); linenum++ {
combined = append(combined, block1[linenum])
}
for ; linenum < len(block2); linenum++ {
combined = append(combined, fmt.Sprintf("%-*s%s", maxlen+gap, "", block2[linenum]))
}
return combined
}
func maxlength(ss []string) (maxlen int) {
for _, s := range ss {
if len(s) > maxlen {
maxlen = len(s)
}
}
return maxlen
}