Skip to content

Commit a49765e

Browse files
committed
Add unidiff text rendering support (fixes #147)
1 parent 9d5eac0 commit a49765e

File tree

6 files changed

+596
-22
lines changed

6 files changed

+596
-22
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
22
"dotnet-test-explorer.testProjectPath": "Facts.DiffPlex",
3+
"cSpell.words": [
4+
"Xunit"
5+
],
36
}

DiffPlex.ConsoleRunner/Program.cs

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,99 @@
11
using System;
2+
using System.IO;
23
using DiffPlex.DiffBuilder;
34
using DiffPlex.DiffBuilder.Model;
45
using DiffPlex.Model;
6+
using DiffPlex.Renderer;
57

68
namespace DiffPlex.ConsoleRunner;
79

810
internal static class Program
911
{
10-
private static void Main()
12+
private static void Main(string[] args)
1113
{
12-
var diffBuilder = new InlineDiffBuilder(new Differ());
13-
var diff = diffBuilder.BuildDiffModel(TestData.OldText, TestData.NewText);
14+
if (args.Length < 3)
15+
{
16+
PrintUsage();
17+
return;
18+
}
1419

15-
foreach (var line in diff.Lines)
20+
try
1621
{
17-
Console.ForegroundColor = ConsoleColor.Blue;
18-
if (line.Position.HasValue) Console.Write(line.Position.Value);
19-
Console.Write('\t');
20-
switch (line.Type)
22+
string command = args[0].ToLowerInvariant();
23+
string result;
24+
25+
if (command == "file")
26+
{
27+
// File comparison mode
28+
string oldFilePath = NormalizePath(args[1]);
29+
string newFilePath = NormalizePath(args[2]);
30+
31+
if (!File.Exists(oldFilePath))
32+
{
33+
Console.Error.WriteLine($"Error: File not found: {oldFilePath}");
34+
return;
35+
}
36+
37+
if (!File.Exists(newFilePath))
38+
{
39+
Console.Error.WriteLine($"Error: File not found: {newFilePath}");
40+
return;
41+
}
42+
43+
string oldText = File.ReadAllText(oldFilePath);
44+
string newText = File.ReadAllText(newFilePath);
45+
result = UnidiffRenderer.GenerateUnidiff(oldText, newText, oldFilePath, newFilePath);
46+
}
47+
else if (command == "text")
48+
{
49+
// Text comparison mode
50+
// Process escape sequences like \n in the input text
51+
string oldText = args[1].Replace("\\n", "\n");
52+
string newText = args[2].Replace("\\n", "\n");
53+
result = UnidiffRenderer.GenerateUnidiff(oldText, newText);
54+
}
55+
else
2156
{
22-
case ChangeType.Inserted:
23-
Console.ForegroundColor = ConsoleColor.Green;
24-
Console.Write("+ ");
25-
break;
26-
case ChangeType.Deleted:
27-
Console.ForegroundColor = ConsoleColor.Red;
28-
Console.Write("- ");
29-
break;
30-
default:
31-
Console.ForegroundColor = ConsoleColor.White;
32-
Console.Write(" ");
33-
break;
57+
Console.Error.WriteLine($"Unknown command: {command}");
58+
PrintUsage();
59+
return;
3460
}
3561

36-
Console.WriteLine(line.Text);
62+
Console.WriteLine(result);
63+
}
64+
catch (Exception ex)
65+
{
66+
Console.Error.WriteLine($"Error: {ex.Message}");
3767
}
3868
}
69+
70+
private static void PrintUsage()
71+
{
72+
Console.WriteLine("DiffPlex.ConsoleRunner - Generate unified diff (unidiff) output");
73+
Console.WriteLine();
74+
Console.WriteLine("Usage:");
75+
Console.WriteLine(" DiffPlex.ConsoleRunner file <old-file> <new-file>");
76+
Console.WriteLine(" DiffPlex.ConsoleRunner text <old-string> <new-string>");
77+
Console.WriteLine();
78+
Console.WriteLine("Commands:");
79+
Console.WriteLine(" file Compare two files and generate a unidiff");
80+
Console.WriteLine(" text Compare two strings and generate a unidiff");
81+
Console.WriteLine();
82+
Console.WriteLine("File paths work with both Windows and Unix-style paths.");
83+
}
84+
85+
/// <summary>
86+
/// Normalizes a file path to work correctly on both Windows and Unix-based systems.
87+
/// </summary>
88+
/// <param name="path">The path to normalize.</param>
89+
/// <returns>A normalized path that works on the current operating system.</returns>
90+
private static string NormalizePath(string path)
91+
{
92+
if (string.IsNullOrWhiteSpace(path))
93+
return path;
94+
95+
// Replace any forward slashes with the platform-specific directory separator
96+
return path.Replace('/', Path.DirectorySeparatorChar)
97+
.Replace('\\', Path.DirectorySeparatorChar);
98+
}
3999
}

DiffPlex/DiffPlex.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\NuGet.props" />
33
<PropertyGroup>
4-
<TargetFrameworks>net45;netstandard1.0;netstandard2.0;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
55
<Version>1.7.2</Version>
66
<PackageTags>diff</PackageTags>
77
<Description>DiffPlex is a diffing library that allows you to programmatically create text diffs. DiffPlex is a fast and tested library.</Description>

0 commit comments

Comments
 (0)