Skip to content

silkfire/Pastel

Repository files navigation

Pastel

logo

NuGet NuGet

Give your console app a nicer look by adding some color to the output it produces. This is achieved by wrapping strings of the output in ANSI escape sequences that instruct the terminal to color the string based on the interpreted code. Tested on both Windows (requires at least Windows 10, v1511 [November Update]) and Linux.

Introduction

Modern terminals have a feature that allows them to print text in different colors. To enable this, a string is wrapped with a special sequence of characters containing a directive to the terminal to color the string that follows and stop coloring when it encounters an end code. Producing these character sequences can be cumbersome, which is the reason why I decided to build this small library that turns this into a very easy task.
Because Pastel only alters the output string, there is no need to manipulate or extend the built-in System.Console class.

If your terminal doesn't support 24-bit colors, it will approximate to the nearest color instead.

How to use

The basic syntax is very simple. Use the Pastel() method on the string you want to colorize and supply a color argument.

"ENTER".Pastel(Color.FromArgb(165, 229, 250))

Console.WriteLine($"Press {"ENTER".Pastel(Color.FromArgb(165, 229, 250))} to continue");

Example 1

You can either use a System.Drawing.Color object, a System.ConsoleColor enum or a hexadecimal string value.
Both upper and lower case hex codes are supported and the leading number sign (#) is optional.

var spectrum = new (string color, string letter)[]
{
    ("#124542", "a"),
    ("#185C58", "b"),
    ("#1E736E", "c"),
    ("#248A84", "d"),
    ("#20B2AA", "e"),
    ("#3FBDB6", "f"),
    ("#5EC8C2", "g"),
    ("#7DD3CE", "i"),
    ("#9CDEDA", "j"),
    ("#BBE9E6", "k")
};

Console.WriteLine(string.Join("", spectrum.Select(s => s.letter.Pastel(s.color))));

Example 2

Example 3

Using a Color/ConsoleColor argument pairs very well with ReSharper as the extension automatically underlines the argument list and colors it accordingly:

ReSharper color object underlining

ConsoleColor and web colors

By default, a ConsoleColor argument emits the corresponding ANSI base color code, which lets the terminal render it using its own theme. Pass useWebColors: true to instead emit the fixed web color that the value represents:

"Colorize me".Pastel(ConsoleColor.Red);                      // Themed by the terminal
"Colorize me".Pastel(ConsoleColor.Red, useWebColors: true);  // Always #FF0000

The web colors follow the combined CSS3 list. For the two names where the web and X11 definitions disagree, the web value is used:

Color Pastel X11
Gray #808080 #BEBEBE
Green #008000 #00FF00 (the web's Lime)

An inherited quirk worth knowing about: DarkGray (#A9A9A9) is lighter than Gray (#808080). DarkGray descends from X11 while Gray descends from the web, and the combined list keeps both. This is intentional and matches System.Drawing.Color.

Note that these are not the Windows console palette values (where Gray is #C0C0C0 and DarkGray is #808080).

Colors outside the 16 ConsoleColor names — such as Maroon or Purple — are reached through the Color or hexadecimal overloads:

"Colorize me".Pastel(Color.Maroon);   // #800000
"Colorize me".Pastel("#B03060");      // X11 Maroon

Background colors

Pastel also supports background colors. The syntax is exactly the same except that the method is called PastelBg.
Both foreground and background colors can be combined by chaining the methods:

"Colorize me".Pastel(Color.Black).PastelBg("FFD000");

Example 4

Disabling / enabling color output

If you for any reason would like to disable any future color output produced by Pastel for the duration of your app, simply call ConsoleExtensions.Disable(). To re-enable color output, call ConsoleExtensions.Enable().

CI/CD environments

Pastel will detect if your application is running under a common CI/CD environment and will disable all coloring if this is the case.
If you'd like to override this check and force colors in CI/CD environments, you can set an environment variable named PASTEL_DISABLE_ENVIRONMENT_DETECTION (value does not matter).

NO_COLOR

Pastel will also honor systems where console color output has explicitly been requested to be turned off. See more information about this initiative at https://no-color.org.

About

Snazz up your console output!

Resources

License

Stars

428 stars

Watchers

10 watching

Forks

Sponsor this project

 

Packages

 
 
 

Contributors