diff-text

This commit is contained in:
2023-02-24 23:34:41 +06:00
parent 971896cacb
commit 9c27744b61
14 changed files with 95 additions and 36 deletions

63
diff-text/Program.cs Normal file
View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using DiffMatchPatch;
using DTLib.Filesystem;
Console.InputEncoding=Encoding.UTF8;
Console.OutputEncoding=Encoding.UTF8;
if (args.Length != 2)
{
Console.WriteLine("usage: [file0] [file1]");
return;
}
var _diff=FileDiff(args[0], args[1]);
PrintDiff(_diff);
List<Diff> FileDiff(string file0, string file1)
{
string fileText0 = File.ReadAllText(file0);
string fileText1 = File.ReadAllText(file1);
return TextDiff(fileText0, fileText1);
}
List<Diff> TextDiff(string text0, string text1)
{
var diff = Diff.Compute(text0, text1, checklines:true);
diff.CleanupSemantic();
return diff;
}
void PrintDiff(List<Diff> diff, bool ignoreWhitespaces=false)
{
foreach (var d in diff)
{
bool whitespaceOnly = d.WhitespaceOnlyDiff;
if(ignoreWhitespaces && whitespaceOnly)
continue;
switch(d.Operation)
{
case Operation.Delete:
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(whitespaceOnly ? d.FormattedText : d.Text);
Console.ResetColor();
break;
case Operation.Insert:
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(whitespaceOnly ? d.FormattedText : d.Text);
Console.ResetColor();
break;
case Operation.Equal:
Console.Write(d.Text);
break;
default:
throw new ArgumentOutOfRangeException(d.Operation.ToString());
}
}
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>diff_text</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="google-diff-match-patch" Version="1.3.70" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<ProjectReference Include="..\..\DTLib\DTLib\DTLib.csproj" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
<PackageReference Include="DTLib" Version="1.1.3" />
</ItemGroup>
</Project>