Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vborovikov/termly
Terminal colorizer for .NET console apps
https://github.com/vborovikov/termly
ansi cli colorize console csharp dotnet terminal tui
Last synced: about 2 months ago
JSON representation
Terminal colorizer for .NET console apps
- Host: GitHub
- URL: https://github.com/vborovikov/termly
- Owner: vborovikov
- License: mit
- Created: 2021-11-22T19:38:44.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-13T15:15:21.000Z (8 months ago)
- Last Synced: 2024-05-13T16:41:41.564Z (8 months ago)
- Topics: ansi, cli, colorize, console, csharp, dotnet, terminal, tui
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Termly
Terminal colorizer for .NET console apps[![NuGet](https://img.shields.io/nuget/dt/Termly.svg)](https://www.nuget.org/packages/Termly)
[![NuGet](https://img.shields.io/nuget/v/Termly.svg)](https://www.nuget.org/packages/Termly)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](https://docs.microsoft.com/en-us/dotnet/api/system.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.
# Usage
You can start adding color to the console output with `InColor` method extension.
```csharp
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.
```csharp
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.
```csharp
Console.Out.WriteLineInColor($"Count: {count:blue} Total: {total:white|green}");
```Background colors are supported too.
```csharp
Console.Error.WriteLine("ALERT".InColor(foreground: ConsoleColor.Black, background: ConsoleColor.Red));
Console.Error.WriteLineInColor($"{"ALERT":black|red}");
```