Giter Site home page Giter Site logo

pastel's Introduction

Pastel

logo

NuGet NuGet fuget.org package last version

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 codes 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.

This library was inspired by Crayon, except that it has two main differences:

  1. Instead of calling the coloring method by using the name of a static class, Pastel provides an extension method on the String object, leaving you to just type the method name and supply the color argument.
  2. This library allows you to produce any color (then it's up to your terminal whether it can correctly interpret the code, provided that it supports 24-bit colors), whereas Crayon only gives you a small set of predefined colors to choose from.

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 or a hexadecimal string value.
Both upper and lower case hex codes are supported and the pound 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 argument pairs very well with ReSharper as the extension automatically underlines the argument list and colors it accordingly:

ReSharper color object underlining

Background colors

Pastel now 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 some reason want 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().

NO_COLOR

Pastel will 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.

pastel's People

Contributors

silkfire avatar jamjar00 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.