Programming, Technology related

Diff in .Net Core using textdiffcore

In Unix systems the ‘diff’ command is used to highlight the differences between files, it is an extremely useful tool for comparing documents. Outside of Unix systems this functionality is hard to come by, especially if you need to embed the functionality into your program. This is where the textdiffcore package fits in; textdiffcore is a cross platform utility built using .NET Core. textdiffcore is able to give you a list of changes as well as produce an output from the changes in a variety of formats (at the time of writing only HTML and Markdown is supported).

How to use textdiffcore

First of all you will need to add the textdiffcore package to your .Net Core application. To do this follow one of these steps:

  • Package Manager:
    • PM> Install-Package textdiffcore -Version 1.0.3
  • .NET CLI
    • dotnet add package textdiffcore –version 1.0.3
  • Packet CLI
    • paket add textdiffcore –version 1.0.3

You should replace the version number with the latest available on nuget (https://www.nuget.org/packages/textdiffcore/)

In your code you will need to add the following using statements:

using textdiffcore;
using textdiffcore.DiffOutputGenerators;
using textdiffcore.TextDiffEngine;

to use the package you will need to create an instance of the TextDiff class. This class can be instantiated using the following arguments new TextDiff(ITextDiffEngine, IDiffOutputGenerator), for example:

TextDiff diffobj = new TextDiff(new MyersDiff(), new MarkdownDiffOutputGenerator());

The code above creates a TextDiff object which will use the Myers Diff algorithm (powered by google/diff-match-patch) as ITextDiffEngine and MarkdownDiffOutputGenerator as IDiffOutputGenerator (produces the output in Markdown format).

Using the object is a matter of calling the GenerateDiffOutput(string, string) method, i.e.:

//Generate the output
string output = diffobj.GenerateDiffOutput("oldText", "newText");
//print the output to the console
System.Console.WriteLine(output);

This will output:

**~~old~~** **new**Text

Which will look something like this when rendered: “old newText”.

You can also access the Diffrence list after calling either the GenerateDiffOutput or GenerateDiffList method like so:

List innerDiffList = diffobj.InnerList;

Example 2:

The HTMLDiffOutputGenerator(TagType, AttributeName, addAttributeValue, removeAttributeValue, equalAttributeValue) produces a more profound result. If we were to create the TextDiff object like so:

TextDiff diffobj = new TextDiff(new MyersDiff(), new HTMLDiffOutputGenerator("span", "style", "color:#003300;background-color:#ccff66;","color:#990000;background-color:#ffcc99;text-decoration:line-through;",""));

And compared the following strings:

string oldText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\nMaecenas luctus ipsum sit amet turpis pulvinar, et consequat magna tincidunt.\r\nUt tristique sem vitae justo elementum, et fermentum arcu elementum.\r\nDonec blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.\r\nPhasellus faucibus vehicula turpis eu pharetra. In imperdiet iaculis cursus.";
string newText = "Lorem ipsum dolor sit amet, consectetur adiafeeing elit.\r\nMaecenas  sit amet turpis pulvinar, et consequat magna tincidunt.\r\nUt tristique sem vitae justo elementum, et free arcu elementum.\r\nDwfvwc blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.\r\nPhasellus dotnet vehicula turpis eu pharetra. In imperdiet iaculis cursus chieown.";

The output would result in the following HTML:

html

When rendered it will look like so:

Lorem ipsum dolor sit amet, consectetur adipiscafeeing elit.
Maecenas luctus ipsum sit amet turpis pulvinar, et consequat magna tincidunt.
Ut tristique sem vitae justo elementum, et fermentumearcu elementum.
Donewfvwc blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.
Phasellus faucibusdotnet vehicula turpis eu pharetra. In imperdiet iaculis cursus chieown.

Programming

C# Tutorials

Over the past 6 months I’ve worked on some intermediate C# tutorials, they go over topics that are not usually covered by other video tutorials on the internet.

 

Click the read more button to see the list.

Continue reading “C# Tutorials”

Programming

Determine which operating system your program is running on with C#

Another tutorial from my developing for windows 7 series. How to check what OS the program is running on.

Layout of the form I'm using
OperatingSystem SystemInfo = Environment.OSVersion;
        public Form1()
        {
            InitializeComponent();
            label2.Text = SystemInfo.VersionString;
            label1.Text = OSV();
        }
        private string OSV()
        {
            if (SystemInfo.Platform == System.PlatformID.Win32NT)
            {
                if (SystemInfo.Version.Major == 5)
                    return "XP";
                else if (SystemInfo.Version.Major == 6)
                {
                    if (SystemInfo.Version.Minor == 1)
                        return "Win 7";                    
                    return "Vista";
                }
            }
            return "";
        }
Programming

AdvancedPostIt Devlopment Update (DevV0.06)

A lot of new things have been added to this protect from DevV0.05, the size of the project has nearly tripled in a week!

Here is the log for 0.06:

-The ‘image viewer’ Renamed to “AdvancedPostItImage”
-Zoom/pan implemented for the image viewer.
-Reworked logic for image viewer.
-Video Support…
-New App “VideoPlayerVLC” and “VideoPlayerWindows.xaml” for videos.
-Main Program Interface..”AdvancedPostItManager”.
-System tray icon implimented.. no functionality yet.
-Early version of a ‘playlist’ implemented.

Few things that i will do before the beta:

-Make the playlist fully funtional.
-Options menu.
-Anything else that i think will be good.

I’m going to take a break form developing this program at lest until the 20th of this month.

DevV0.06