dotnet-installer/src/Microsoft.DotNet.Cli.Utils/Reporter.cs

84 lines
2.2 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.DotNet.Cli.Utils
{
// Stupid-simple console manager
2016-01-06 07:48:50 +00:00
public class Reporter
{
private static readonly Reporter NullReporter = new Reporter(console: null);
2015-11-02 00:21:10 +00:00
private static object _lock = new object();
2015-12-07 22:18:09 +00:00
private readonly AnsiConsole _console;
2015-11-02 00:21:10 +00:00
static Reporter()
{
Reset();
}
2015-11-02 00:21:10 +00:00
private Reporter(AnsiConsole console)
{
_console = console;
}
public static Reporter Output { get; private set; }
public static Reporter Error { get; private set; }
public static Reporter Verbose { get; private set; }
/// <summary>
/// Resets the Reporters to write to the current Console Out/Error.
/// </summary>
public static void Reset()
{
lock (_lock)
{
Output = new Reporter(AnsiConsole.GetOutput());
Error = new Reporter(AnsiConsole.GetError());
Verbose = IsVerbose ?
new Reporter(AnsiConsole.GetOutput()) :
NullReporter;
}
}
2015-11-02 00:21:10 +00:00
public static bool IsVerbose => CommandContext.IsVerbose();
2015-11-02 00:21:10 +00:00
public void WriteLine(string message)
{
2015-12-07 22:18:09 +00:00
lock (_lock)
2015-11-02 00:21:10 +00:00
{
if (CommandContext.ShouldPassAnsiCodesThrough())
{
_console?.Writer?.WriteLine(message);
}
else
{
_console?.WriteLine(message);
}
}
}
public void WriteLine()
{
2015-12-07 22:18:09 +00:00
lock (_lock)
2015-11-02 00:21:10 +00:00
{
_console?.Writer?.WriteLine();
}
}
2015-12-07 22:18:09 +00:00
public void Write(string message)
{
lock (_lock)
{
if (CommandContext.ShouldPassAnsiCodesThrough())
{
_console?.Writer?.Write(message);
}
else
{
_console?.Write(message);
}
2015-12-07 22:18:09 +00:00
}
}
}
}