-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ANSI escape codes to control colours of parameters we print (#466)
.NET's `Console.ForegroundColor` only targets stdout. If we rely on `Console.ForegroundColor` and stdout is redirected (e.g. `bmx print | iex`), we won't get any colour on stderr either. Related .NET issue: dotnet/runtime#83146 Changing to use ANSI escape codes to control text colours to work around this issue. https://desire2learn.atlassian.net/browse/VUL-423
- Loading branch information
Showing
2 changed files
with
68 additions
and
7 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,54 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace D2L.Bmx; | ||
|
||
// The code here exists because legacy Windows console doesn't support ANSI escape codes by default. | ||
// We can consider deleting everything here once Windows 10 is out of support and | ||
// Windows Terminal becomes the default console on all supported Windows versions. | ||
internal static partial class VirtualTerminal { | ||
private const string Kernel32 = "kernel32.dll"; | ||
private const int STD_ERROR_HANDLE = -12; | ||
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; | ||
|
||
private static bool _enablingAttempted = false; | ||
private static bool _enabled = false; | ||
|
||
// https://learn.microsoft.com/en-us/windows/console/classic-vs-vt | ||
public static bool TryEnableOnStderr() { | ||
if( _enablingAttempted ) { | ||
return _enabled; | ||
} | ||
_enablingAttempted = true; | ||
|
||
if( !RuntimeInformation.IsOSPlatform( OSPlatform.Windows ) ) { | ||
// POSIX systems all support ANSI escape codes | ||
_enabled = true; | ||
return true; | ||
} | ||
nint stderrHandle = GetStdHandle( STD_ERROR_HANDLE ); | ||
if( !GetConsoleMode( stderrHandle, out int mode ) ) { | ||
_enabled = false; | ||
return false; | ||
} | ||
if( ( mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING ) == ENABLE_VIRTUAL_TERMINAL_PROCESSING ) { | ||
_enabled = true; | ||
return true; | ||
} | ||
_enabled = SetConsoleMode( stderrHandle, (int)( mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING ) ); | ||
return _enabled; | ||
} | ||
|
||
// https://learn.microsoft.com/en-us/windows/console/getstdhandle | ||
[LibraryImport( Kernel32 )] | ||
private static partial IntPtr GetStdHandle( int nStdHandle ); | ||
|
||
// https://learn.microsoft.com/en-us/windows/console/getconsolemode | ||
[LibraryImport( Kernel32 )] | ||
[return: MarshalAs( UnmanagedType.Bool )] | ||
private static partial bool GetConsoleMode( IntPtr handle, out int mode ); | ||
|
||
// https://learn.microsoft.com/en-us/windows/console/setconsolemode | ||
[LibraryImport( Kernel32 )] | ||
[return: MarshalAs( UnmanagedType.Bool )] | ||
private static partial bool SetConsoleMode( IntPtr handle, int mode ); | ||
} |