2015-11-16 19:21:57 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2015-10-13 21:31:29 +00:00
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
|
{
|
|
|
|
|
// Stupid-simple console manager
|
2016-01-06 07:48:50 +00:00
|
|
|
|
public class Reporter
|
2015-10-13 21:31:29 +00:00
|
|
|
|
{
|
2016-02-05 02:18:08 +00:00
|
|
|
|
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
|
|
|
|
|
2016-04-15 04:33:41 +00:00
|
|
|
|
static Reporter()
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 00:21:10 +00:00
|
|
|
|
private Reporter(AnsiConsole console)
|
|
|
|
|
{
|
|
|
|
|
_console = console;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 04:33:41 +00:00
|
|
|
|
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());
|
2016-04-18 22:51:35 +00:00
|
|
|
|
Verbose = IsVerbose ?
|
2016-04-15 04:33:41 +00:00
|
|
|
|
new Reporter(AnsiConsole.GetOutput()) :
|
|
|
|
|
NullReporter;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-02 00:21:10 +00:00
|
|
|
|
|
2016-04-18 22:51:35 +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)
|
|
|
|
|
{
|
2016-02-05 20:55:09 +00:00
|
|
|
|
if (CommandContext.ShouldPassAnsiCodesThrough())
|
|
|
|
|
{
|
|
|
|
|
_console?.Writer?.Write(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_console?.Write(message);
|
|
|
|
|
}
|
2015-12-07 22:18:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-13 21:31:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|