Write to TortoiseHG GUI Console

Viewed 218

I've created a tool/plugin what can be launched within the TortoiseHG GUI, for a specific repository.

This tool is running a few time consuming tasks (3-4 minutes) in a .NET Console application (running in the bg) and I am wondering if and how I can write some data to the console of TortoiseHG. Just to notify the user.

I've found a interesting Mercurial.Net library: https://archive.codeplex.com/?p=mercurialnet and on the site in the discussions tab someone asked the same question:

Great library! How would one display messages to TortoiseHg? Either as a a message box or in the output log.

Someone answered:

It should be enough to just print them to the console, ie. either:
Console.Out.WriteLine("information");

But for me it is not working.

Here is the (simple) code I tried so far:

 Mercurial.Client.SetClientPath(@"C:\Program Files\TortoiseHg");
 Mercurial.Repository repo = new Repository(_documentDirectory);
 Console.Out.WriteLine("information");

Am I missing something?

EDIT

I created a custom Python extension for TortoiseHG, I saved it inside this folder: C:\Program Files\TortoiseHg\hgext and in the mercurial.ini file I added this:

[extensions]
logger = C:/Program Files/TortoiseHg/hgext/console-logger.py

from mercurial import registrar
from mercurial.i18n import _

cmdtable = {}
command = registrar.command(cmdtable)

@command('WriteToTortoise', [], norepo=True)
def WriteToTortoise(ui, value):
    ui.warn('INFO: ' + value + '\n\n')

This allows me to use the WriteToTortoise command: enter image description here

It can be use in cmd as well, only not jet visible in tortoise.

enter image description here

At least I made a test console app in C# (where the initial tool is running in) and have this code

class Program
{
    static void Main(string[] args)
    {
        Mercurial.Repository repo = new Repository(@"C:\Users\Rymo\source\repos\Test");

        Mercurial.Gui.GuiClient.ClientType = GuiClientType.PyQT;
        var cmd = new WriteGuiConsole();
        Mercurial.Gui.GuiClient.Execute(repo, cmd);
        //Mercurial.Gui.GuiClient.InitGui(repo);
        Console.ReadLine();
    }
}

My command (see cmd variable):

class WriteGuiConsole : IGuiCommand
{
    public string Command => "WriteToTortoise";

    public IEnumerable<string> Arguments => new string[] { "This message should be visible in Tortoise.." };

    public Collection<string> AdditionalArguments => new Collection<string>();

    public IMercurialCommandObserver Observer => new DebugObserver();

    public int Timeout => 3000;

    public void After(int exitCode, string standardOutput, string standardErrorOutput)
    {
        Console.WriteLine($"exitCode: {exitCode}");
        Console.WriteLine($"standardOutput: {standardOutput}");
        Console.WriteLine($"standardErrorOutput: {standardErrorOutput}");
    }

    public void Before()
    {
    }

    public void Validate()
    {
    }
}

public class CustomDebugObserver : IMercurialCommandObserver
{
    public void ErrorOutput(string line)
    {
        Console.WriteLine($"ErrorOutput: {line}");
    }

    public void Executed(string command, string arguments, int exitCode, string output, string errorOutput)
    {
        Console.WriteLine($"command: {command}");
        Console.WriteLine($"arguments: {arguments}");
        Console.WriteLine($"exitCode: {exitCode}");
        Console.WriteLine($"output: {output}");
        Console.WriteLine($"errorOutput: {errorOutput}");
    }

    public void Executing(string command, string arguments)
    {
        Console.WriteLine($"command: {command}");
        Console.WriteLine($"arguments: {arguments}");
    }

    public void Output(string line)
    {
        Console.WriteLine($"line: {line}");
    }
}

I can access all Tortoise features from the C# code (push/pull/revert etc.) but a simple console output seems a bridge too far at the moment.

EDIT Again a morning without success.. Pity.. Probably it's faster to redirect the tool its console output to a Windows Forms application to inform the user.

But there must be someone who can push me into the right direction, or tell me that I'am wasting my time =)..

EDIT This topic is posted on TortoiseHG Developers forum (Google groups) as well, when I got the answer I'll will update it here too.

EDIT I am getting closer.. I modified the Python extension as following:

    from mercurial import registrar
    from mercurial.i18n import _
    from subprocess import Popen, PIPE, STDOUT

    def uisetup(ui):
        p = Popen('ConsoleToTortoiseTest.exe', stdout = PIPE, stderr = STDOUT, shell = True)
        WriteToTortoise(ui, p.stdout.readline())

    cmdtable = {}
    command = registrar.command(cmdtable)

    @command('WriteToTortoise', [], norepo=True)
    def WriteToTortoise(ui, value):
        ui.warn('INFO: ' + value.replace('+', ' ') + '\n\n')

The issue now is that its not writing to the console real-time but when I call fe. the 'hg help' command, I guess this is because the hg command lauches the extension and then the extension is executing a subproces of the .NET application. Not the solutions for now.

enter image description here

Any advise will be appreciated.

0 Answers
Related