Skip to content

Commit

Permalink
fix incorrect newline handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Dehler authored and HeavyWombat committed Feb 6, 2024
1 parent 1c3f3f4 commit 214f951
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
17 changes: 13 additions & 4 deletions assets/multiline/expected-dyff-spruce.human
Expand Up @@ -13,18 +13,27 @@


files.newline.content
± value change in multiline text (three inserts, three deletions)
± value change in multiline text (four inserts, four deletions)
 
 - 
 - This line will change 1
 + This line changed 1
 UnChanged line
 
 - This line will change 2
 + This line changed 2
 UnChanged line
 
 
 - 
 Moved line
 + 
 
 
 UnChanged line
 - This line will change 3
 + This line changed 3
 + 
 
 

Expand Down Expand Up @@ -81,8 +90,8 @@
 [22 lines unchanged)]
 
 End line 1
 End line 1
 End line 1
 End line 1
 End line 2
 End line 3
 End line 4


18 changes: 11 additions & 7 deletions assets/multiline/from.yml
Expand Up @@ -15,14 +15,18 @@ files:
This line will change 1
UnChanged line
This line will change 2
UnChanged line
This line will change 3
Moved line
UnChanged line
This line will change 3
- name: complex
content: |
Expand Down Expand Up @@ -91,6 +95,6 @@ files:
Truncated line
Truncated line
End line 1
End line 1
End line 1
End line 1
End line 2
End line 3
End line 4
18 changes: 11 additions & 7 deletions assets/multiline/to.yml
Expand Up @@ -12,13 +12,17 @@ files:
# + to keep trailing newlines
content: |+
This line changed 1
UnChanged line
This line changed 2
UnChanged line
Moved line
UnChanged line
This line changed 3
Expand Down Expand Up @@ -90,6 +94,6 @@ files:
Truncated line
Truncated line
End line 1
End line 1
End line 1
End line 1
End line 2
End line 3
End line 4
8 changes: 6 additions & 2 deletions pkg/dyff/output_human.go
Expand Up @@ -371,14 +371,18 @@ func (report *HumanReport) writeStringDiff(output stringWriter, from string, to
continue
}
// add amount of unchanged lines as configured
lines := strings.Split(strings.TrimSuffix(d.Text, "\n"), "\n")
lines := strings.Split(d.Text, "\n")
lower := int(math.Min(float64(len(lines)), float64(multilineContextLines)))
upper := len(lines) - multilineContextLines
// if string ends with \n we need to display one more line on the upper limit
if strings.HasSuffix(d.Text, "\n") {
upper--
}
var val string
if upper <= lower {
val = strings.Join(lines, "\n")
} else {
val = fmt.Sprintf("%s\n\n[%s unchanged)]\n\n%s\n",
val = fmt.Sprintf("%s\n\n[%s unchanged)]\n\n%s",
strings.Join(lines[:lower], "\n"),
text.Plural((upper-lower), "line"),
strings.Join(lines[upper:], "\n"))
Expand Down

0 comments on commit 214f951

Please sign in to comment.