Repository navigation
Expand file tree
/
Copy pathreport.go
More file actions
149 lines (132 loc) · 3.67 KB
/
Copy pathreport.go
File metadata and controls
149 lines (132 loc) · 3.67 KB
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
package main
// What a finding is, and how it reaches the screen.
import (
"fmt"
"io"
"sort"
"strings"
)
type Severity int
const (
// Note is worth knowing.
Note Severity = iota
// Narrows is something that takes people out of the lineup.
Narrows
// Headline is the count itself, which always comes first.
Headline
)
type Finding struct {
Tag string
Weight Severity
Title string
Lines []string
// Advice is the part people act on. A finding without it is a finding
// nobody does anything about.
Advice string
}
type Report struct {
Title string
Findings []Finding
// Gaps are what could not be read. They print first, because a count
// built on half an export is half a count.
Gaps []string
}
func (r *Report) add(f Finding) { r.Findings = append(r.Findings, f) }
const (
indent = " "
wrap = 62
)
func (r *Report) render(out io.Writer) {
fmt.Fprintf(out, "\n %s\n %s\n\n", r.Title, strings.Repeat("=", min(len(r.Title), wrap+13)))
for _, g := range r.Gaps {
block(out, "UNREAD ", "Something here could not be read", nil, g)
}
// Loudest first, keeping the order within a level. Somebody who reads
// the first screen and stops should have read the part that mattered.
sorted := make([]Finding, len(r.Findings))
copy(sorted, r.Findings)
sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].Weight > sorted[j].Weight })
for _, f := range sorted {
block(out, f.Tag, f.Title, f.Lines, f.Advice)
}
fmt.Fprintln(out, " Nothing was changed, and nothing was sent anywhere.")
fmt.Fprintln(out)
}
func block(out io.Writer, tag, title string, lines []string, advice string) {
head := wrapText(title, wrap+10)
if len(head) == 0 {
head = []string{""}
}
fmt.Fprintf(out, " [%s] %s\n", tag, head[0])
for _, line := range head[1:] {
fmt.Fprintf(out, "%s%s\n", indent, line)
}
for _, line := range lines {
if line = strings.TrimRight(line, " "); line == "" {
fmt.Fprintln(out)
continue
}
fmt.Fprintf(out, "%s%s\n", indent, line)
}
if advice != "" {
if len(lines) > 0 {
fmt.Fprintln(out)
}
for _, line := range wrapText(advice, wrap) {
fmt.Fprintf(out, "%s%s\n", indent, line)
}
}
fmt.Fprintln(out)
}
// wrapText counts letters rather than bytes. The report is as likely to
// carry "Málaga" as "Leeds", and the two should wrap in the same place.
func wrapText(s string, width int) []string {
words := strings.Fields(s)
if len(words) == 0 {
return nil
}
var lines []string
line := words[0]
for _, w := range words[1:] {
if runes(line)+1+runes(w) > width {
lines = append(lines, line)
line = w
continue
}
line += " " + w
}
return append(lines, line)
}
func runes(s string) int { return len([]rune(s)) }
func plural(n int, one, many string) string {
if n == 1 {
return fmt.Sprintf("%d %s", n, one)
}
return fmt.Sprintf("%s %s", commas(int64(n)), many)
}
func atMost(lines []string, n int) []string {
if len(lines) <= n {
return lines
}
kept := make([]string, 0, n+1)
kept = append(kept, lines[:n]...)
return append(kept, fmt.Sprintf("... and %d more", len(lines)-n))
}
// columns puts a label on the left and a figure on the right, with the
// figures lined up, because a funnel with ragged numbers gets skimmed
// rather than read.
const columnAt = 44
func columns(left, right string) []string {
if runes(left) >= columnAt {
return []string{left, strings.Repeat(" ", columnAt) + right}
}
return []string{left + strings.Repeat(" ", columnAt-runes(left)) + right}
}
// quote cuts what somebody said down to something that fits on a line.
func quote(s string) string {
s = strings.Join(strings.Fields(s), " ")
if runes(s) > 56 {
s = string([]rune(s)[:53]) + "..."
}
return `"` + s + `"`
}