feat: AnsiUtils exposes ANSI codes

This commit is contained in:
Chris Cameron
2020-02-28 11:27:39 -05:00
parent 4229a6a73f
commit 82d0598352
2 changed files with 58 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace ICD.Common.Utils namespace ICD.Common.Utils
@@ -13,8 +14,25 @@ namespace ICD.Common.Utils
public const string COLOR_CYAN = "\x1b[36;1m"; public const string COLOR_CYAN = "\x1b[36;1m";
public const string COLOR_WHITE = "\x1b[37;1m"; public const string COLOR_WHITE = "\x1b[37;1m";
public const string COLOR_YELLOW_ON_RED_BACKGROUND = "\x1b[93;41m"; public const string COLOR_YELLOW_ON_RED_BACKGROUND = "\x1b[93;41m";
public const string ANSI_RESET = "\x1b[0m";
public const string CODE_RESET = "\x1b[0m"; public const string CODE_BLACK = "30";
public const string CODE_RED = "31";
public const string CODE_GREEN = "32";
public const string CODE_YELLOW = "33";
public const string CODE_BLUE = "34";
public const string CODE_MAGENTA = "35";
public const string CODE_CYAN = "36";
public const string CODE_WHITE = "37";
public const string CODE_BRIGHT_BLACK = "30;1";
public const string CODE_BRIGHT_RED = "31;1";
public const string CODE_BRIGHT_GREEN = "32;1";
public const string CODE_BRIGHT_YELLOW = "33;1";
public const string CODE_BRIGHT_BLUE = "34;1";
public const string CODE_BRIGHT_MAGENTA = "35;1";
public const string CODE_BRIGHT_CYAN = "36;1";
public const string CODE_BRIGHT_WHITE = "37;1";
/// <summary> /// <summary>
/// Matches ANSI escape codes, e.g. \x1b[31m and \x1b[30;1m /// Matches ANSI escape codes, e.g. \x1b[31m and \x1b[30;1m
@@ -28,23 +46,23 @@ namespace ICD.Common.Utils
private static readonly Dictionary<string, string> s_PuttyColors = private static readonly Dictionary<string, string> s_PuttyColors =
new Dictionary<string, string> new Dictionary<string, string>
{ {
{"30", "#000000"}, // Black {CODE_BLACK, "#000000"},
{"31", "#BB0000"}, // Red {CODE_RED, "#BB0000"},
{"32", "#00BB00"}, // Green {CODE_GREEN, "#00BB00"},
{"33", "#BBBB00"}, // Yellow {CODE_YELLOW, "#BBBB00"},
{"34", "#0000BB"}, // Blue {CODE_BLUE, "#0000BB"},
{"35", "#BB00BB"}, // Magenta {CODE_MAGENTA, "#BB00BB"},
{"36", "#00BBBB"}, // Cyan {CODE_CYAN, "#00BBBB"},
{"37", "#BBBBBB"}, // White {CODE_WHITE, "#BBBBBB"},
{"30;1", "#555555"}, // Bright Black {CODE_BRIGHT_BLACK, "#555555"},
{"31;1", "#FF5555"}, // Bright Red {CODE_BRIGHT_RED, "#FF5555"},
{"32;1", "#55FF55"}, // Bright Green {CODE_BRIGHT_GREEN, "#55FF55"},
{"33;1", "#FFFF55"}, // Bright Yellow {CODE_BRIGHT_YELLOW, "#FFFF55"},
{"34;1", "#5555FF"}, // Bright Blue {CODE_BRIGHT_BLUE, "#5555FF"},
{"35;1", "#FF55FF"}, // Bright Magenta {CODE_BRIGHT_MAGENTA, "#FF55FF"},
{"36;1", "#55FFFF"}, // Bright Cyan {CODE_BRIGHT_CYAN, "#55FFFF"},
{"37;1", "#FFFFFF"}, // Bright White {CODE_BRIGHT_WHITE, "#FFFFFF"}
}; };
/// <summary> /// <summary>
@@ -64,17 +82,17 @@ namespace ICD.Common.Utils
} }
/// <summary> /// <summary>
/// Prefixes the given data with an ANSI control code and suffixes with a reset code. /// Prefixes the given data with an ANSI control sequence and suffixes with a reset sequence.
/// </summary> /// </summary>
/// <param name="data"></param> /// <param name="data"></param>
/// <param name="code"></param> /// <param name="ansiSequence"></param>
/// <returns></returns> /// <returns></returns>
public static string Format(string data, string code) public static string Format(string data, string ansiSequence)
{ {
// % needs to be escaped or weird things happen // % needs to be escaped or weird things happen
data = string.IsNullOrEmpty(data) ? data : data.Replace("%", "%%"); data = string.IsNullOrEmpty(data) ? data : data.Replace("%", "%%");
return string.Format("{0}{1}{2}", code, data, CODE_RESET); return string.Format("{0}{1}{2}", ansiSequence, data, ANSI_RESET);
} }
/// <summary> /// <summary>
@@ -141,6 +159,23 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public string GetColor(IDictionary<string, string> colors, bool invertBright) public string GetColor(IDictionary<string, string> colors, bool invertBright)
{ {
if (colors == null)
throw new ArgumentNullException("colors");
return GetColor<string>(colors, invertBright);
}
/// <summary>
/// Gets the color value for the code.
/// </summary>
/// <param name="colors"></param>
/// <param name="invertBright"></param>
/// <returns></returns>
public T GetColor<T>(IDictionary<string, T> colors, bool invertBright)
{
if (colors == null)
throw new ArgumentNullException("colors");
string code = invertBright ? AnsiUtils.InvertBright(Code) : Code; string code = invertBright ? AnsiUtils.InvertBright(Code) : Code;
return colors[code]; return colors[code];
} }

View File

@@ -32,7 +32,7 @@ namespace ICD.Common.Utils
public static string FormatAnsi(this eConsoleColor extends, string data) public static string FormatAnsi(this eConsoleColor extends, string data)
{ {
string code = s_ConsoleColorCodes.GetDefault(extends, AnsiUtils.CODE_RESET); string code = s_ConsoleColorCodes.GetDefault(extends, AnsiUtils.ANSI_RESET);
return AnsiUtils.Format(data, code); return AnsiUtils.Format(data, code);
} }
} }