Mapping Console Text Coloring and Highlighting to a RichTextBox in Windows Forms

55 Views Asked by At

Is it possible to replicate the coloring and highlighting of text from the console to a RichTextBox in Windows Forms? This is what the output of the console application I'm trying to replicate in a RichTextBox looks like:

https://i.imgur.com/GudhbzE.png

And this is what the output looks like in the RichTextBox:

[38;5;218m[    c-server-monitor] [0m[0m[32m  _______  ______                           
[38;5;218m[    c-server-monitor] [0m[32m |  ___\ \/ / ___|  ___ _ ____   _____ _ __ 
[38;5;218m[    c-server-monitor] [0m[32m | |_   \  /\___ \ / _ \ '__\ \ / / _ \ '__|
[38;5;218m[    c-server-monitor] [0m[32m |  _|  /  \ ___) |  __/ |   \ V /  __/ |   
[38;5;218m[    c-server-monitor] [0m[32m |_|   /_/\_\____/ \___|_|    \_/ \___|_|   
[38;5;218m[    c-server-monitor] [0m[32m-------------------------------- [93mmonitor[32m ---[0m
[38;5;218m[    c-server-monitor] [0m[0m
[38;5;161m[    c-scripting-core] [0m[0mCreating script environments for monitor
]0;txAdmin v6.0.2: default [17:19:25][tx:v6.0.2] Profile 'default' starting...
[17:19:25][tx:WebServer] Listening on 0.0.0.0.
[17:19:25][tx:UpdateChecker] This version of txAdmin is outdated.
[17:19:25][tx:UpdateChecker] Please update as soon as possible.
[17:19:25][tx:UpdateChecker] For more information: https://discord.gg/uAmsGa2
[17:19:26][tx] 
[17:19:26][tx]    ┏â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”┓
[17:19:26][tx]    â”                                    â”
[17:19:26][tx]    â”     All ready! Please access:      â”
[17:19:26][tx]    â”      http://localhost:40120/       â”
[17:19:26][tx]    â”    http://85.221.132.150:40120/    â”
[17:19:26][tx]    â”                                    â”
[17:19:26][tx]    â”—â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”›
[17:19:26][tx] 
[17:19:26][tx] [!] Home-hosting fxserver is not recommended [!]
[17:19:26][tx] You need to open the fxserver port (usually 30120) on Windows Firewall
[17:19:26][tx] and port forward it in your router for other players be able to access it.
[17:19:26][tx] We recommend renting a server from  https://zap-hosting.com/txAdmin .
[17:19:26][tx:FXRunner] Please open txAdmin on the browser to configure your server.

I realize that it might not be possible to replicate this exactly, but I know that text can be colored/highlighted in a RichTextBox, so creating a similar format should be possible. I would be very grateful for some example code. Here is my current code:

private void button1_Click(object sender, EventArgs e)
{
    Task.Run(() => StartConsoleApp("FXServer.exe"));
}

private void StartConsoleApp(string exePath)
{
    Process process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = exePath,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        }
    };

    process.OutputDataReceived += (sender, args) => AppendText(args.Data, Color.White);
    process.ErrorDataReceived += (sender, args) => AppendText(args.Data, Color.Red);

    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
}

private void AppendText(string text, Color color)
{
    if (text == null)
        return;

    if (InvokeRequired)
    {
        Invoke(new Action(() => AppendText(text, color)));
    }
    else
    {
        richTextBoxConsoleOutput.SelectionStart = richTextBoxConsoleOutput.TextLength;
        richTextBoxConsoleOutput.SelectionLength = 0;

        richTextBoxConsoleOutput.SelectionColor = color;
        richTextBoxConsoleOutput.AppendText(text + Environment.NewLine);
        richTextBoxConsoleOutput.SelectionColor = richTextBoxConsoleOutput.ForeColor;

        richTextBoxConsoleOutput.ScrollToCaret();
    }
}
0

There are 0 best solutions below