2016-07-19 78 views
2

如何使用格式化代码格式化字符串以使Windows cmd具有格式化字符串?基于控制台格式化代码的格式字符串


所以基本上我有一个像~b~Hello World~r~这样的字符串。

当我输出到cmd时,它应该显示为<blue from here on>Hello World<reset to normal>


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


因此,我想到的是:

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

是设置“Console.BackgroundColor”还是“Console.ForegroundColor”,然后调用Console.ResetColor(),如[this](http://stackoverflow.com/questions/6859483/)如何对变化的前景和背景,字体颜色,在控制台)? –

+0

请不要在Stack Overflow上使用txtspk。例如,“some1”不是英语中的单词。另外,当提到你自己时,I是大写的。 –

+0

我们是在堆栈溢出还是语法溢出? – Bluscream

回答

2

据我所知,Windows控制台应用程序中没有这样的控制代码,如cmd.exe。有一些创造性的方法可以达到类似的结果。其中之一是How to echo with different colors in the Windows command line。我出于好奇而尝试了它,它工作得很好。它使用了一些jscript魔法。对于日常使用,如果您想要转义代码格式化功能,您可能会发现其中一种bash shell模拟器更有用。 (How to develop in Linux-Like Shell (bash) on Windows?

UPDATE:

我扔在一起的东西非常快速和肮脏展示一种方法以类似于您在使用的问题一个一个的风格用“代码”。这可能不是“最好”的方式。但它可能引发一个想法。

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

我认为可能有一种方法来获取正则表达式来捕获前导文本。但是我没有太多时间去尝试。我有兴趣看看是否有一种模式可以让正则表达式完成所有的工作。

+0

嗯,这听起来像我必须开发一个系统来拆分字符串那里,并使用控制台。为它的每个部分写():c – Bluscream

+0

您原来的文章使得它听起来像您想要的(ANSI)转义代码,您可以在cmd.exe中运行自己的脚本。但现在看起来更像是你正在开发一些东西供其他人使用。在这种情况下,您可以轻松创建一些可以模拟代码的扩展或实用程序。但是它总是归结为设置BG和FG颜色,然后编写一个字符串,除了其他注释中提到的情况(例如Windows 10)。如果这是一个C#应用程序打算作为控制台应用程序运行,那么我可能会有一个扩展的例子。 – rhaben

2

我想你说的是ANSI转义代码。你可以阅读关于他们here

基本上你只是将ESCAPE字符('\ x1b'作品)发送到控制台,然后是一个'['字符。然后你发送你想要的颜色值,然后输入'm'。

喜欢的东西:

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

Windows控制台的支持是相当有限,除非你明确地打开它。我相信Windows 10支持ANSI转义代码。

+0

对不起,我不得不为你撤销'最佳答案',但我只是测试它,并没有奏效。 [截图](http://prntscr.com/bv9f8j) – Bluscream

+0

好处是,它在conEMU [截图](http://prntscr.com/bv9iyy)中工作,但因为我想发布这个作为服务器应用程序我需要另一种方案 – Bluscream