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

Using the VLC ActiveX control in WPF

How to integrate the powerful VLC media player into your WPF application

Prerequisites:

  • Windows version of VLC Media Player with the ActiveX component installed.
  • Microsoft Visual Studio 2010 professional / Microsoft Visual C# 2010 Express (Don’t know if this method works on older versions).

Instructions:

  1. If you are not adding this to a current project then create a Visual C# > Windows > WPF Application.
  2. Add a User Control to your project (Project > Add New Item > Visual C# > User Control).
  3. Open the Toolbox (if you can’t see this: View > Toolbox).
  4. Right Click anywhere on the Toolbox then click on Choose Items. (This may take a while to open)
  5. Navigate to the COM Components tab, find and check “VideoLAN VLC ActiveX plugin v1” (axvlc.dell) and press OK.
  6. Now in your Toolbox you should see “VideoLAN VLC ActiveX plugin v1”. Double click on it and set its Dock Property to “Fill”
  7. Now save an build your project (Build > Build Solution.. or press F6)
  8. In the Solution Explorer under References you should now see “AXVLC” and “AxAXVLC”. if you don’t add them manually by right clicking on References, go into your projects debug folder and add “AxInterop.AXVLC.dll” and “Interop.AXVLC.dll”.
  9. Right clicking on References in the Solution Explorer, then Add Reference; Browse to the .NET Tab and find and select “WindowsFormsIntegration”, then press OK.
  10. Now in MainWindow.xaml (or where ever you want to add it to), Add a name to your grid (or the container)… i named mine “grid1”.
  11. From the toolbox add a WindowsFormsHost to the grid, and name it. I named mine “WFH1”  (see Figure 1)
  12. Now right click on your main window and click on View Code.
  13. Declare “AxAXVLC.AxVLCPlugin” as a global variable. (AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();
  14. in your default constructor add the player object as the child of the WindowsFormsHost (WFH1.Child = vlcPlayer;)
  15. And that’s it! you wont see anything when you run the program, but the player is there, i will post a code snippet below so you can get a file to open and play in it (Figure 2).
  16. See http://wiki.videolan.org/ActiveX for instructions on how to use the VLC media player ActiveX Plugin.

UPDATE 16-OCT-2011: (List of Fields/Methods/Events of the Plug-in)
Here are a list of fields/methods/events that should allow you to have a lot of control over the plug-in:
Fields

  • bool AutoLoop { get; set; }
  • bool AutoPlay { get; set; }
  • bool CtlVisible { get; set; }
  • int Length { get; }
  • string MRL { get; set; }
  • bool Playing { get; }
  • int PlaylistCount { get; }
  • int PlaylistIndex { get; }
  • float Position { get; set; }
  • int Time { get; set; }
  • string VersionInfo { get; }
  • int Volume { get; set; }

Events

  • MediaPlayerBackward;
  • MediaPlayerBuffering;
  • MediaPlayerEncounteredError;
  • MediaPlayerEndReached;
  • MediaPlayerForward;
  • MediaPlayerNothingSpecial;
  • MediaPlayerOpening;
  • MediaPlayerPausableChanged;
  • MediaPlayerPaused;
  • MediaPlayerPlaying;
  • MediaPlayerPositionChanged;
  • MediaPlayerSeekableChanged;
  • MediaPlayerStopped;
  • MediaPlayerTimeChanged;
  • pauseEvent;
  • playEvent;
  • stopEvent;

Methods

  • void addTarget(string uri, object options, VLCPlaylistMode mode, int position);
  • void AttachInterfaces();
  • void CreateSink();
  • void DetachSink();
  • void fullscreen();
  • object getVariable(string name);
  • void pause();
  • void play();
  • void playFaster();
  • void playlistClear();
  • void playlistNext();
  • void playlistPrev();
  • void playSlower();
  • void setVariable(string name, object value);
  • void shuttle(int seconds);
  • void stop();
  • void toggleMute();
Figuare 1

Example of further useage:

I just added a button to my application to open allow the user to select a file and play it in the player:

[Updated 08 Oct 2012, courtesy of bielb89 from YouTube]

VLC V2.x.x.x +You will need to add “file:///” +before the Url for the file.

Figure 2.. Click on the more button to see a written version of the code.. [This Picture is outdated]
using System.Windows;
using System.Windows.Forms;

namespace WPFVLC
{
    ///
<summary> /// Interaction logic for MainWindow.xaml
 /// </summary>
    public partial class MainWindow : Window
    {
        AxAXVLC.AxVLCPlugin vlcPlayer = new AxAXVLC.AxVLCPlugin();
        public MainWindow()
        {
            InitializeComponent();
            WFH1.Child = vlcPlayer;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string mrl = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            if (ofd.FileName != "")
            {
                mrl = ofd.FileName;
                vlcPlayer.addTarget("file:///" +mrl, null, AXVLC.VLCPlaylistMode.VLCPlayListReplaceAndGo, 0);
                vlcPlayer.play();
            }
        }
    }
}