基于控制台的格式代码格式字符串 [英] Format String based on Formatting codes for console

查看:226
本文介绍了基于控制台的格式代码格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何格式化格式代码的字符串具有用于Windows CMD?






所以基本上格式化字符串我有一个像一b〜的Hello World〜R〜



字符串

当我输出到CMD它应该显示为<的Hello World<从这里开始>蓝色恢复到正常方式>






据我knwo,CMD有一定的Unicode字符更改下列文本格式,但我不记得他们(像 \\\#4






所以,我想到的是:

 公共字符串的formatString(字符串输入)
{
输入= Regex.Replace(输入一b〜,< unicode的蓝色>中,RegexOptions。 IGNORECASE);
输入= Regex.Replace(输入,〜R〜,&下; Unicode的复位>中,RegexOptions.IgnoreCase);
返回输入;
}


解决方案

据我所知,有在Windows控制台应用程序,例如cmd.exe的没有这样的控制代码。有一些创造性的方式来达到类似的效果。其中之一是如何使用不同的颜色呼应在Windows命令行。我尝试过了好奇和它工作得很好。它使用一些魔法的JScript。对于日常使用你可能会发现在bash shell模拟器之一,如果你想逃避代码格式化功能更加有用。 (如何在Linux中,像壳牌开发(庆典)在Windows



更新:



我扔在一起一些非常快速和肮脏展示一种方法以类似于一个你在问题中使用的样式使用代码。这未必是最佳的方式。但它也可能引发一个想法。

 类节目
{
静态无效的主要(字串[] args )
{
@
这是〜R〜红~~这是一b〜蓝色~~这只是一些文字
去解决问题有点〜摹〜现在有点绿〜
.WriteToConsole()。
Console.ReadKey();
}
}

静态公共类StringConsoleExtensions
{
私人静态只读字典<字符串,ConsoleColor>色彩表=新词典与LT;字符串,ConsoleColor>
{
{R,ConsoleColor.Red}
{B,ConsoleColor.Blue}
{G,ConsoleColor.Green}
{W,ConsoleColor.White}
};
静态公共无效WriteToConsole(此字符串值)
{
变种位置= 0;
的foreach(赛的比赛中Regex.Matches(值,@〜(R | G | C | W)〜([^〜] *)~~))
{
变种leadingText = value.Substring(位置,match.Index - 位);
位置+ = leadingText.Length + match.Length;
Console.Write(leadingText);
VAR currentColor = Console.ForegroundColor;

{
Console.ForegroundColor =颜色表[match.Groups [1] .value的]。
Console.Write(match.Groups [2]。价值);
}
终于
{
Console.ForegroundColor = currentColor;
}
}
如果(仓位< value.Length)
{
Console.Write(value.Substring(位置,value.Length - 位置));
}
}
}



我觉得有可能是办法让正则表达式来捕捉龙头文本。但我没有很多时间进行实验。我很想看看是否有这将使正则表达式来完成所有的工作模式。


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;
}

解决方案

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.

这篇关于基于控制台的格式代码格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆