|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using DiffPlex.DiffBuilder; |
3 | 4 | using DiffPlex.DiffBuilder.Model; |
4 | 5 | using DiffPlex.Model; |
| 6 | +using DiffPlex.Renderer; |
5 | 7 |
|
6 | 8 | namespace DiffPlex.ConsoleRunner; |
7 | 9 |
|
8 | 10 | internal static class Program |
9 | 11 | { |
10 | | - private static void Main() |
| 12 | + private static void Main(string[] args) |
11 | 13 | { |
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 | + } |
14 | 19 |
|
15 | | - foreach (var line in diff.Lines) |
| 20 | + try |
16 | 21 | { |
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 |
21 | 56 | { |
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; |
34 | 60 | } |
35 | 61 |
|
36 | | - Console.WriteLine(line.Text); |
| 62 | + Console.WriteLine(result); |
| 63 | + } |
| 64 | + catch (Exception ex) |
| 65 | + { |
| 66 | + Console.Error.WriteLine($"Error: {ex.Message}"); |
37 | 67 | } |
38 | 68 | } |
| 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 | + } |
39 | 99 | } |
0 commit comments