You can use CsConsoleFormat† library to format console output with colors.
Here's what your code would look like:
using static System.ConsoleColor;
ConsoleRenderer.RenderDocument(new Document().AddChildren(
new Span(">>> Order: ") { Color = Cyan },
new Span("Data") { Color = Gray },
new Span("Parity") { Color = DarkGreen },
new Span(" <<<") { Color = Cyan }
));
or, alternatively:
ConsoleRenderer.RenderDocument(new Document().AddChildren(
new Span { Color = Cyan }.AddChildren(
">>> Order: ",
new Span("Data") { Color = Gray },
new Span("Parity") { Color = DarkGreen },
" <<<"
)
));
or, with helper code (see below):
ColoredSpans.Render(">>> Order: ".Cyan(), "Data".Gray(), "Parity".DarkGreen(), " <<<".Cyan());
The top two methods, while not being very conscise, have many benefits:
- Make the intetion clear and support hierarchy, for example, child spans added to a parent span will inherit the parent's colors.
- Unlike
ColoredConsoleWrite
helper method, generate a complete document which can be adjusted as a whole (for example, word wrapping, background).
- Don't rely on "inverse console color" concept, which is alien for .NET developers on Windows, to set background color.
If you want more conscise code (for example, you write a lot of formatted paragraphs with many highlighted words), you can use this collection of helper methods, inspired by Edokan.KaiZen.Colors, to write code like in the 3rd example above:
public static class Spans
{
public static Span Black(this string text) => new Span(text) { Color = ConsoleColor.Black };
public static Span DarkBlue(this string text) => new Span(text) { Color = ConsoleColor.DarkBlue };
public static Span DarkGreen(this string text) => new Span(text) { Color = ConsoleColor.DarkGreen };
public static Span DarkCyan(this string text) => new Span(text) { Color = ConsoleColor.DarkCyan };
public static Span DarkRed(this string text) => new Span(text) { Color = ConsoleColor.DarkRed };
public static Span DarkMagenta(this string text) => new Span(text) { Color = ConsoleColor.DarkMagenta };
public static Span DarkYellow(this string text) => new Span(text) { Color = ConsoleColor.DarkYellow };
public static Span Gray(this string text) => new Span(text) { Color = ConsoleColor.Gray };
public static Span DarkGray(this string text) => new Span(text) { Color = ConsoleColor.DarkGray };
public static Span Blue(this string text) => new Span(text) { Color = ConsoleColor.Blue };
public static Span Green(this string text) => new Span(text) { Color = ConsoleColor.Green };
public static Span Cyan(this string text) => new Span(text) { Color = ConsoleColor.Cyan };
public static Span Red(this string text) => new Span(text) { Color = ConsoleColor.Red };
public static Span Magenta(this string text) => new Span(text) { Color = ConsoleColor.Magenta };
public static Span Yellow(this string text) => new Span(text) { Color = ConsoleColor.Yellow };
public static Span White(this string text) => new Span(text) { Color = ConsoleColor.White };
public static void Render(object[] elements) => ConsoleRenderer.RenderDocument(new Document().AddChildren(elements));
}
† CsConsoleFormat was developed by me.