new cmdline options for diff-text

This commit is contained in:
timerix 2023-03-22 03:10:38 +06:00
parent 0190ad5db8
commit 4e81f615e2
3 changed files with 60 additions and 8 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using DiffMatchPatch; using DiffMatchPatch;
using DTLib.Console;
using DTLib.Filesystem; using DTLib.Filesystem;
namespace diff_text; namespace diff_text;
@ -13,14 +14,39 @@ public static class DiffText
Console.InputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = 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; return;
} }
var _diff = FileDiff(args[0], args[1]); try
PrintDiff(_diff); {
List<Diff>? 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<Diff> FileDiff(string file0, string file1) public static List<Diff> FileDiff(string file0, string file1)
@ -32,14 +58,40 @@ public static class DiffText
public static List<Diff> TextDiff(string text0, string text1) public static List<Diff> TextDiff(string text0, string text1)
{ {
var diff = Diff.Compute(text0, text1, checklines: true); List<Diff>? diff = Diff.Compute(text0, text1, checklines: false);
if (diff is null)
throw new NullReferenceException("diff is null");
diff.CleanupSemantic(); diff.CleanupSemantic();
return diff; return diff;
} }
public static void PrintDiff(List<Diff> diff, bool ignoreWhitespaces = false) public static void PrintDiff(List<Diff> diff, bool ignoreWhitespaces = false, bool noColors = false)
{ {
Console.ResetColor(); 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) foreach (var d in diff)
{ {
bool whitespaceOnly = d.WhitespaceOnlyDiff; bool whitespaceOnly = d.WhitespaceOnlyDiff;

View File

@ -15,6 +15,6 @@
<ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" /> <ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' "> <ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
<PackageReference Include="DTLib" Version="1.1.7" /> <PackageReference Include="DTLib" Version="1.1.8" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -19,7 +19,7 @@
<ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" /> <ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' "> <ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
<PackageReference Include="DTLib" Version="1.1.7" /> <PackageReference Include="DTLib" Version="1.1.8" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\diff-text\diff-text.csproj" /> <ProjectReference Include="..\diff-text\diff-text.csproj" />