Skip to content

Commit

Permalink
Rename -omit to -compact. Better comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Feb 3, 2022
1 parent 73dfce0 commit 11a3522
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

dist/
lines.db
coverage.txt
left
right
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Add option to omit same lines in output
- Add `-compact` option to exclude identical lines from output

### Changed
- Print new lines in right file
Expand Down
4 changes: 2 additions & 2 deletions cmd/lhdiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

func main() {
omit := flag.Bool("omit", false, "Omit lines with same line numbers from output")
compact := flag.Bool("compact", false, "Exclude identical lines from output")
flag.Parse()
leftFile := flag.Arg(0)
rightFile := flag.Arg(1)
left, _ := ioutil.ReadFile(leftFile)
right, _ := ioutil.ReadFile(rightFile)
linePairs, leftCount, newRightLines := lhdiff.Lhdiff(string(left), string(right), 4)
lhdiff.PrintLinePairs(linePairs, leftCount, newRightLines, *omit)
lhdiff.PrintLinePairs(linePairs, leftCount, newRightLines, !*compact)
}
8 changes: 5 additions & 3 deletions lhdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ func Lhdiff(left string, right string, contextSize int) (map[int]LinePair, int,
return allPairs, len(leftLines), rightLineNumbers
}

func PrintLinePairs(linePairs map[int]LinePair, leftLineCount int, newRightLines []int, omitSameLine bool) {
func PrintLinePairs(linePairs map[int]LinePair, leftLineCount int, newRightLines []int, includeIdenticalLines bool) {
for leftLineNumber := 0; leftLineNumber < leftLineCount; leftLineNumber++ {
pair, exists := linePairs[leftLineNumber]
if !exists {
fmt.Printf("%d,_\n", leftLineNumber+1)
} else if !omitSameLine || leftLineNumber != pair.right.lineNumber {
fmt.Printf("%d,%d\n", leftLineNumber+1, pair.right.lineNumber+1)
} else {
if includeIdenticalLines || !(pair.left.content == pair.right.content && leftLineNumber == pair.right.lineNumber) {
fmt.Printf("%d,%d\n", leftLineNumber+1, pair.right.lineNumber+1)
}
}
}
for _, rightLine := range newRightLines {
Expand Down
39 changes: 28 additions & 11 deletions lhdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trois
quatre`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,_
Expand All @@ -31,7 +31,7 @@ three
four`

pairs, leftCount, newRightLines := Lhdiff(left, left, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
// 1,1
Expand All @@ -51,21 +51,38 @@ three
two
four`
pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, true)
PrintLinePairs(pairs, leftCount, newRightLines, false)

// Output:
// 2,3
// 3,2
}

func ExampleLhdiff_withoutPrintingIdenticalLinesWithChangedLines() {
left := `one
two
three
four`

right := `one
two
three x
four`
pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)

// Output:
// 3,3
}

func ExampleLhdiff_withEmptyLeft() {
right := `one
two
three
four`

pairs, leftCount, newRightLines := Lhdiff("", right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
// 1,_
Expand All @@ -82,7 +99,7 @@ three
four`

pairs, leftCount, newRightLines := Lhdiff(left, "", 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,_
Expand All @@ -106,7 +123,7 @@ APPLE PEAR
thirteen fourteen fifteen`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,1
Expand All @@ -128,7 +145,7 @@ two
one`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
// 1,4
Expand Down Expand Up @@ -165,7 +182,7 @@ eleven
`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,1
Expand Down Expand Up @@ -220,7 +237,7 @@ func ExampleLhdiff_withDataFromPaper() {
`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,1
Expand Down Expand Up @@ -289,7 +306,7 @@ func main() {
`

pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,1
Expand Down Expand Up @@ -873,7 +890,7 @@ func RemoveMultipleSpaceAndTrim(s string) string {
`
// https://github.com/aslakhellesoy/lhdiff/commit/4ae3495de0c31675940861592a3929df8154785f
pairs, leftCount, newRightLines := Lhdiff(left, right, 4)
PrintLinePairs(pairs, leftCount, newRightLines, false)
PrintLinePairs(pairs, leftCount, newRightLines, true)

// Output:
//1,1
Expand Down

0 comments on commit 11a3522

Please sign in to comment.