From 4e81f615e2767c8aac8237ec8653d5efec3a5d60 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 22 Mar 2023 03:10:38 +0600 Subject: [PATCH] new cmdline options for diff-text --- diff-text/Program.cs | 64 ++++++++++++++++++-- diff-text/diff-text.csproj | 2 +- paradox-mod-merger/paradox-mod-merger.csproj | 2 +- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/diff-text/Program.cs b/diff-text/Program.cs index c1b1eee..deb9947 100644 --- a/diff-text/Program.cs +++ b/diff-text/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using DiffMatchPatch; +using DTLib.Console; using DTLib.Filesystem; namespace diff_text; @@ -13,14 +14,39 @@ public static class DiffText Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; - if (args.Length != 2) + if (args.Length < 1) { - Console.WriteLine("usage: [file0] [file1]"); + Console.WriteLine("too few arguments, use -h to show help "); return; } - var _diff = FileDiff(args[0], args[1]); - PrintDiff(_diff); + try + { + List? diff = null; + bool noColors = false; + new LaunchArgumentParser( + new LaunchArgument(new[] { "s", "string" }, + "shows difference of two strings", + (s0, s1) => diff=TextDiff(s0, s1), + "string0", "string1", 1), + new LaunchArgument(new[] { "f", "file" }, + "shows difference of two text files", + (f0,f1) => diff=FileDiff(f0, f1), + "file0", "file1", 1), + new LaunchArgument(new []{"p", "plain-text","no-colors"}, + "print diff in plain text format", + ()=> noColors=true, 0) + ).ParseAndHandle(args); + if (diff == null) + throw new Exception("no action specified: use -s or -f"); + PrintDiff(diff, false, noColors); + } + catch (LaunchArgumentParser.ExitAfterHelpException) + { } + catch (Exception ex) + { + ColoredConsole.WriteLine("r", $"{ex.Message}\n{ex.StackTrace}"); + } } public static List FileDiff(string file0, string file1) @@ -32,14 +58,40 @@ public static class DiffText public static List TextDiff(string text0, string text1) { - var diff = Diff.Compute(text0, text1, checklines: true); + List? diff = Diff.Compute(text0, text1, checklines: false); + if (diff is null) + throw new NullReferenceException("diff is null"); diff.CleanupSemantic(); return diff; } - public static void PrintDiff(List diff, bool ignoreWhitespaces = false) + public static void PrintDiff(List diff, bool ignoreWhitespaces = false, bool noColors = false) { Console.ResetColor(); + + + if (noColors) + { + StringBuilder b = new(); + foreach (var patch in Patch.FromDiffs(diff)) + { + b.Append("@@ " + patch.Coordinates + " @@\n"); + foreach (var patchDiff in patch.Diffs) + { + char opChar = patchDiff.Operation switch + { + Operation.Delete => '<', + Operation.Insert => '>', + Operation.Equal => ' ', + _ => throw new ArgumentOutOfRangeException() + }; + b.Append(opChar).Append(' ').Append(patchDiff.FormattedText).Append('\n'); + } + } + Console.WriteLine(b.ToString()); + return; + } + foreach (var d in diff) { bool whitespaceOnly = d.WhitespaceOnlyDiff; diff --git a/diff-text/diff-text.csproj b/diff-text/diff-text.csproj index 38fbf77..2f99905 100644 --- a/diff-text/diff-text.csproj +++ b/diff-text/diff-text.csproj @@ -15,6 +15,6 @@ - + diff --git a/paradox-mod-merger/paradox-mod-merger.csproj b/paradox-mod-merger/paradox-mod-merger.csproj index 90d2f6b..4e123f1 100644 --- a/paradox-mod-merger/paradox-mod-merger.csproj +++ b/paradox-mod-merger/paradox-mod-merger.csproj @@ -19,7 +19,7 @@ - +