写一个小的命令行工具,用不同的颜色输出会很好。这可能吗?
当前回答
class Program
{
static void Main()
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
Console.ResetColor();
}
}
从这里拍的。
其他回答
一种同时给多个单词上色的示例方法。
private static void WriteColor(string str, params (string substring, ConsoleColor color)[] colors)
{
var words = Regex.Split(str, @"( )");
foreach (var word in words)
{
(string substring, ConsoleColor color) cl = colors.FirstOrDefault(x => x.substring.Equals("{" + word + "}"));
if (cl.substring != null)
{
Console.ForegroundColor = cl.color;
Console.Write(cl.substring.Substring(1, cl.substring.Length - 2));
Console.ResetColor();
}
else
{
Console.Write(word);
}
}
}
用法:
WriteColor("This is my message with new color with red", ("{message}", ConsoleColor.Red), ("{with}", ConsoleColor.Blue));
输出:
我开发了一个名为cConsole的有趣的小类库,用于彩色控制台输出。 使用示例:
const string tom = "Tom";
const string jerry = "Jerry";
CConsole.WriteLine($"Hello {tom:red} and {jerry:green}");
它使用c# FormattableString, IFormatProvider和ICustomFormatter接口的一些功能来设置文本切片的前景和背景颜色。 你可以在这里看到cConsole的源代码
只是补充上面的答案,所有使用控制台。要改变同一行文本的颜色,可以这样写:
Console.Write("This test ");
Console.BackgroundColor = bTestSuccess ? ConsoleColor.DarkGreen : ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine((bTestSuccess ? "PASSED" : "FAILED"));
Console.ResetColor();
class Program
{
static void Main()
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
Console.ResetColor();
}
}
从这里拍的。
我发现给控制台输出片段着色的最简单方法是在Windows控制台中使用ANSI转义序列。
public static int Main(string[] args)
{
string NL = Environment.NewLine; // shortcut
string NORMAL = Console.IsOutputRedirected ? "" : "\x1b[39m";
string RED = Console.IsOutputRedirected ? "" : "\x1b[91m";
string GREEN = Console.IsOutputRedirected ? "" : "\x1b[92m";
string YELLOW = Console.IsOutputRedirected ? "" : "\x1b[93m";
string BLUE = Console.IsOutputRedirected ? "" : "\x1b[94m";
string MAGENTA = Console.IsOutputRedirected ? "" : "\x1b[95m";
string CYAN = Console.IsOutputRedirected ? "" : "\x1b[96m";
string GREY = Console.IsOutputRedirected ? "" : "\x1b[97m";
string BOLD = Console.IsOutputRedirected ? "" : "\x1b[1m";
string NOBOLD = Console.IsOutputRedirected ? "" : "\x1b[22m";
string UNDERLINE = Console.IsOutputRedirected ? "" : "\x1b[4m";
string NOUNDERLINE = Console.IsOutputRedirected ? "" : "\x1b[24m";
string REVERSE = Console.IsOutputRedirected ? "" : "\x1b[7m";
string NOREVERSE = Console.IsOutputRedirected ? "" : "\x1b[27m";
Console.WriteLine($"This is {RED}Red{NORMAL}, {GREEN}Green{NORMAL}, {YELLOW}Yellow{NORMAL}, {BLUE}Blue{NORMAL}, {MAGENTA}Magenta{NORMAL}, {CYAN}Cyan{NORMAL}, {GREY}Grey{NORMAL}! ");
Console.WriteLine($"This is {BOLD}Bold{NOBOLD}, {UNDERLINE}Underline{NOUNDERLINE}, {REVERSE}Reverse{NOREVERSE}! ");
}
输出:
NOBOLD代码实际上是“正常强度”。详见维基百科链接页面上的“SGR(选择图形再现)参数”部分。
重定向测试避免将转义序列输出到文件中,如果输出被重定向。如果用户有一个非黑白配色方案,它将不会被重置,但你可以使用控制台功能来保存/恢复用户的配色方案在程序的开始和结束,如果这很重要的话。