Fix Windows ANSI support detection with redirected stderr

This commit is contained in:
Bastian Eicher 2021-03-14 10:55:31 +01:00 committed by Patrik Svensson
parent 2540f48622
commit 08b65cfa47
2 changed files with 9 additions and 6 deletions

View File

@ -68,7 +68,7 @@ namespace Spectre.Console
if (settings.Ansi == AnsiSupport.Detect) if (settings.Ansi == AnsiSupport.Detect)
{ {
(supportsAnsi, legacyConsole) = AnsiDetector.Detect(true); (supportsAnsi, legacyConsole) = AnsiDetector.Detect(buffer.IsStandardError(), true);
// Check whether or not this is a legacy console from the existing instance (if any). // Check whether or not this is a legacy console from the existing instance (if any).
// We need to do this because once we upgrade the console to support ENABLE_VIRTUAL_TERMINAL_PROCESSING // We need to do this because once we upgrade the console to support ENABLE_VIRTUAL_TERMINAL_PROCESSING
@ -93,7 +93,7 @@ namespace Spectre.Console
else else
{ {
// Try detecting whether or not this // Try detecting whether or not this
(_, legacyConsole) = AnsiDetector.Detect(false); (_, legacyConsole) = AnsiDetector.Detect(buffer.IsStandardError(), false);
} }
} }
} }

View File

@ -32,7 +32,7 @@ namespace Spectre.Console
new Regex("bvterm"), // Bitvise SSH Client new Regex("bvterm"), // Bitvise SSH Client
}; };
public static (bool SupportsAnsi, bool LegacyConsole) Detect(bool upgrade) public static (bool SupportsAnsi, bool LegacyConsole) Detect(bool stdError, bool upgrade)
{ {
// Running on Windows? // Running on Windows?
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@ -44,7 +44,7 @@ namespace Spectre.Console
return (true, false); return (true, false);
} }
var supportsAnsi = Windows.SupportsAnsi(upgrade, out var legacyConsole); var supportsAnsi = Windows.SupportsAnsi(stdError, upgrade, out var legacyConsole);
return (supportsAnsi, legacyConsole); return (supportsAnsi, legacyConsole);
} }
@ -71,6 +71,9 @@ namespace Spectre.Console
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore")] [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore")]
private const int STD_OUTPUT_HANDLE = -11; private const int STD_OUTPUT_HANDLE = -11;
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore")]
private const int STD_ERROR_HANDLE = -12;
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore")] [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore")]
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
@ -89,13 +92,13 @@ namespace Spectre.Console
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
public static extern uint GetLastError(); public static extern uint GetLastError();
public static bool SupportsAnsi(bool upgrade, out bool isLegacy) public static bool SupportsAnsi(bool upgrade, bool stdError, out bool isLegacy)
{ {
isLegacy = false; isLegacy = false;
try try
{ {
var @out = GetStdHandle(STD_OUTPUT_HANDLE); var @out = GetStdHandle(stdError ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
if (!GetConsoleMode(@out, out var mode)) if (!GetConsoleMode(@out, out var mode))
{ {
// Could not get console mode, try TERM (set in cygwin, WSL-Shell). // Could not get console mode, try TERM (set in cygwin, WSL-Shell).