Tilde coloring engine

Introduction

Simple elegant text coloring engine for console programs. Based on Empty Page #0 color engine.

Color may be specified by its number or name surrounded by tilde (~) character. One tilde followed by a character other than a letter or number or a fragment consisting of two or more tilde characters will not be formatted.

Colors that can be used are the same as on a standard command console. There are 15 different colours plus black.

Dark colour identifiers are preceded by the letter ‘d’ (dark). In a similar way, bright-colour identifiers can be preceded by the letter ‘l’ (light).

Color changes one by one are silently ignored. Only the last defined color will be used.

Current color is remembered before writing color text and restored after writing. Special color ~0~ may be used to force setting back original color inside text.

Examples

    Energy.Core.Tilde.WriteLine("~yellow~Hello, ~cyan~world~white~!");

_images/tilde01.png

    Energy.Core.Tilde.Write(" ~1~{1}~2~{2}~3~{3}~4~{4}~5~{5}~6~{6} ", null
        , 1, 2, 3, 4, 5, 6);

_images/tilde02.png

    Energy.Core.Tilde.WriteLine("You can use ~`~yellow~`~ to mark text ~yellow~yellow~0~.");

_images/tilde03.png

    Energy.Core.Tilde.WriteLine("~yellow~Welcome to ~blue~ReadLine ~yellow~example~white~!");
    Console.ForegroundColor = ConsoleColor.Magenta;
    Energy.Core.Tilde.Write("Write ~c~something~0~: ~magenta~");
    while (true)
    {
        string input = Energy.Core.Tilde.ReadLine();
        if (input == null)
        {
            System.Threading.Thread.Sleep(500);
            continue;
        }
        else
        {
            Energy.Core.Tilde.WriteLine("You have written ~green~{0}~0~...", input);
            break;
        }
    }
    Energy.Core.Tilde.Pause();

_images/tilde04.png

try
{
    throw new NotSupportedException();
}
catch (Exception exception)
{
    Energy.Core.Tilde.WriteException(exception, true);
}

_images/tilde05.png

Pause

Writes out pause text and waits for user to input anything by reading line from console.

void Energy.Core.Tilde.Pause()

Break

Write out one break line and set default text color.

void Energy.Core.Tilde.Break()

Write out empty lines and set default text color.

void Energy.Core.Tilde.Break(int count)

Write out ruler line surrounded by empty lines.

void Energy.Core.Tilde.Break(int padding, string line)

ReadLine

Read line from console if available. Does not wait for user to enter anything so may be useful in loops. Thread safe. Returns null if user did not press Enter key.

string Energy.Core.Tilde.ReadLine()
Energy.Core.Tilde.WriteLine("~yellow~Welcome to ~blue~ReadLine ~yellow~example~white~!");
Console.ForegroundColor = ConsoleColor.Magenta;
Energy.Core.Tilde.Write("Write ~c~something~0~: ~magenta~");
while (true)
{
    string input = Energy.Core.Tilde.ReadLine();
    if (input == null)
    {
        Thread.Sleep(500);
        continue;
    }
    else
    {
        Energy.Core.Tilde.WriteLine("You have written ~green~{0}~0~..."
            , input);
        break;
    }
}

_images/tilde06.png

Input

Write out prompt message and wait for input string. If empty string is read from console, function will return defaultValue parameter value.

If message contains placeholder {0}, it will be replaced with defaultValue parameter value.

public static string Input(string message, string defaultValue)

Exception

Write out exception message with optional stack trace.

public static void Exception(Exception exception, bool trace)

_images/tilde05.png

Strip

Strip tildes from text.

public static string Strip(string text)

Escape

Escape string which may contain tilde characters.

public static string Escape(string text)

Length

Return total length of tilde string. Doesn’t count tilde control strings.

public static int Length(string example)

Color

Tilde coloring engine console color string table.

public class Color
{
    public static string DarkBlue = "~1~";
    public static string DarkGreen = "~2~";
    public static string DarkCyan = "~3~";
    public static string DarkRed = "~4~";
    public static string DarkMagenta = "~5~";
    public static string DarkYellow = "~6~";
    public static string Gray = "~7~";
    public static string DarkGray = "~8~";
    public static string Blue = "~9~";
    public static string Green = "~10~";
    public static string Cyan = "~11~";
    public static string Red = "~12~";
    public static string Magenta = "~13~";
    public static string Yellow = "~14~";
    public static string White = "~15~";
    public static string Black = "~16~";
}