2

How can I format a string with formatting codes to have a formatted string for the windows cmd?


So basically I have a string like ~b~Hello World~r~.

When I output it to the cmd it should show up as <blue from here on>Hello World<reset to normal>.


As far as I knwo, cmd has some Unicode chars to change the following text formatting but I don't remember them (something like \u00234)


So what I think of is:

public string FormatString(string input)
{
    input = Regex.Replace(input, "~b~", "<unicode for blue>", RegexOptions.IgnoreCase);
    input = Regex.Replace(input, "~r~", "<Unicode for reset>", RegexOptions.IgnoreCase);
    return input;
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Bluscream
  • 255
  • 1
  • 3
  • 18

2 Answers2

2

I think you're talking about ANSI escape codes. You can read about them here.

Basically you just send the ESCAPE character ('\x1b' works) to the console, followed by a '[' character. Then you send the color values you want, followed by a 'm'.

Something like:

Console.WriteLine("\x1b[31mRed\x1b[0;37m");

Windows console support is pretty limited unless you explicitly turn it on. I believe Windows 10 supports ANSI escape codes out of the gate.

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • Im sorry that i had to revoke the 'best answer' for you, but i just tested it and it didn't work. [screenshot](http://prntscr.com/bv9f8j) – Bluscream Jul 20 '16 at 14:16
  • The good thing is that it did work in conEMU [screenshot](http://prntscr.com/bv9iyy) but since i want to release this as server application i need another solution. – Bluscream Jul 20 '16 at 14:24
2

To my knowledge, there are no such control codes in Windows console applications such as cmd.exe. There are some creative ways to achieve a similar result. One of these is How to echo with different colors in the Windows command line. I tried it out of curiosity and it works just fine. It uses some jscript magic. For everyday use you might find one of the bash shell emulators more useful if you want escape code formatting capabilities. (How to develop in Linux-Like Shell (bash) on Windows?)

UPDATE:

I threw together something very quick and dirty to demonstrate one approach to using "codes" in a style similar to one you used in the question. This may not be the "best" way. But it might spark an idea.

class Program
{
    static void Main(string[] args)
    {
        @"
This is in ~r~red~~ and this is in ~b~blue~~. This is just some more text 
to work it out a bit. ~g~And now a bit of green~~.
".WriteToConsole();
        Console.ReadKey();
    }
}

static public class StringConsoleExtensions
{
    private static readonly Dictionary<string, ConsoleColor> ColorMap = new Dictionary<string, ConsoleColor>
    {
        { "r", ConsoleColor.Red },
        { "b", ConsoleColor.Blue },
        { "g", ConsoleColor.Green },
        { "w", ConsoleColor.White },
    };
    static public void WriteToConsole(this string value)
    {
        var position = 0;
        foreach (Match match in Regex.Matches(value, @"~(r|g|b|w)~([^~]*)~~"))
        {
            var leadingText = value.Substring(position, match.Index - position);
            position += leadingText.Length + match.Length;
            Console.Write(leadingText);
            var currentColor = Console.ForegroundColor;
            try
            {
                Console.ForegroundColor = ColorMap[match.Groups[1].Value];
                Console.Write(match.Groups[2].Value);
            }
            finally
            {
                Console.ForegroundColor = currentColor;
            }
        }
        if (position < value.Length)
        {
            Console.Write(value.Substring(position, value.Length - position));
        }
    }
}

I think there may be a way to get the regex to capture the leading text. But I didn't have a lot of time to experiment. I'd be interested to see if there is a pattern that would allow the regex to do all the work.

Community
  • 1
  • 1
rhaben
  • 159
  • 2
  • 9
  • hmmm, that sounds like i have to develop a system to split the string there and use Console.Write() for every part of it :c – Bluscream Jul 20 '16 at 14:19
  • Your original post made it sound like you wanted (ANSI) escape codes for scripts that you would run yourself in cmd.exe. But now it seems more like you are developing something for use by others. In that case, you could easily create some extensions or utilities that could mimic the codes. But it will always boil down to set the BG and FG colors then Write a string except the cases noted in other comments (e.g. Windows 10). I might have an example of an extension if this a C# application intended to run as a console app. – rhaben Jul 20 '16 at 16:34