-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
ConsoleReporter.cs
125 lines (108 loc) · 4.06 KB
/
ConsoleReporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// This file has been modified from the original form. See Notice.txt in the project root for more information.
using System;
using System.IO;
namespace McMaster.Extensions.CommandLineUtils
{
/// <summary>
/// A thread-safe reporter that forwards to console output.
/// </summary>
public class ConsoleReporter : IReporter
{
private readonly object _writeLock = new();
/// <summary>
/// Initializes an instance of <see cref="ConsoleReporter"/>.
/// </summary>
/// <param name="console"></param>
public ConsoleReporter(IConsole console)
: this(console, verbose: false, quiet: false)
{ }
/// <summary>
/// Initializes an instance of <see cref="ConsoleReporter"/>.
/// </summary>
/// <param name="console"></param>
/// <param name="verbose">When false, Verbose does not display output.</param>
/// <param name="quiet">When true, only Warn and Error display output</param>
public ConsoleReporter(IConsole console, bool verbose, bool quiet)
{
Console = console ?? throw new ArgumentNullException(nameof(console));
IsVerbose = verbose;
IsQuiet = quiet;
}
/// <summary>
/// The console to write to.
/// </summary>
protected IConsole Console { get; }
/// <summary>
/// Is verbose output displayed.
/// </summary>
public bool IsVerbose { get; set; }
/// <summary>
/// Is verbose output and regular output hidden.
/// </summary>
public bool IsQuiet { get; set; }
/// <summary>
/// Write a line with color.
/// </summary>
/// <param name="writer"></param>
/// <param name="message"></param>
/// <param name="foregroundColor"></param>
/// <param name="backgroundColor"></param>
protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = default)
{
lock (_writeLock)
{
if (foregroundColor.HasValue)
{
Console.ForegroundColor = foregroundColor.Value;
}
if (backgroundColor.HasValue)
{
Console.BackgroundColor = backgroundColor.Value;
}
writer.WriteLine(message);
if (foregroundColor.HasValue)
{
Console.ResetColor();
}
}
}
/// <summary>
/// Writes a message in <see cref="ConsoleColor.Red"/> to <see cref="IConsole.Error"/>.
/// </summary>
/// <param name="message"></param>
public virtual void Error(string message)
=> WriteLine(Console.Error, message, ConsoleColor.Red);
/// <summary>
/// Writes a message in <see cref="ConsoleColor.Yellow"/> to <see cref="IConsole.Out"/>.
/// </summary>
/// <param name="message"></param>
public virtual void Warn(string message)
=> WriteLine(Console.Out, message, ConsoleColor.Yellow);
/// <summary>
/// Writes a message to <see cref="IConsole.Out"/>.
/// </summary>
/// <param name="message"></param>
public virtual void Output(string message)
{
if (IsQuiet)
{
return;
}
WriteLine(Console.Out, message, foregroundColor: null);
}
/// <summary>
/// Writes a message in <see cref="ConsoleColor.DarkGray"/> to <see cref="IConsole.Out"/>.
/// </summary>
/// <param name="message"></param>
public virtual void Verbose(string message)
{
if (!IsVerbose)
{
return;
}
WriteLine(Console.Out, message, ConsoleColor.DarkGray);
}
}
}