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.

Everything else, Technology related

How to import freinds from Facebook to Google plus (via Yahoo and Gmail)

I know that there are tons of tutorials on this already, but well none of them actually worked for me. Here is how i did it.

Continue reading “How to import freinds from Facebook to Google plus (via Yahoo and Gmail)”

Technology related

My thoughts on the Micrsoft Exam 70-680.

After about two weeks of preparing, i passed the 70-680 exam. I have to say i didn’t know that Windows 7 was capable of doing some of the stuff I’ve read about!

Guss now i can call myself a “Microsoft Certified Professional” :).

For people who are looking for resources for the exam, here is a list of things i found useful:

Technology related

Security vulnerability? Antivirus and my keylogger…

Just out of curiosity i wanted to learn how to make a key-logger… In less than 2 hours I was able to make a program that detects keystrokes and emails them to me.

At first my antivirus did pick it up, but soon I found out that was because my program was trying to  write to a file that was already open. After a minute of re-programming i fixed that hole, and now the program can run easily without being picked up by my antivirus..

Then i thought it wasn’t catching it because i was the author of the program and my antivirus (AVG Free 2011) knows that, so it doesn’t catch it on my computer, but i proved that assumption wrong by successfully running it on my net-book which is using Windows 7 starter and Microsoft security essentials.

The program is about 11kb is size and take around 5-8mb of memory to run. Most people wouldn’t bother to check Task Manager to see what process are running, and therefore not even realize its running.

Example of the email i get:

“LButton LButton ShiftKey LShiftKey T H I S Space I S Space A N Space E X A M P L E Space O F Space A Space S Back F I L E Space T H A T Space I S Space S E N T Space T O Space M E Space V I A Space E M A I L OemPeriod OemPeriod OemPeriod Space I OemQuotes M Space S U R P R I S E D Space T H A T Space N O Space A N T I V I R U S Space P R O G R A M Space H A S Space D E T E D Back C T E D Space I T Space Y E Y Back T OemPeriod OemPeriod OemPeriod Space L O O K Space M Y Space P A S W O R D Space I S ShiftKey LShiftKey OemSemicolon Space M M M A J H F C U A B C G J K ShiftKey LShiftKey D2 D7 D7 D3 J LButton LButton LButton LButton RButton LButton”

So am I missing something? Because I really don’t understand how I was able to make such a program in less than 2 hours.