new cmdline options for diff-text
This commit is contained in:
parent
0190ad5db8
commit
4e81f615e2
@ -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>? 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)
|
||||
@ -32,14 +58,40 @@ public static class DiffText
|
||||
|
||||
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();
|
||||
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();
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@ -15,6 +15,6 @@
|
||||
<ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||
<PackageReference Include="DTLib" Version="1.1.7" />
|
||||
<PackageReference Include="DTLib" Version="1.1.8" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
<ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
|
||||
<PackageReference Include="DTLib" Version="1.1.7" />
|
||||
<PackageReference Include="DTLib" Version="1.1.8" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\diff-text\diff-text.csproj" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user