Skip to content

Commit bb2b3ef

Browse files
0xcedstephentoub
authored andcommitted
Add possibility to write ANSI color escape codes when the console output is redirected
This introduces an opt-in mechanism to allow writing ANSI color escape codes even when the console output is redirected. To enable ANSI color codes, the `DOTNET_CONSOLE_ANSI_COLOR` environment variable must be set to `true` (case insensitive) or `1`. Fixes #33980
1 parent a423f69 commit bb2b3ef

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/libraries/System.Console/src/System/ConsolePal.Unix.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ private static void WriteSetColorString(bool foreground, ConsoleColor color)
779779
// Changing the color involves writing an ANSI character sequence out to the output stream.
780780
// We only want to do this if we know that sequence will be interpreted by the output.
781781
// rather than simply displayed visibly.
782-
if (Console.IsOutputRedirected)
782+
if (!SupportsAnsiColor())
783783
return;
784784

785785
// See if we've already cached a format string for this foreground/background
@@ -813,13 +813,33 @@ private static void WriteSetColorString(bool foreground, ConsoleColor color)
813813
/// <summary>Writes out the ANSI string to reset colors.</summary>
814814
private static void WriteResetColorString()
815815
{
816-
// We only want to send the reset string if we're targeting a TTY device
817-
if (!Console.IsOutputRedirected)
816+
if (SupportsAnsiColor())
818817
{
819818
WriteStdoutAnsiString(TerminalFormatStrings.Instance.Reset);
820819
}
821820
}
822821

822+
/// <summary>
823+
/// Tests whether ANSI color codes should be emitted or not.
824+
/// <para>
825+
/// If the <c>DOTNET_CONSOLE_ANSI_COLOR</c> environment variable contains <c>true</c> (case insensitive) or <c>1</c>
826+
/// then ANSI color codes are supported. If the <c>DOTNET_CONSOLE_ANSI_COLOR</c> environment variable is not defined
827+
/// or contains a non truthy value, then ANSI color codes are supported if the console output is not redirected.
828+
/// </para>
829+
/// </summary>
830+
/// <returns><c>true</c> if ANSI color escape codes must be emitted, <c>false</c> if they must not be emitted.</returns>
831+
/// <remarks>This was discussed in https://github.com/dotnet/runtime/issues/33980</remarks>
832+
private static bool SupportsAnsiColor()
833+
{
834+
string? consoleAnsiColor = Environment.GetEnvironmentVariable("DOTNET_CONSOLE_ANSI_COLOR");
835+
if (consoleAnsiColor != null)
836+
{
837+
return consoleAnsiColor == "1" || (bool.TryParse(consoleAnsiColor, out bool enabled) && enabled);
838+
}
839+
840+
return !Console.IsOutputRedirected;
841+
}
842+
823843
/// <summary>
824844
/// The values of the ConsoleColor enums unfortunately don't map to the
825845
/// corresponding ANSI values. We need to do the mapping manually.

0 commit comments

Comments
 (0)