@@ -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