File: unified.go

package info (click to toggle)
golang-github-pkg-diff 0.0~git20210226.20ebb0f-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,528 kB
  • sloc: makefile: 3
file content (176 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download
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
package write

import (
	"bufio"
	"fmt"
	"io"

	"github.com/pkg/diff/edit"
)

// A Pair supports writing a unified diff, element by element.
// A is the initial state; B is the final state.
type Pair interface {
	// WriteATo writes the element a[aᵢ] to w.
	WriteATo(w io.Writer, ai int) (int, error)
	// WriteBTo writes the element b[bᵢ] to w.
	WriteBTo(w io.Writer, bi int) (int, error)
}

// Unified writes e to w using unified diff format.
// ab writes the individual elements. Opts are optional write arguments.
// Unified returns the number of bytes written and the first error (if any) encountered.
// Before writing, edit scripts usually have their context reduced,
// such as by a call to ctxt.Size.
func Unified(e edit.Script, w io.Writer, ab Pair, opts ...Option) error {
	// read opts
	nameA := "a"
	nameB := "b"
	color := false
	for _, opt := range opts {
		switch opt := opt.(type) {
		case names:
			nameA = opt.a
			nameB = opt.b
		case colorOpt:
			color = true
		// TODO: add date/time/timezone WriteOpts
		default:
			panic(fmt.Sprintf("unrecognized WriteOpt type %T", opt))
		}
	}

	bw := bufio.NewWriter(w)

	needsColorReset := false

	// per-file header
	if color {
		bw.WriteString(ansiBold)
		needsColorReset = true
	}
	fmt.Fprintf(bw, "--- %s\n", nameA)
	fmt.Fprintf(bw, "+++ %s\n", nameB)

	for i := 0; i < len(e.Ranges); {
		// Peek into the future to learn the line ranges for this chunk of output.
		// A chunk of output ends when there's a discontiguity in the edit script.
		var ar, br lineRange
		var started [2]bool
		var j int
		for j = i; j < len(e.Ranges); j++ {
			curr := e.Ranges[j]
			if !curr.IsInsert() {
				if !started[0] {
					ar.first = curr.LowA
					started[0] = true
				}
				ar.last = curr.HighA
			}
			if !curr.IsDelete() {
				if !started[1] {
					br.first = curr.LowB
					started[1] = true
				}
				br.last = curr.HighB
			}
			if j+1 >= len(e.Ranges) {
				// end of script
				break
			}
			if next := e.Ranges[j+1]; curr.HighA != next.LowA || curr.HighB != next.LowB {
				// discontiguous edit script
				break
			}
		}

		// Print chunk header.
		// TODO: add per-chunk context, like what function we're in
		// But how do we get this? need to add PairWriter methods?
		// Maybe it should be stored in the EditScript,
		// and we can have EditScript methods to populate it somehow?
		if color {
			if needsColorReset {
				bw.WriteString(ansiReset)
			}
			bw.WriteString(ansiFgBlue)
			needsColorReset = true
		}
		fmt.Fprintf(bw, "@@ -%s +%s @@\n", ar, br)

		// Print prefixed lines.
		for k := i; k <= j; k++ {
			seg := e.Ranges[k]
			switch seg.Op() {
			case edit.Eq:
				if needsColorReset {
					bw.WriteString(ansiReset)
				}
				for m := seg.LowA; m < seg.HighA; m++ {
					// " a[m]\n"
					bw.WriteByte(' ')
					ab.WriteATo(bw, m)
					bw.WriteByte('\n')
				}
			case edit.Del:
				if color {
					bw.WriteString(ansiFgRed)
					needsColorReset = true
				}
				for m := seg.LowA; m < seg.HighA; m++ {
					// "-a[m]\n"
					bw.WriteByte('-')
					ab.WriteATo(bw, m)
					bw.WriteByte('\n')
				}
			case edit.Ins:
				if color {
					bw.WriteString(ansiFgGreen)
					needsColorReset = true
				}
				for m := seg.LowB; m < seg.HighB; m++ {
					// "+b[m]\n"
					bw.WriteByte('+')
					ab.WriteBTo(bw, m)
					bw.WriteByte('\n')
				}
			}
		}

		// Advance to next chunk.
		i = j + 1

		// TODO: break if error detected?
	}

	// Always finish the output with no color, to prevent "leaking" the
	// color into any output that follows a diff.
	if needsColorReset {
		bw.WriteString(ansiReset)
	}

	// TODO:
	// If the last line of a file doesn't end in a newline character,
	// it is displayed with a newline character,
	// and the following line in the chunk has the literal text (starting in the first column):
	// '\ No newline at end of file'

	return bw.Flush()
}

type lineRange struct {
	first, last int
}

func (r lineRange) String() string {
	len := r.last - r.first
	r.first++ // 1-based index, safe to modify r directly because it is a value
	if len <= 0 {
		r.first-- // for no obvious reason, empty ranges are "before" the range
	}
	return fmt.Sprintf("%d,%d", r.first, len)
}

func (r lineRange) GoString() string {
	return fmt.Sprintf("(%d, %d)", r.first, r.last)
}