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 any terminal, using the user's favorite color scheme.
You can start adding color to the console output with Print
, PrintLine
, and InColor
extension methods.
private static void HandleContextLog(object sender, LoggingEventArgs e)
{
if (e.Kind > ChannelMessageKind.Trace)
{
// using PrintLine method
Console.Error.PrintLine($"{e.Source:gray}: {e.RawMessage:darkBlue}");
// using InColor method
Console.Error.WriteLine($"{e.Source.InColor(ConsoleColor.Gray)}: {e.RawMessage.InColor(ConsoleColor.DarkBlue)}");
}
}
The overloaded extension methods Print
and PrintLine
are used to colorize interpolated string parameters with the same or different colors.
Console.Out.PrintLine(ConsoleColor.DarkYellow, $"Parameters Count: {parameters.Statistics.ParametersCount}");
Console.Out.PrintLine($"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.PrintLine($"{"ALERT":black|red}");