Skip to content

Commit d2be797

Browse files
authored
chore: add NO_COLOR environment variable support (#2870)
1 parent 2d917be commit d2be797

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/BenchmarkDotNet/Loggers/ConsoleLogger.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ public sealed class ConsoleLogger : ILogger
1515
public static readonly ILogger Default = new ConsoleLogger();
1616
public static readonly ILogger Ascii = new ConsoleLogger(false);
1717
public static readonly ILogger Unicode = new ConsoleLogger(true);
18-
private static readonly bool ConsoleSupportsColors
19-
= !(OsDetector.IsAndroid() || OsDetector.IsIOS() || RuntimeInformation.IsWasm || OsDetector.IsTvOS());
18+
private static readonly Lazy<bool> ConsoleSupportsColors = new(() =>
19+
{
20+
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("NO_COLOR")))
21+
return false;
22+
23+
return !(OsDetector.IsAndroid() || OsDetector.IsIOS() || RuntimeInformation.IsWasm || OsDetector.IsTvOS());
24+
});
2025

2126
private readonly bool unicodeSupport;
2227
private readonly Dictionary<LogKind, ConsoleColor> colorScheme;
@@ -45,7 +50,7 @@ private void Write(LogKind logKind, Action<string> write, string text)
4550
if (!unicodeSupport)
4651
text = text.ToAscii();
4752

48-
if (!ConsoleSupportsColors)
53+
if (!ConsoleSupportsColors.Value)
4954
{
5055
write(text);
5156
return;

0 commit comments

Comments
 (0)