Handle output to stderr

This commit is contained in:
Bastian Eicher 2021-02-12 10:57:37 +01:00 committed by Patrik Svensson
parent 9312663bde
commit a1050fc676
4 changed files with 21 additions and 4 deletions

View File

@ -28,7 +28,7 @@ namespace Spectre.Console
var (supportsAnsi, legacyConsole) = DetectAnsi(settings, buffer); var (supportsAnsi, legacyConsole) = DetectAnsi(settings, buffer);
// Use the provided encoding or fall back to UTF-8 // Use the provided encoding or fall back to UTF-8
var encoding = buffer.IsStandardOut() ? System.Console.OutputEncoding : Encoding.UTF8; var encoding = buffer.IsStandardOut() || buffer.IsStandardError() ? System.Console.OutputEncoding : Encoding.UTF8;
// Get the color system // Get the color system
var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect
@ -73,14 +73,14 @@ namespace Spectre.Console
// 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
// on Windows, there is no way of detecting whether or not we're running on a legacy console or not. // on Windows, there is no way of detecting whether or not we're running on a legacy console or not.
if (AnsiConsole.Created && !legacyConsole && buffer.IsStandardOut() && AnsiConsole.Profile.Capabilities.Legacy) if (AnsiConsole.Created && !legacyConsole && (buffer.IsStandardOut() || buffer.IsStandardError()) && AnsiConsole.Profile.Capabilities.Legacy)
{ {
legacyConsole = AnsiConsole.Profile.Capabilities.Legacy; legacyConsole = AnsiConsole.Profile.Capabilities.Legacy;
} }
} }
else else
{ {
if (buffer.IsStandardOut()) if (buffer.IsStandardOut() || buffer.IsStandardError())
{ {
// Are we running on Windows? // Are we running on Windows?
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))

View File

@ -49,6 +49,11 @@ namespace Spectre.Console
return System.Console.IsOutputRedirected; return System.Console.IsOutputRedirected;
} }
if (_profile.Out.IsStandardError())
{
return System.Console.IsErrorRedirected;
}
// Not stdout, so must be a TTY. // Not stdout, so must be a TTY.
return true; return true;
} }

View File

@ -15,5 +15,17 @@ namespace Spectre.Console
return false; return false;
} }
} }
public static bool IsStandardError(this TextWriter writer)
{
try
{
return writer == System.Console.Error;
}
catch
{
return false;
}
}
} }
} }

View File

@ -73,7 +73,7 @@ namespace Spectre.Console
} }
// Need to update the output encoding for stdout? // Need to update the output encoding for stdout?
if (_out.IsStandardOut()) if (_out.IsStandardOut() || _out.IsStandardError())
{ {
System.Console.OutputEncoding = value; System.Console.OutputEncoding = value;
} }