feat: Simplifying ANSI color methods, better cross-platform color support

This commit is contained in:
Chris Cameron
2020-02-21 21:53:27 -05:00
parent c453717998
commit 985578985a
9 changed files with 110 additions and 169 deletions

View File

@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- Fixed exception trying to get DHCP status of network interfaces on Linux
- Fixed a bug where color formatted console output on Net Standard was not raising the OnConsolePrint event
- Simplifying ANSI color methods, better cross-platform color support
## [10.3.0] - 2020-01-20
### Changed

View File

@@ -0,0 +1,41 @@
namespace ICD.Common.Utils
{
public static class AnsiUtils
{
public const string COLOR_RED = "\x1b[31;1m";
public const string COLOR_GREEN = "\x1b[32;1m";
public const string COLOR_YELLOW = "\x1b[33;1m";
public const string COLOR_BLUE = "\x1b[34;1m";
public const string COLOR_MAGENTA = "\x1b[35;1m";
public const string COLOR_CYAN = "\x1b[36;1m";
public const string COLOR_WHITE = "\x1b[37;1m";
public const string COLOR_YELLOW_ON_RED_BACKGROUND = "\x1b[93;41m";
public const string CODE_RESET = "\x1b[0m";
/// <summary>
/// Constructor.
/// </summary>
static AnsiUtils()
{
#if !SIMPLSHARP
// Enables ANSI color output in windows/linux console
Pastel.ConsoleExtensions.Enable();
#endif
}
/// <summary>
/// Prefixes the given data with an ANSI control code and suffixes with a reset code.
/// </summary>
/// <param name="data"></param>
/// <param name="code"></param>
/// <returns></returns>
public static string Format(string data, string code)
{
// % needs to be escaped or weird things happen
data = string.IsNullOrEmpty(data) ? data : data.Replace("%", "%%");
return string.Format("{0}{1}{2}", code, data, CODE_RESET);
}
}
}

View File

@@ -1,111 +0,0 @@
using System;
namespace ICD.Common.Utils
{
public enum eConsoleColor
{
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
YellowOnRed
}
public static class ConsoleColorExtensions
{
public const string CONSOLE_RED = "\x1B[31;1m";
public const string CONSOLE_GREEN = "\x1B[32;1m";
public const string CONSOLE_YELLOW = "\x1B[33;1m";
public const string CONSOLE_BLUE = "\x1B[34;1m";
public const string CONSOLE_MAGENTA = "\x1B[35;1m";
public const string CONSOLE_CYAN = "\x1B[36;1m";
public const string CONSOLE_WHITE = "\x1B[37;1m";
public const string CONSOLE_YELLOW_ON_RED_BACKGROUND = "\x1B[93;41m";
public const string CONSOLE_RESET = "\x1B[0m";
public static string FormatAnsi(this eConsoleColor extends, string data)
{
// % needs to be escaped or weird things happen
data = string.IsNullOrEmpty(data) ? data : data.Replace("%", "%%");
return string.Format("{0}{1}{2}", extends.ToAnsiPrefix(), data, CONSOLE_RESET);
}
public static string ToAnsiPrefix(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
return CONSOLE_RED;
case eConsoleColor.Green:
return CONSOLE_GREEN;
case eConsoleColor.Yellow:
return CONSOLE_YELLOW;
case eConsoleColor.Blue:
return CONSOLE_BLUE;
case eConsoleColor.Magenta:
return CONSOLE_MAGENTA;
case eConsoleColor.Cyan:
return CONSOLE_CYAN;
case eConsoleColor.White:
return CONSOLE_WHITE;
case eConsoleColor.YellowOnRed:
return CONSOLE_YELLOW_ON_RED_BACKGROUND;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
#if STANDARD
public static ConsoleColor ToForegroundConsoleColor(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
return ConsoleColor.Red;
case eConsoleColor.Green:
return ConsoleColor.Green;
case eConsoleColor.Yellow:
case eConsoleColor.YellowOnRed:
return ConsoleColor.Yellow;
case eConsoleColor.Blue:
return ConsoleColor.Blue;
case eConsoleColor.Magenta:
return ConsoleColor.Magenta;
case eConsoleColor.Cyan:
return ConsoleColor.Cyan;
case eConsoleColor.White:
return ConsoleColor.White;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
public static ConsoleColor ToBackgroundConsoleColor(this eConsoleColor extends)
{
switch (extends)
{
case eConsoleColor.Red:
case eConsoleColor.Green:
case eConsoleColor.Yellow:
case eConsoleColor.Blue:
case eConsoleColor.Magenta:
case eConsoleColor.Cyan:
case eConsoleColor.White:
return ConsoleColor.Black;
case eConsoleColor.YellowOnRed:
return ConsoleColor.Red;
default:
throw new ArgumentOutOfRangeException("extends");
}
}
#endif
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
namespace ICD.Common.Utils.Converters
{
@@ -48,7 +47,7 @@ namespace ICD.Common.Utils.Converters
int depth = 0;
// Hack - Append a reset to close any open spans
ansi += "\x1b[0m";
ansi += AnsiUtils.CODE_RESET;
return RegexUtils.ReplaceGroup(ansi, ANSI_PATTERN, "match", match =>
{

View File

@@ -42,6 +42,7 @@
<PackageReference Include="Microsoft.Data.SQLite" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Pastel" Version="1.3.1" />
<PackageReference Include="System.Net.NetworkInformation" Version="4.3.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>

View File

@@ -74,6 +74,7 @@
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="AnsiUtils.cs" />
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Attributes\IIcdAttribute.cs" />
<Compile Include="Attributes\RangeAttribute.cs" />
@@ -85,7 +86,7 @@
<Compile Include="Comparers\FileNameEqualityComparer.cs" />
<Compile Include="Comparers\PredicateComparer.cs" />
<Compile Include="Comparers\SequenceComparer.cs" />
<Compile Include="ConsoleColor.cs" />
<Compile Include="eConsoleColor.cs" />
<Compile Include="Converters\AnsiToHtml.cs" />
<Compile Include="DateTimeUtils.cs" />
<Compile Include="Email\EmailClient.cs" />

View File

@@ -49,9 +49,7 @@ namespace ICD.Common.Utils
{
try
{
CrestronConsole.ConsoleCommandResponse(message);
}
catch (NotSupportedException)
{
@@ -84,17 +82,7 @@ namespace ICD.Common.Utils
public static void PrintLine(eConsoleColor color, string message)
{
string ansi = color.FormatAnsi(message);
#if SIMPLSHARP
PrintLine(ansi);
#else
System.Console.ForegroundColor = color.ToForegroundConsoleColor();
System.Console.BackgroundColor = color.ToBackgroundConsoleColor();
System.Console.WriteLine(message);
System.Console.ResetColor();
OnConsolePrint.Raise(null, new StringEventArgs(ansi + IcdEnvironment.NewLine));
#endif
}
public static void PrintLine(eConsoleColor color, string message, params object[] args)
@@ -123,17 +111,7 @@ namespace ICD.Common.Utils
public static void Print(eConsoleColor color, string message)
{
string ansi = color.FormatAnsi(message);
#if SIMPLSHARP
Print(color.FormatAnsi(message));
#else
System.Console.ForegroundColor = color.ToForegroundConsoleColor();
System.Console.BackgroundColor = color.ToBackgroundConsoleColor();
System.Console.Write(message);
System.Console.ResetColor();
OnConsolePrint.Raise(null, new StringEventArgs(ansi));
#endif
Print(ansi);
}
public static void Print(eConsoleColor color, string message, params object[] args)

View File

@@ -25,14 +25,12 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
message = eConsoleColor.Red.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Error(message);
#else
Console.Write("Error - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(message);
Console.ResetColor();
Console.Error.WriteLine("Error - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
#endif
}
finally
@@ -54,14 +52,13 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
message = eConsoleColor.Yellow.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Warn(message);
#else
Console.Write("Warn - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine(message);
Console.ResetColor();
Console.Error.WriteLine("Warn - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
#endif
}
finally
@@ -83,14 +80,12 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
message = eConsoleColor.Blue.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Notice(message);
#else
Console.Write("Notice - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Blue;
Console.Error.WriteLine(message);
Console.ResetColor();
Console.Error.WriteLine("Notice - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
#endif
}
finally
@@ -112,14 +107,12 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
message = eConsoleColor.Green.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Ok(message);
#else
Console.Write("OK - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Green;
Console.Error.WriteLine(message);
Console.ResetColor();
Console.Error.WriteLine("OK - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
#endif
}
finally
@@ -141,15 +134,16 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
#if !SIMPLSHARP
message = string.Format("{0}: {1}", ex.GetType().Name, message);
#endif
message = eConsoleColor.YellowOnRed.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Exception(message, ex);
#else
Console.Write("Except - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, message);
Console.ResetColor();
Console.Error.WriteLine("Except - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
Console.Error.WriteLine(ex.StackTrace);
#endif
}
@@ -173,14 +167,12 @@ namespace ICD.Common.Utils
try
{
#if SIMPLSHARP
message = eConsoleColor.Cyan.FormatAnsi(message);
#if SIMPLSHARP
ErrorLog.Info(message);
#else
Console.Write("Info - {0} - ", IcdEnvironment.GetLocalTime());
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Error.WriteLine(message);
Console.ResetColor();
Console.Error.WriteLine("Info - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
#endif
}
finally

View File

@@ -0,0 +1,39 @@
using System.Collections.Generic;
using ICD.Common.Utils.Extensions;
namespace ICD.Common.Utils
{
public enum eConsoleColor
{
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
YellowOnRed
}
public static class ConsoleColorExtensions
{
private static readonly Dictionary<eConsoleColor, string> s_ConsoleColorCodes =
new Dictionary<eConsoleColor, string>
{
{eConsoleColor.Red, AnsiUtils.COLOR_RED},
{eConsoleColor.Green, AnsiUtils.COLOR_GREEN},
{eConsoleColor.Yellow, AnsiUtils.COLOR_YELLOW},
{eConsoleColor.Blue, AnsiUtils.COLOR_BLUE},
{eConsoleColor.Magenta, AnsiUtils.COLOR_MAGENTA},
{eConsoleColor.Cyan, AnsiUtils.COLOR_CYAN},
{eConsoleColor.White, AnsiUtils.COLOR_WHITE},
{eConsoleColor.YellowOnRed, AnsiUtils.COLOR_YELLOW_ON_RED_BACKGROUND}
};
public static string FormatAnsi(this eConsoleColor extends, string data)
{
string code = s_ConsoleColorCodes.GetDefault(extends, AnsiUtils.CODE_RESET);
return AnsiUtils.Format(data, code);
}
}
}