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