Toolbelt.AnsiEscCode.Colorize 1.0.0

dotnet add package Toolbelt.AnsiEscCode.Colorize --version 1.0.0                
NuGet\Install-Package Toolbelt.AnsiEscCode.Colorize -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Toolbelt.AnsiEscCode.Colorize" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolbelt.AnsiEscCode.Colorize --version 1.0.0                
#r "nuget: Toolbelt.AnsiEscCode.Colorize, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Toolbelt.AnsiEscCode.Colorize as a Cake Addin
#addin nuget:?package=Toolbelt.AnsiEscCode.Colorize&version=1.0.0

// Install Toolbelt.AnsiEscCode.Colorize as a Cake Tool
#tool nuget:?package=Toolbelt.AnsiEscCode.Colorize&version=1.0.0                

ANSI Escape Codes - Colorize NuGet Package

Summary

Very simple and small .NET library to just only append ANSI color escape sequence to a string.

Usage

Step 1. Install this package to the project.

If you use dotnet CLI, enter the command below at the project folder to do it.

> dotnet add package Toolbelt.AnsiEscCode.Colorize

Step 2. Add a declaration to your C# source code to make this library available, like this.

// Top of your C# source file (.cs)
using System;
...
// 👇 Add this line.
using static Toolbelt.AnsiEscCode.Colorize;
...

Step 3. Just surround a text what you want to be colored by static methods that name is the color name. (Those static methods are provided by Colorize static class.)

For example, if you implemented like this code,

Console.WriteLine($"Hello,{Red("World")}");

You will get this.

fig.1

How does it work

What the static methods of Colorize static class does is, just surround a text by ANSI color escape sequence code.

Red("World") ⇒ "\x1b[91m" + "World" + "\x1b[0m"

Notice for Windows OS users

Unfortunately, some console environments on Windows OS have not enabled the ANSI escape code feature by default.

Therefore, to enable it, you may have to implement some code for enabling the ANSI escape code feature.

For example, at first, add a C# code as below to your project,

// Add this C# code to your project.
using System;
using System.Runtime.InteropServices;

public static class AnsiCodeEnabler
{
  [DllImport("kernel32.dll")]
  private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

  [DllImport("kernel32.dll")]
  private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

  [DllImport("kernel32.dll", SetLastError = true)]
  private static extern IntPtr GetStdHandle(int nStdHandle);

  [DllImport("kernel32.dll")]
  private static extern uint GetLastError();

  public static void Enable()
  {
    const int STD_OUTPUT_HANDLE = -11;
    const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
    const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;

    var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleMode(iStdOut, out uint outConsoleMode))
    {
      outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
      SetConsoleMode(iStdOut, outConsoleMode);
    }
  }
}

and next, invoke it at the beginning of your program.

class Program
{
  static void Main(string[] args)
  {
    AnsiCodeEnabler.Enable(); // 👈 Invoke it.
    ...

License

The Unlicense

(Because, it's too small, it's too simple, and more, the originality in this library is almost none.)

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 2,110 9/2/2020

v.1.0.0
- 1st release.