Skip to content

Commit 2401540

Browse files
committed
Add support for NO_COLOR
fixes #52
1 parent 11be53b commit 2401540

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MatthiWare.CommandLine.Abstractions.Usage
2+
{
3+
/// <summary>
4+
/// Environment variables
5+
/// </summary>
6+
public interface IEnvironmentVariablesService
7+
{
8+
/// <summary>
9+
/// Inidicates if NO_COLOR environment variable has been set
10+
/// https://no-color.org/
11+
/// </summary>
12+
bool NoColorRequested { get; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using MatthiWare.CommandLine.Abstractions.Usage;
2+
using System;
3+
4+
namespace MatthiWare.CommandLine.Core.Usage
5+
{
6+
/// <inheritdoc/>
7+
public class EnvironmentVariableService : IEnvironmentVariablesService
8+
{
9+
private const string NoColorId = "NO_COLOR";
10+
11+
/// <inheritdoc/>
12+
public bool NoColorRequested => Environment.GetEnvironmentVariable(NoColorId) != null;
13+
}
14+
}

CommandLineParser/Core/Usage/UsagePrinter.cs

+30-5
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,55 @@ namespace MatthiWare.CommandLine.Core.Usage
99
/// <inheritdoc/>
1010
public class UsagePrinter : IUsagePrinter
1111
{
12+
private readonly IEnvironmentVariablesService environmentVariablesService;
13+
1214
protected ICommandLineCommandContainer Container { get; }
1315

1416
/// <inheritdoc/>
1517
public IUsageBuilder Builder { get; }
1618

17-
/// <inheritdoc/>
19+
/// <summary>
20+
/// Creates a new CLI output usage printer
21+
/// </summary>
22+
/// <param name="container"></param>
23+
/// <param name="builder"></param>
1824
public UsagePrinter(ICommandLineCommandContainer container, IUsageBuilder builder)
25+
: this(container, builder, new EnvironmentVariableService())
26+
{ }
27+
28+
/// <summary>
29+
/// Creates a new CLI output usage printer
30+
/// </summary>
31+
/// <param name="container"></param>
32+
/// <param name="builder"></param>
33+
/// <param name="environmentVariablesService"></param>
34+
public UsagePrinter(ICommandLineCommandContainer container, IUsageBuilder builder, IEnvironmentVariablesService environmentVariablesService)
1935
{
20-
Container = container;
21-
Builder = builder;
36+
Container = container ?? throw new ArgumentNullException(nameof(container));
37+
Builder = builder ?? throw new ArgumentNullException(nameof(builder));
38+
this.environmentVariablesService = environmentVariablesService ?? throw new ArgumentNullException(nameof(environmentVariablesService));
2239
}
2340

2441
/// <inheritdoc/>
2542
public virtual void PrintErrors(IReadOnlyCollection<Exception> errors)
2643
{
44+
bool canOutputColor = !this.environmentVariablesService.NoColorRequested;
45+
2746
var previousColor = Console.ForegroundColor;
2847

29-
Console.ForegroundColor = ConsoleColor.Red;
48+
if (canOutputColor)
49+
{
50+
Console.ForegroundColor = ConsoleColor.Red;
51+
}
3052

3153
Builder.AddErrors(errors);
3254

3355
Console.Error.WriteLine(Builder.Build());
3456

35-
Console.ForegroundColor = previousColor;
57+
if (canOutputColor)
58+
{
59+
Console.ForegroundColor = previousColor;
60+
}
3661

3762
Console.WriteLine();
3863
}

0 commit comments

Comments
 (0)