Support setting the static console

This commit is contained in:
Patrik Svensson 2021-02-28 23:11:35 +01:00 committed by Phil Scott
parent bff3438a5a
commit 29e6e34f83
3 changed files with 43 additions and 15 deletions

View File

@ -13,7 +13,10 @@ namespace Spectre.Console
/// </summary>
public static void Record()
{
_recorder = new Recorder(_console.Value);
if (_recorder == null)
{
_recorder = new Recorder(Console);
}
}
/// <summary>

View File

@ -7,25 +7,43 @@ namespace Spectre.Console
/// </summary>
public static partial class AnsiConsole
{
private static readonly Lazy<IAnsiConsole> _console = new Lazy<IAnsiConsole>(() =>
{
var console = Create(new AnsiConsoleSettings
private static Recorder? _recorder;
private static Lazy<IAnsiConsole> _console = new Lazy<IAnsiConsole>(
() =>
{
Ansi = AnsiSupport.Detect,
ColorSystem = ColorSystemSupport.Detect,
Out = System.Console.Out,
var console = Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.Detect,
ColorSystem = ColorSystemSupport.Detect,
Out = System.Console.Out,
});
Created = true;
return console;
});
Created = true;
return console;
});
private static Recorder? _recorder;
/// <summary>
/// Gets the underlying <see cref="IAnsiConsole"/>.
/// Gets or sets the underlying <see cref="IAnsiConsole"/>.
/// </summary>
public static IAnsiConsole Console => _recorder ?? _console.Value;
public static IAnsiConsole Console
{
get
{
return _recorder ?? _console.Value;
}
set
{
_console = new Lazy<IAnsiConsole>(() => value);
if (_recorder != null)
{
// Recreate the recorder
_recorder = _recorder.Clone(value);
}
Created = true;
}
}
/// <summary>
/// Gets the <see cref="IAnsiConsoleCursor"/>.

View File

@ -67,6 +67,13 @@ namespace Spectre.Console
_console.Write(segments);
}
internal Recorder Clone(IAnsiConsole console)
{
var recorder = new Recorder(console);
recorder.Recorded.AddRange(Recorded);
return recorder;
}
/// <summary>
/// Records the specified segments.
/// </summary>