PR feedback: Reduce block nesting in QueryIsScreenAndTryEnableAnsiColorCodes

This commit is contained in:
Ladi Prosek 2023-04-04 11:27:42 +02:00
Родитель 41390058e7
Коммит 7c51985be3
1 изменённых файлов: 36 добавлений и 34 удалений

Просмотреть файл

@ -1483,53 +1483,55 @@ internal static class NativeMethods
#if !CLR2COMPATIBILITY
internal static (bool acceptAnsiColorCodes, bool outputIsScreen, uint? originalConsoleMode) QueryIsScreenAndTryEnableAnsiColorCodes()
{
if (Console.IsOutputRedirected)
{
// There's no ANSI terminal support is console output is redirected.
return (acceptAnsiColorCodes: false, outputIsScreen: false, originalConsoleMode: null);
}
bool acceptAnsiColorCodes = false;
bool outputIsScreen = false;
uint? originalConsoleMode = null;
if (!Console.IsOutputRedirected)
if (IsWindows)
{
if (IsWindows)
try
{
try
IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleMode(stdOut, out uint consoleMode))
{
IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleMode(stdOut, out uint consoleMode))
if ((consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == ENABLE_VIRTUAL_TERMINAL_PROCESSING)
{
if ((consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == ENABLE_VIRTUAL_TERMINAL_PROCESSING)
{
// Console is already in required state.
acceptAnsiColorCodes = true;
}
else
{
originalConsoleMode = consoleMode;
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (SetConsoleMode(stdOut, consoleMode) && GetConsoleMode(stdOut, out consoleMode))
{
// We only know if vt100 is supported if the previous call actually set the new flag, older
// systems ignore the setting.
acceptAnsiColorCodes = (consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == ENABLE_VIRTUAL_TERMINAL_PROCESSING;
}
}
uint fileType = GetFileType(stdOut);
// The std out is a char type (LPT or Console).
outputIsScreen = fileType == FILE_TYPE_CHAR;
acceptAnsiColorCodes &= outputIsScreen;
// Console is already in required state.
acceptAnsiColorCodes = true;
}
}
catch
{
// In the unlikely case that the above fails we just ignore and continue.
else
{
originalConsoleMode = consoleMode;
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (SetConsoleMode(stdOut, consoleMode) && GetConsoleMode(stdOut, out consoleMode))
{
// We only know if vt100 is supported if the previous call actually set the new flag, older
// systems ignore the setting.
acceptAnsiColorCodes = (consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == ENABLE_VIRTUAL_TERMINAL_PROCESSING;
}
}
uint fileType = GetFileType(stdOut);
// The std out is a char type (LPT or Console).
outputIsScreen = fileType == FILE_TYPE_CHAR;
acceptAnsiColorCodes &= outputIsScreen;
}
}
else
catch
{
// On posix OSes we expect console always supports VT100 coloring unless it is explicitly marked as "dumb".
acceptAnsiColorCodes = Environment.GetEnvironmentVariable("TERM") != "dumb";
// In the unlikely case that the above fails we just ignore and continue.
}
}
else
{
// On posix OSes we expect console always supports VT100 coloring unless it is explicitly marked as "dumb".
acceptAnsiColorCodes = Environment.GetEnvironmentVariable("TERM") != "dumb";
}
return (acceptAnsiColorCodes, outputIsScreen, originalConsoleMode);
}