-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #18. I am very torn about having a single write package with a routine per format: write.Pair write.Option write.Names write.Unified write.SideBySide vs having a package per format: write.Option write.Names unified.Pair unified.Write sidebyside.Pair sidebyside.Write One motivation for separate packages is that the ideal interface for different formats is different. For unified, WriteTo is high performance, and we don't need more. For side by side, we need to be able to inspect and even modify the line before writing, so it makes more sense to have an interface specified in terms of []byte. However, we can use a bytes.Buffer internally to bridge from WriteTo to []byte, and most write options are shared between the formats. So for the moment, try out a single write package. Maybe Daniel (or someone else) will have a compelling reason to go one way or another. The adaptor API is getting ugly. It'll get fixed (mostly eliminated) soon.
- Loading branch information
Showing
11 changed files
with
146 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1 @@ | ||
package diff | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/pkg/diff/myers" | ||
) | ||
|
||
// A WriterTo type supports writing a diff, element by element. | ||
// A is the initial state; B is the final state. | ||
type WriterTo interface { | ||
// WriteATo writes the element a[ai] to w. | ||
WriteATo(w io.Writer, ai int) (int, error) | ||
// WriteBTo writes the element b[bi] to w. | ||
WriteBTo(w io.Writer, bi int) (int, error) | ||
} | ||
|
||
// PairWriterTo is the union of Pair and WriterTo. | ||
type PairWriterTo interface { | ||
myers.Pair | ||
WriterTo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= | ||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package write | ||
|
||
import "io" | ||
|
||
func newErrWriter(w io.Writer) *errwriter { | ||
return &errwriter{w: w} | ||
} | ||
|
||
// An errwriter wraps a writer. | ||
// As soon as one write fails, it consumes all subsequent writes. | ||
// This reduces the amount of error-checking required | ||
// in write-heavy code. | ||
type errwriter struct { | ||
w io.Writer | ||
err error | ||
wrote int | ||
attempted int | ||
} | ||
|
||
func (w *errwriter) Write(b []byte) (int, error) { | ||
w.attempted += len(b) | ||
if w.err != nil { | ||
return 0, w.err // TODO: use something like errors.Wrap(w.err)? | ||
} | ||
n, err := w.w.Write(b) | ||
if err != nil { | ||
w.err = err | ||
} | ||
w.wrote += n | ||
return n, err | ||
} | ||
|
||
func (w *errwriter) WriteString(s string) { | ||
// TODO: use w.w's WriteString method, if it exists | ||
w.Write([]byte(s)) | ||
} | ||
|
||
func (w *errwriter) WriteByte(b byte) { | ||
// TODO: use w.w's WriteByte method, if it exists | ||
w.Write([]byte{b}) | ||
} | ||
|
||
func (w *errwriter) Error() error { | ||
return w.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package write provides routines for writing diffs. | ||
package write | ||
|
||
// An Option modifies behavior when writing a diff. | ||
type Option interface { | ||
isOption() | ||
} | ||
|
||
// Names provides the before/after names for writing a diff. | ||
// They are traditionally filenames. | ||
func Names(a, b string) Option { | ||
return names{a, b} | ||
} | ||
|
||
type names struct { | ||
a, b string | ||
} | ||
|
||
func (names) isOption() {} | ||
|
||
// TerminalColor specifies that a diff intended | ||
// for a terminal should be written using colors. | ||
// | ||
// Do not use TerminalColor if TERM=dumb is set in the environment. | ||
func TerminalColor() Option { | ||
return colorOpt(true) | ||
} | ||
|
||
type colorOpt bool | ||
|
||
func (colorOpt) isOption() {} | ||
|
||
const ( | ||
ansiBold = "\u001b[1m" | ||
ansiFgRed = "\u001b[31m" | ||
ansiFgGreen = "\u001b[32m" | ||
ansiFgBlue = "\u001b[36m" | ||
ansiReset = "\u001b[0m" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package write | ||
|
||
// TODO: add diff writing that uses < and > (don't know what that is called) | ||
// TODO: add side by side diffs | ||
// TODO: add html diffs (?) | ||
// TODO: add intraline highlighting? | ||
// TODO: a way to specify alternative colors, like a ColorScheme write option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters