【发布时间】:2010-04-30 08:40:14
【问题描述】:
写一个小命令行工具,输出不同颜色就好了。这可能吗?
【问题讨论】:
写一个小命令行工具,输出不同颜色就好了。这可能吗?
【问题讨论】:
是的。请参阅此article。这是那里的一个例子:
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
【讨论】:
class Program
{
static void Main()
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
Console.ResetColor();
}
}
取自here。
【讨论】:
以上 cmets 都是可靠的响应,但请注意它们不是线程安全的。如果您使用多个线程写入控制台,更改颜色将添加一个竞争条件,可能会创建一些看起来很奇怪的输出。不过修复起来很简单:
public class ConsoleWriter
{
private static object _MessageLock= new object();
public void WriteMessage(string message)
{
lock (_MessageLock)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}
}
}
【讨论】:
Write 调用将不会出现乱码。见stackoverflow.com/questions/4812508/…。只是写调用的顺序不“安全”。
我创建了一个small plugin(在NuGet 上可用),它允许您向控制台输出添加任何(如果您的终端支持)颜色,而不受经典的限制解决方案。
它通过扩展String 对象来工作,语法非常简单:
"colorize me".Pastel("#1E90FF");
支持前景色和背景色。
【讨论】:
是的,这很容易而且可能。定义第一个默认颜色。
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.Clear() 设置新的控制台颜色很重要。如果您不执行此步骤,您可以在使用 Console.ReadLine() 询问值时看到组合颜色。
然后您可以更改每个打印件的颜色:
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Red text over black.");
完成程序后,请记住在完成时重置控制台颜色:
Console.ResetColor();
Console.Clear();
现在使用 netcore,如果您想“保留”用户体验,我们还有另一个问题,因为终端在每个操作系统上都有不同的颜色。
我正在制作一个用文本格式解决这个问题的库:颜色、对齐方式等等。随意使用和贡献。
https://github.com/deinsoftware/colorify/ 也可以作为 NuGet package 使用
【讨论】:
这是我编写的一个简单的方法,用于编写带有内联颜色更改的控制台消息。它只支持一种颜色,但它符合我的需求。
// usage: WriteColor("This is my [message] with inline [color] changes.", ConsoleColor.Yellow);
static void WriteColor(string message, ConsoleColor color)
{
var pieces = Regex.Split(message, @"(\[[^\]]*\])");
for(int i=0;i<pieces.Length;i++)
{
string piece = pieces[i];
if (piece.StartsWith("[") && piece.EndsWith("]"))
{
Console.ForegroundColor = color;
piece = piece.Substring(1,piece.Length-2);
}
Console.Write(piece);
Console.ResetColor();
}
Console.WriteLine();
}
【讨论】:
只是在上面的答案中添加所有使用Console.WriteLine:在同一行文本上更改颜色,例如:
Console.Write("This test ");
Console.BackgroundColor = bTestSuccess ? ConsoleColor.DarkGreen : ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine((bTestSuccess ? "PASSED" : "FAILED"));
Console.ResetColor();
【讨论】:
是的,可以如下。这些颜色可以在控制台应用程序中使用,以红色等形式查看一些错误。
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;//after this line every text will be white on blue background
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
Console.ResetColor();//reset to the defoult colour
【讨论】:
同时为多个单词着色的示例方法。
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));
输出:
【讨论】:
当我想使用Console.WriteLine(); 时,我确实只想调整文本颜色
所以我不得不写
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("my message");
Console.ResetColor();
每次都想写点什么
所以我发明了我的 WriteLine() 方法,并一直在 Program 类中使用它而不是 Console.WriteLine()
public static void WriteLine(string buffer, ConsoleColor foreground = ConsoleColor.DarkGreen, ConsoleColor backgroundColor = ConsoleColor.Black)
{
Console.ForegroundColor = foreground;
Console.BackgroundColor = backgroundColor;
Console.WriteLine(buffer);
Console.ResetColor();
}
为了更简单,我还写了一个Readline() 方法,如下所示:
public static string ReadLine()
{
var line = Console.ReadLine();
return line ?? string.Empty;
}
所以现在我们必须在控制台中写入或读取内容:
static void Main(string[] args) {
WriteLine("hello this is a colored text");
var answer = Readline();
}
【讨论】:
我为彩色控制台输出开发了一个名为 cConsole 的小型有趣类库。
用法示例:
const string tom = "Tom";
const string jerry = "Jerry";
CConsole.WriteLine($"Hello {tom:red} and {jerry:green}");
它使用 C# FormattableString、IFormatProvider 和 ICustomFormatter 接口的一些功能来设置文本切片的前景色和背景色。
可以看cConsole源码here
【讨论】: