Skip to content

Commit

Permalink
ctxt: create package
Browse files Browse the repository at this point in the history
Part of #18.
  • Loading branch information
josharian committed Dec 30, 2019
1 parent f932f2c commit 9897735
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/pkg-diff-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"github.com/pkg/diff"
"github.com/pkg/diff/ctxt"
"github.com/pkg/diff/myers"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func main() {
defer cancel()
}
e := myers.Diff(ctx, ab)
e = diff.EditScriptWithContextSize(e, *unified) // limit amount of output context
e = ctxt.Size(e, *unified) // limit amount of output context
opts := []diff.WriteOpt{
diff.Names(aName, bName),
}
Expand Down
14 changes: 6 additions & 8 deletions context.go → ctxt/size.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package diff
// Package ctxt provides routines to reduce the amount of context in an edit script.
package ctxt

import (
"fmt"

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

// WithContextSize returns an edit script preserving only n common elements of context for changes.
// Size returns an edit script preserving only n common elements of context for changes.
// The returned edit script may alias the input.
// If n is negative, WithContextSize panics.
// To generate a "unified diff", use WithContextSize and then WriteUnified the resulting edit script.
func EditScriptWithContextSize(e edit.Script, n int) edit.Script {
// If n is negative, Size panics.
func Size(e edit.Script, n int) edit.Script {
if n < 0 {
panic(fmt.Sprintf("EditScript.WithContextSize called with negative n: %d", n))
panic("ctxt.Size called with negative n")
}

// Handle small scripts.
Expand Down
3 changes: 2 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/pkg/diff"
"github.com/pkg/diff/ctxt"
"github.com/pkg/diff/myers"
)

Expand All @@ -18,7 +19,7 @@ func Example_testHelper() {
if e.IsIdentity() {
return
}
e = diff.EditScriptWithContextSize(e, 1)
e = ctxt.Size(e, 1)
diff.WriteUnified(e, os.Stdout, ab)
// Output:
// --- a
Expand Down
2 changes: 2 additions & 0 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
// WriteUnified writes e to w using unified diff format.
// ab writes the individual elements. Opts are optional write arguments.
// WriteUnified 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 WriteUnified(e edit.Script, w io.Writer, ab WriterTo, opts ...WriteOpt) (int, error) {
// read opts
nameA := "a"
Expand Down
3 changes: 2 additions & 1 deletion unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/pkg/diff"
"github.com/pkg/diff/ctxt"
"github.com/pkg/diff/myers"
"github.com/sergi/go-diff/diffmatchpatch"
)
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestGolden(t *testing.T) {
// Doing it as I have done, the lazy way, mixes concerns: diff algorithm vs unification algorithm
// vs unified diff formatting.
e := myers.Diff(context.Background(), ab)
e = diff.EditScriptWithContextSize(e, 3)
e = ctxt.Size(e, 3)
buf := new(bytes.Buffer)
diff.WriteUnified(e, buf, ab, test.opts...)
got := buf.String()
Expand Down

0 comments on commit 9897735

Please sign in to comment.