Terminal colorizer for .NET console apps
Termly provides an easy to use set of extension methods to display colorized text in the terminal. It doesn't support arbitrary colors, instead it reuses the same old ConsoleColor constants. The idea here is that you don't need to come up with your own unique color pallete to display textual data, you just continue using the colors the user already chose for their terminal. The colorization is achieved with the use of a small subset of ANSI escape codes.
Apps with Termly look great in Windows Terminal with your favorite color scheme.
You can start adding color to the console output with InColor
method extension.
private static void HandleContextLog(object sender, LoggingEventArgs e)
{
if (e.Kind > ChannelMessageKind.Trace)
{
// using InColor method
Console.Error.WriteLine($"{e.Source.InColor(ConsoleColor.Gray)}: {e.RawMessage.InColor(ConsoleColor.DarkBlue)}");
// using WriteLineInColor method
Console.Error.WriteLineInColor($"{e.Source:gray}: {e.RawMessage:darkBlue}");
}
}
The extension methods Write
and WriteLine
are used to colorize interpolated string parameters.
Console.Out.WriteLine(ConsoleColor.DarkYellow, $"Parameters Count: {parameters.Statistics.ParametersCount}");
There are also methods WriteInColor
and WriteLineInColor
which allow you to colorize each interpolated string parameter independently.
Console.Out.WriteLineInColor($"Count: {count:blue} Total: {total:white|green}");
Background colors are supported too.
Console.Error.WriteLine("ALERT".InColor(foreground: ConsoleColor.Black, background: ConsoleColor.Red));
Console.Error.WriteLineInColor($"{"ALERT":black|red}");