forked from creachadair/mds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
198 lines (178 loc) · 5.66 KB
/
format.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
package mdiff
import (
"cmp"
"fmt"
"io"
"strconv"
"time"
"github.com/creachadair/mds/slice"
)
// FormatFunc is a function that renders a Diff as text to an io.Writer.
//
// A FormatFunc should accept a nil info pointer, and should skip or supply
// default values for missing fields.
type FormatFunc func(w io.Writer, d *Diff, fi *FileInfo) error
// TimeFormat is the default format string used to render timestamps in context
// and unified diff outputs. It is based on the RFC 2822 time format.
const TimeFormat = "2006-01-02 15:04:05.999999 -0700"
// FileInfo specifies file metadata to use when formatting a diff.
type FileInfo struct {
// Left is the filename to use for the left-hand input.
Left string
// Right is the filename to use for the right-hand argument.
Right string
// LeftTime is the timestamp to use for the left-hand input.
LeftTime time.Time
// RightTime is the timestamp to use for the right-hand input.
RightTime time.Time
// TimeFormat specifies the time format to use for timestamps.
// Any format string accepted by time.Format is permitted.
// If omitted, it uses the TimeFormat constant.
TimeFormat string
}
// FormatUnified is a [FormatFunc] that renders d in the [unified diff] format
// introduced by GNU diff. If fi == nil, the file header is omitted.
//
// [unified diff]: https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html
func FormatUnified(w io.Writer, d *Diff, fi *FileInfo) error {
if len(d.Chunks) == 0 {
return nil
}
if fi != nil {
fmtFileHeader(w, "--- ", cmp.Or(fi.Left, "a"), fi.LeftTime, cmp.Or(fi.TimeFormat, TimeFormat))
fmtFileHeader(w, "+++ ", cmp.Or(fi.Right, "b"), fi.RightTime, cmp.Or(fi.TimeFormat, TimeFormat))
}
for _, c := range d.Chunks {
fmt.Fprintln(w, "@@", uspan("-", c.LStart, c.LEnd), uspan("+", c.RStart, c.REnd), "@@")
for _, e := range c.Edits {
switch e.Op {
case slice.OpDrop:
writeLines(w, "-", e.X)
case slice.OpEmit:
writeLines(w, " ", e.X)
case slice.OpCopy:
writeLines(w, "+", e.Y)
case slice.OpReplace:
writeLines(w, "-", e.X)
writeLines(w, "+", e.Y)
}
}
}
return nil
}
func fmtFileHeader(w io.Writer, prefix, name string, ts time.Time, tfmt string) {
fmt.Fprint(w, prefix, name)
if !ts.IsZero() {
fmt.Fprint(w, "\t", ts.Format(tfmt))
}
fmt.Fprintln(w)
}
// FormatContext is a [FormatFunc] that renders d in the [context diff] format
// introduced by BSD diff. If fi == nil, the file header is omitted.
//
// [context diff]: https://www.gnu.org/software/diffutils/manual/html_node/Context-Format.html
func FormatContext(w io.Writer, d *Diff, fi *FileInfo) error {
if len(d.Chunks) == 0 {
return nil
}
if fi != nil {
fmtFileHeader(w, "*** ", cmp.Or(fi.Left, "a"), fi.LeftTime, cmp.Or(fi.TimeFormat, TimeFormat))
fmtFileHeader(w, "--- ", cmp.Or(fi.Right, "b"), fi.RightTime, cmp.Or(fi.TimeFormat, TimeFormat))
}
for _, c := range d.Chunks {
// Why 15 stars? I can't say. Berkeley just liked it better that way.
fmt.Fprintln(w, "***************")
fmt.Fprintf(w, "*** %s ****\n", dspan(c.LStart, c.LEnd))
if hasRelevantEdits(c.Edits, slice.OpDrop) {
for _, e := range c.Edits {
switch e.Op {
case slice.OpDrop:
writeLines(w, "- ", e.X)
case slice.OpEmit:
writeLines(w, " ", e.X)
case slice.OpReplace:
writeLines(w, "! ", e.X)
}
}
}
fmt.Fprintf(w, "--- %s ----\n", dspan(c.RStart, c.REnd))
if hasRelevantEdits(c.Edits, slice.OpCopy) {
for _, e := range c.Edits {
switch e.Op {
case slice.OpCopy:
writeLines(w, "+ ", e.Y)
case slice.OpEmit:
writeLines(w, " ", e.X)
case slice.OpReplace:
writeLines(w, "! ", e.Y)
}
}
}
}
return nil
}
// Format is a [FormatFunc] that renders d in the "normal" [Unix diff] format.
// This format does not include a file header, so the FileInfo is ignored.
//
// [Unix diff]: https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html
func Format(w io.Writer, d *Diff, _ *FileInfo) error {
for _, c := range d.Chunks {
lpos, rpos := c.LStart, c.RStart
for _, e := range c.Edits {
switch e.Op {
case slice.OpDrop:
// Diff considers deletions to happen AFTER the previous line rather
// than on the current one.
fmt.Fprintf(w, "%sd%d\n", dspan(lpos, lpos+len(e.X)), rpos-1)
writeLines(w, "< ", e.X)
lpos += len(e.X)
case slice.OpEmit:
lpos += len(e.X)
rpos += len(e.X)
case slice.OpCopy:
// Diff considers insertions to happen AFTER the previons line rather
// than on the current one.
fmt.Fprintf(w, "%da%s\n", lpos-1, dspan(rpos, rpos+len(e.Y)))
writeLines(w, "> ", e.Y)
rpos += len(e.Y)
case slice.OpReplace:
fmt.Fprintf(w, "%sc%s\n", dspan(lpos, lpos+len(e.X)), dspan(rpos, rpos+len(e.Y)))
writeLines(w, "< ", e.X)
fmt.Fprintln(w, "---")
writeLines(w, "> ", e.Y)
lpos += len(e.X)
rpos += len(e.Y)
}
}
}
return nil
}
// dspan formats the range start, end as a diff span.
func dspan(start, end int) string {
if end-start == 1 {
return strconv.Itoa(start)
}
return fmt.Sprintf("%d,%d", start, end-1)
}
// uspan formats the range start, end as a unified diff span.
func uspan(side string, start, end int) string {
if end-start == 1 {
return side + strconv.Itoa(start)
}
return fmt.Sprintf("%s%d,%d", side, start, end-start)
}
func writeLines(w io.Writer, pfx string, lines []string) {
for _, line := range lines {
fmt.Fprint(w, pfx, line, "\n")
}
}
// hasRelevantEdits reports whether es contains at least one edit with either
// the specified opcode or slice.OpReplace.
func hasRelevantEdits(es []Edit, op slice.EditOp) bool {
for _, e := range es {
if e.Op == op || e.Op == slice.OpReplace {
return true
}
}
return false
}