diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6ef055f..e43f792 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/ICD.Common.Utils/AnsiUtils.cs b/ICD.Common.Utils/AnsiUtils.cs
new file mode 100644
index 0000000..71f4878
--- /dev/null
+++ b/ICD.Common.Utils/AnsiUtils.cs
@@ -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";
+
+ ///
+ /// Constructor.
+ ///
+ static AnsiUtils()
+ {
+#if !SIMPLSHARP
+ // Enables ANSI color output in windows/linux console
+ Pastel.ConsoleExtensions.Enable();
+#endif
+ }
+
+ ///
+ /// Prefixes the given data with an ANSI control code and suffixes with a reset code.
+ ///
+ ///
+ ///
+ ///
+ 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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/ICD.Common.Utils/ConsoleColor.cs b/ICD.Common.Utils/ConsoleColor.cs
deleted file mode 100644
index c88c8bb..0000000
--- a/ICD.Common.Utils/ConsoleColor.cs
+++ /dev/null
@@ -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
- }
-}
diff --git a/ICD.Common.Utils/Converters/AnsiToHtml.cs b/ICD.Common.Utils/Converters/AnsiToHtml.cs
index 6dac84d..a003cf3 100644
--- a/ICD.Common.Utils/Converters/AnsiToHtml.cs
+++ b/ICD.Common.Utils/Converters/AnsiToHtml.cs
@@ -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 =>
{
diff --git a/ICD.Common.Utils/ICD.Common.Utils_NetStandard.csproj b/ICD.Common.Utils/ICD.Common.Utils_NetStandard.csproj
index abc33c9..80b13b3 100644
--- a/ICD.Common.Utils/ICD.Common.Utils_NetStandard.csproj
+++ b/ICD.Common.Utils/ICD.Common.Utils_NetStandard.csproj
@@ -42,6 +42,7 @@
+
diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj
index bc0cfbb..a917c72 100644
--- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj
+++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj
@@ -74,6 +74,7 @@
+
@@ -85,7 +86,7 @@
-
+
diff --git a/ICD.Common.Utils/IcdConsole.cs b/ICD.Common.Utils/IcdConsole.cs
index 29fed95..94e5c78 100644
--- a/ICD.Common.Utils/IcdConsole.cs
+++ b/ICD.Common.Utils/IcdConsole.cs
@@ -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)
diff --git a/ICD.Common.Utils/IcdErrorLog.cs b/ICD.Common.Utils/IcdErrorLog.cs
index 26a90fd..93f5982 100644
--- a/ICD.Common.Utils/IcdErrorLog.cs
+++ b/ICD.Common.Utils/IcdErrorLog.cs
@@ -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
diff --git a/ICD.Common.Utils/eConsoleColor.cs b/ICD.Common.Utils/eConsoleColor.cs
new file mode 100644
index 0000000..fadda4b
--- /dev/null
+++ b/ICD.Common.Utils/eConsoleColor.cs
@@ -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 s_ConsoleColorCodes =
+ new Dictionary
+ {
+ {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);
+ }
+ }
+}