Reputation: 253
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;
}
Upvotes: 2
Views: 372
Reputation: 159
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.
Upvotes: 2
Reputation: 19496
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.
Upvotes: 2