@@ -9,30 +9,55 @@ namespace MatthiWare.CommandLine.Core.Usage
9
9
/// <inheritdoc/>
10
10
public class UsagePrinter : IUsagePrinter
11
11
{
12
+ private readonly IEnvironmentVariablesService environmentVariablesService ;
13
+
12
14
protected ICommandLineCommandContainer Container { get ; }
13
15
14
16
/// <inheritdoc/>
15
17
public IUsageBuilder Builder { get ; }
16
18
17
- /// <inheritdoc/>
19
+ /// <summary>
20
+ /// Creates a new CLI output usage printer
21
+ /// </summary>
22
+ /// <param name="container"></param>
23
+ /// <param name="builder"></param>
18
24
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 )
19
35
{
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 ) ) ;
22
39
}
23
40
24
41
/// <inheritdoc/>
25
42
public virtual void PrintErrors ( IReadOnlyCollection < Exception > errors )
26
43
{
44
+ bool canOutputColor = ! this . environmentVariablesService . NoColorRequested ;
45
+
27
46
var previousColor = Console . ForegroundColor ;
28
47
29
- Console . ForegroundColor = ConsoleColor . Red ;
48
+ if ( canOutputColor )
49
+ {
50
+ Console . ForegroundColor = ConsoleColor . Red ;
51
+ }
30
52
31
53
Builder . AddErrors ( errors ) ;
32
54
33
55
Console . Error . WriteLine ( Builder . Build ( ) ) ;
34
56
35
- Console . ForegroundColor = previousColor ;
57
+ if ( canOutputColor )
58
+ {
59
+ Console . ForegroundColor = previousColor ;
60
+ }
36
61
37
62
Console . WriteLine ( ) ;
38
63
}
0 commit comments