mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: Simplifying ANSI color methods, better cross-platform color support
This commit is contained in:
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
### Changed
|
### Changed
|
||||||
- Fixed exception trying to get DHCP status of network interfaces on Linux
|
- 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
|
- 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
|
## [10.3.0] - 2020-01-20
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
41
ICD.Common.Utils/AnsiUtils.cs
Normal file
41
ICD.Common.Utils/AnsiUtils.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Converters
|
namespace ICD.Common.Utils.Converters
|
||||||
{
|
{
|
||||||
@@ -48,7 +47,7 @@ namespace ICD.Common.Utils.Converters
|
|||||||
int depth = 0;
|
int depth = 0;
|
||||||
|
|
||||||
// Hack - Append a reset to close any open spans
|
// Hack - Append a reset to close any open spans
|
||||||
ansi += "\x1b[0m";
|
ansi += AnsiUtils.CODE_RESET;
|
||||||
|
|
||||||
return RegexUtils.ReplaceGroup(ansi, ANSI_PATTERN, "match", match =>
|
return RegexUtils.ReplaceGroup(ansi, ANSI_PATTERN, "match", match =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
<PackageReference Include="Microsoft.Data.SQLite" Version="2.2.6" />
|
<PackageReference Include="Microsoft.Data.SQLite" Version="2.2.6" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
<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.Net.NetworkInformation" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
|
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -74,6 +74,7 @@
|
|||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="AnsiUtils.cs" />
|
||||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||||
<Compile Include="Attributes\IIcdAttribute.cs" />
|
<Compile Include="Attributes\IIcdAttribute.cs" />
|
||||||
<Compile Include="Attributes\RangeAttribute.cs" />
|
<Compile Include="Attributes\RangeAttribute.cs" />
|
||||||
@@ -85,7 +86,7 @@
|
|||||||
<Compile Include="Comparers\FileNameEqualityComparer.cs" />
|
<Compile Include="Comparers\FileNameEqualityComparer.cs" />
|
||||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||||
<Compile Include="Comparers\SequenceComparer.cs" />
|
<Compile Include="Comparers\SequenceComparer.cs" />
|
||||||
<Compile Include="ConsoleColor.cs" />
|
<Compile Include="eConsoleColor.cs" />
|
||||||
<Compile Include="Converters\AnsiToHtml.cs" />
|
<Compile Include="Converters\AnsiToHtml.cs" />
|
||||||
<Compile Include="DateTimeUtils.cs" />
|
<Compile Include="DateTimeUtils.cs" />
|
||||||
<Compile Include="Email\EmailClient.cs" />
|
<Compile Include="Email\EmailClient.cs" />
|
||||||
|
|||||||
@@ -49,9 +49,7 @@ namespace ICD.Common.Utils
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
CrestronConsole.ConsoleCommandResponse(message);
|
CrestronConsole.ConsoleCommandResponse(message);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (NotSupportedException)
|
catch (NotSupportedException)
|
||||||
{
|
{
|
||||||
@@ -84,17 +82,7 @@ namespace ICD.Common.Utils
|
|||||||
public static void PrintLine(eConsoleColor color, string message)
|
public static void PrintLine(eConsoleColor color, string message)
|
||||||
{
|
{
|
||||||
string ansi = color.FormatAnsi(message);
|
string ansi = color.FormatAnsi(message);
|
||||||
|
|
||||||
#if SIMPLSHARP
|
|
||||||
PrintLine(ansi);
|
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)
|
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)
|
public static void Print(eConsoleColor color, string message)
|
||||||
{
|
{
|
||||||
string ansi = color.FormatAnsi(message);
|
string ansi = color.FormatAnsi(message);
|
||||||
|
Print(ansi);
|
||||||
#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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Print(eConsoleColor color, string message, params object[] args)
|
public static void Print(eConsoleColor color, string message, params object[] args)
|
||||||
|
|||||||
@@ -25,14 +25,12 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
message = eConsoleColor.Red.FormatAnsi(message);
|
message = eConsoleColor.Red.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
ErrorLog.Error(message);
|
ErrorLog.Error(message);
|
||||||
#else
|
#else
|
||||||
Console.Write("Error - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("Error - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.Error.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -54,14 +52,13 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
message = eConsoleColor.Yellow.FormatAnsi(message);
|
message = eConsoleColor.Yellow.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
|
|
||||||
ErrorLog.Warn(message);
|
ErrorLog.Warn(message);
|
||||||
#else
|
#else
|
||||||
Console.Write("Warn - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("Warn - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
||||||
Console.Error.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -83,14 +80,12 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
message = eConsoleColor.Blue.FormatAnsi(message);
|
message = eConsoleColor.Blue.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
ErrorLog.Notice(message);
|
ErrorLog.Notice(message);
|
||||||
#else
|
#else
|
||||||
Console.Write("Notice - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("Notice - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Blue;
|
|
||||||
Console.Error.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -112,14 +107,12 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
message = eConsoleColor.Green.FormatAnsi(message);
|
message = eConsoleColor.Green.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
ErrorLog.Ok(message);
|
ErrorLog.Ok(message);
|
||||||
#else
|
#else
|
||||||
Console.Write("OK - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("OK - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.Error.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -141,15 +134,16 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
#if !SIMPLSHARP
|
||||||
|
message = string.Format("{0}: {1}", ex.GetType().Name, message);
|
||||||
|
#endif
|
||||||
|
|
||||||
message = eConsoleColor.YellowOnRed.FormatAnsi(message);
|
message = eConsoleColor.YellowOnRed.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
ErrorLog.Exception(message, ex);
|
ErrorLog.Exception(message, ex);
|
||||||
#else
|
#else
|
||||||
Console.Write("Except - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("Except - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
||||||
Console.BackgroundColor = ConsoleColor.Red;
|
|
||||||
Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, message);
|
|
||||||
Console.ResetColor();
|
|
||||||
Console.Error.WriteLine(ex.StackTrace);
|
Console.Error.WriteLine(ex.StackTrace);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -173,14 +167,12 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
message = eConsoleColor.Cyan.FormatAnsi(message);
|
message = eConsoleColor.Cyan.FormatAnsi(message);
|
||||||
|
|
||||||
|
#if SIMPLSHARP
|
||||||
ErrorLog.Info(message);
|
ErrorLog.Info(message);
|
||||||
#else
|
#else
|
||||||
Console.Write("Info - {0} - ", IcdEnvironment.GetLocalTime());
|
Console.Error.WriteLine("Info - {0} - {1}", IcdEnvironment.GetLocalTime(), message);
|
||||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
||||||
Console.Error.WriteLine(message);
|
|
||||||
Console.ResetColor();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
39
ICD.Common.Utils/eConsoleColor.cs
Normal file
39
ICD.Common.Utils/eConsoleColor.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user