Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Verify.Terminal.Tests/Data/Fourth/new
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
line one
line two
line three
2 changes: 2 additions & 0 deletions src/Verify.Terminal.Tests/Data/Fourth/old
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
line one
line two
3 changes: 3 additions & 0 deletions src/Verify.Terminal.Tests/Data/Third/new
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
feat: add new feature

[tag1]
2 changes: 2 additions & 0 deletions src/Verify.Terminal.Tests/Data/Third/old
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat: add new feature
[tag1]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ First.received.txt
12 │-│············message:·"Unexpected·character·'='",
12 │+│············severity:·Error,
13 │-│············severity:·Verbose,
14 14 │ │········},
15 15 │ │····],
14 13 │ │········},
15 14 │ │····],
─────────────┴─┴────────────────────────────────────────────────────────────────
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
────────────────────────────────────────────────────────────────────────────────
Fourth.received.txt
────────────────────────────────────────────────────────────────────────────────
-old snapshot
+new snapshot
───────────┬─┬──────────────────────────────────────────────────────────────────
1 1 │ │line·one
2 2 │ │line·two
3 │+│line·three
───────────┴─┴──────────────────────────────────────────────────────────────────
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Second.received.txt
───────────┬─┬──────────────────────────────────────────────────────────────────
1 1 │ │+-Greeting----+
2 │+│|·············|
3 3 │ │|·Hello·World·|
2 3 │ │|·Hello·World·|
4 │+│|·············|
5 5 │ │+-------------+
3 5 │ │+-------------+
4 │-│
───────────┴─┴──────────────────────────────────────────────────────────────────
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
────────────────────────────────────────────────────────────────────────────────
Third.received.txt
────────────────────────────────────────────────────────────────────────────────
-old snapshot
+new snapshot
───────────┬─┬──────────────────────────────────────────────────────────────────
1 1 │ │feat:·add·new·feature
2 │+│
2 3 │ │[tag1]
3 4 │ │
───────────┴─┴──────────────────────────────────────────────────────────────────
2 changes: 2 additions & 0 deletions src/Verify.Terminal.Tests/SnapshotRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class SnapshotRendererTests
[Expectation("Render")]
[InlineData("First")]
[InlineData("Second")]
[InlineData("Third")]
[InlineData("Fourth")]
public Task Should_Render_Correctly(string scenario)
{
// Given
Expand Down
4 changes: 4 additions & 0 deletions src/Verify.Terminal.Tests/Verify.Terminal.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<EmbeddedResource Include="Data\Second\old" />
<EmbeddedResource Include="Data\First\new" />
<EmbeddedResource Include="Data\First\old" />
<EmbeddedResource Include="Data\Third\new" />
<EmbeddedResource Include="Data\Third\old" />
<EmbeddedResource Include="Data\Fourth\new" />
<EmbeddedResource Include="Data\Fourth\old" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="Spectre.Console.Testing" />
Expand Down
11 changes: 10 additions & 1 deletion src/Verify.Terminal/SnapshotDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public SnapshotDiff(Snapshot snapshot, List<DiffPiece> old, List<DiffPiece> @new
{
// Found an unchanged line after something that
// had been modified or deleted.
// Stop is exclusive, so it must be allowed to reach New.Count,
// otherwise the last line of the snapshot is never rendered.
var rangeStart = Math.Max(0, start.Value - contextLines);
var rangeEnd = Math.Min(index + contextLines, New.Count - 1);
var rangeEnd = Math.Min(index + contextLines, New.Count);
ranges.Add((rangeStart, rangeEnd));

start = null;
Expand All @@ -55,6 +57,13 @@ public SnapshotDiff(Snapshot snapshot, List<DiffPiece> old, List<DiffPiece> @new
index++;
}

if (start != null)
{
// The snapshot ended while still processing a modified or deleted
// line, so there is no trailing unchanged line to close the range.
ranges.Add((Math.Max(0, start.Value - contextLines), New.Count));
}

if (ranges.Count == 0)
{
return new List<(int Start, int Stop)>();
Expand Down
26 changes: 15 additions & 11 deletions src/Verify.Terminal/SnapshotRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ public IRenderable Render(SnapshotDiff diff, int contextLines)
if (@new.Type == ChangeType.Modified)
{
// Modified lines
RenderLine(ctx, old, index, '-', Color.Red, true, false);
RenderLine(ctx, @new, index, '+', Color.Green, false, true);
RenderLine(ctx, old, old.Position, null, '-', Color.Red);
RenderLine(ctx, @new, null, @new.Position, '+', Color.Green);
}
else if (@new.Type == ChangeType.Inserted)
{
// Inserted
RenderLine(ctx, @new, index, '+', Color.Green, false, true);
RenderLine(ctx, @new, null, @new.Position, '+', Color.Green);
}
else if (@new.Type == ChangeType.Imaginary)
{
// Modified lines
RenderLine(ctx, old, index, '-', Color.Red, true, false);
// Deleted
RenderLine(ctx, old, old.Position, null, '-', Color.Red);
}
else
{
// Unchanged
RenderLine(ctx, @new, index, ' ', Color.Grey, true, true);
RenderLine(ctx, @new, old.Position, @new.Position, ' ', Color.Grey);
}
}

Expand All @@ -114,14 +114,18 @@ public IRenderable Render(SnapshotDiff diff, int contextLines)

private void RenderLine(
SnapshotRendererContext ctx, DiffPiece piece,
int index, char op, Color color,
bool showLeft, bool showRight)
int? leftPosition, int? rightPosition, char op, Color color)
{
var leftLineNumber = showLeft ? (index + 1).ToString() : string.Empty;
var rightLineNumber = showRight ? (index + 1).ToString() : string.Empty;
// The positions come from the snapshots themselves rather than from the
// index into the aligned diff, which drifts once a line is added or removed.
var leftLineNumber = leftPosition?.ToString() ?? string.Empty;
var rightLineNumber = rightPosition?.ToString() ?? string.Empty;

var maxWidth = _console.Profile.Width - (4 + ctx.LineNumberWidth + 4 + ctx.LineNumberWidth + 1 + 1 + 1 + 1);
var pieces = (piece.Text.Length / maxWidth) + ((piece.Text.Length % maxWidth) == 0 ? 0 : 1);

// An empty line still occupies a single row. Without the minimum of one,
// added or removed blank lines would not be rendered at all.
var pieces = Math.Max(1, (piece.Text.Length / maxWidth) + ((piece.Text.Length % maxWidth) == 0 ? 0 : 1));

for (var i = 0; i < pieces; i++)
{
Expand Down