-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): enable ANSI colors console mode on windows
closes #131
- Loading branch information
Showing
13 changed files
with
454 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
|
||
namespace SecTester.Core.Logger; | ||
|
||
public class AnsiCodeColor : IEquatable<AnsiCodeColor> | ||
{ | ||
private readonly string _color; | ||
private readonly int _hashcode; | ||
|
||
public static readonly AnsiCodeColor DefaultForeground = new("\x1B[39m\x1B[22m"); | ||
public static readonly AnsiCodeColor Red = new("\x1B[1m\x1B[31m"); | ||
public static readonly AnsiCodeColor DarkRed = new("\x1B[31m"); | ||
public static readonly AnsiCodeColor Yellow = new("\x1B[1m\x1B[33m"); | ||
public static readonly AnsiCodeColor DarkGreen = new("\x1B[32m"); | ||
public static readonly AnsiCodeColor White = new("\x1B[1m\x1B[37m"); | ||
public static readonly AnsiCodeColor Cyan = new("\x1B[1m\x1B[36m"); | ||
|
||
public AnsiCodeColor(string color) | ||
{ | ||
if (string.IsNullOrEmpty(color)) | ||
{ | ||
throw new ArgumentNullException(nameof(color)); | ||
} | ||
|
||
_color = color; | ||
_hashcode = StringComparer.OrdinalIgnoreCase.GetHashCode(_color); | ||
} | ||
|
||
public override string ToString() => _color; | ||
public override int GetHashCode() => _hashcode; | ||
public override bool Equals(object obj) => Equals(obj as AnsiCodeColor); | ||
|
||
public bool Equals(AnsiCodeColor? other) | ||
{ | ||
return other is not null && _color.Equals(other._color, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public static implicit operator string(AnsiCodeColor codeColor) => codeColor.ToString(); | ||
|
||
public static bool operator ==(AnsiCodeColor? left, AnsiCodeColor? right) | ||
{ | ||
return left is null || right is null ? ReferenceEquals(left, right) : left.Equals(right); | ||
} | ||
|
||
public static bool operator !=(AnsiCodeColor? left, AnsiCodeColor? right) | ||
{ | ||
return !(left == right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SecTester.Core.Logger; | ||
|
||
public interface AnsiCodeColorizer | ||
{ | ||
string Colorize(AnsiCodeColor ansiCodeColor, string input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace SecTester.Core.Logger; | ||
|
||
public class DefaultAnsiCodeColorizer : AnsiCodeColorizer | ||
{ | ||
private readonly bool _enabled; | ||
|
||
public DefaultAnsiCodeColorizer(bool enabled) | ||
{ | ||
_enabled = enabled; | ||
} | ||
|
||
public string Colorize(AnsiCodeColor ansiCodeColor, string input) | ||
{ | ||
return !_enabled ? input : $"{ansiCodeColor}{input}{AnsiCodeColor.DefaultForeground}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace SecTester.Core.Utils; | ||
|
||
// This is from https://github.com/silkfire/Pastel/blob/master/src/ConsoleExtensions.cs | ||
[ExcludeFromCodeCoverage] | ||
internal static class ConsoleUtils | ||
{ | ||
private const string Kernel32 = "kernel32"; | ||
|
||
private const int StdOutHandle = -11; | ||
private const int StdErrHandle = -12; | ||
|
||
private const uint EnableProcessedOutput = 0x0001; | ||
private const uint EnableVirtualTerminalProcessing = 0x0004; | ||
private const uint AnsiColorRequiredMode = EnableProcessedOutput | EnableVirtualTerminalProcessing; | ||
|
||
[DllImport(Kernel32)] | ||
private static extern bool GetConsoleMode(SafeFileHandle hConsoleHandle, out uint lpMode); | ||
|
||
[DllImport(Kernel32)] | ||
private static extern bool SetConsoleMode(SafeFileHandle hConsoleHandle, uint dwMode); | ||
|
||
[DllImport(Kernel32, SetLastError = true)] | ||
private static extern SafeFileHandle GetStdHandle(int nStdHandle); | ||
|
||
public static bool IsColored { get; } | ||
|
||
static ConsoleUtils() | ||
{ | ||
IsColored = Environment.GetEnvironmentVariable("NO_COLOR") == null; | ||
IsColored = IsColored && EnableAnsiColors(); | ||
} | ||
|
||
private static bool EnableAnsiColors() | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return true; | ||
} | ||
|
||
return EnableWindowsAnsiColors(StdOutHandle) && EnableWindowsAnsiColors(StdErrHandle); | ||
} | ||
|
||
private static bool EnableWindowsAnsiColors(int consoleHandle) | ||
{ | ||
var handle = GetStdHandle(consoleHandle); | ||
|
||
if (handle.IsInvalid || !GetConsoleMode(handle, out var outConsoleMode)) | ||
{ | ||
return false; | ||
} | ||
|
||
return AnsiColorRequiredMode == (outConsoleMode & AnsiColorRequiredMode) || | ||
SetConsoleMode(handle, outConsoleMode | AnsiColorRequiredMode); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.