You can use the CsConsoleFormat † library to format console output with colors.
Here is 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, using an auxiliary code (see below):
ColoredSpans.Render(">>> Order: ".Cyan(), "Data".Gray(), "Parity".DarkGreen(), " <<<".Cyan());
The two best methods, although not very narrow, have many advantages:
- Make intuition clear and maintain a hierarchy, for example, child intervals added to the parent range inherit parent colors.
- Unlike the
ColoredConsoleWrite helper method, generate a complete document that can be adjusted as a whole (for example, word wrap, background). - Do not rely on the concept of "reverse console color," which is alien to .NET developers on Windows to set the background color.
If you need more specific code (for example, you write a lot of formatted paragraphs with many words highlighted), you can use this collection of helper methods inspired by Edokan.KaiZen.Colors to write code, as in the third 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.
source share