Patrik Svensson cd0d182f12 Add support for recording console output
This commit adds support for recording console output
as well as exporting it to either text or HTML. A user can
also provide their own encoder if they wish.
2020-09-21 13:33:28 +02:00

67 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A console recorder used to record output from a console.
/// </summary>
public sealed class Recorder : IAnsiConsole, IDisposable
{
private readonly IAnsiConsole _console;
private readonly List<Segment> _recorded;
/// <inheritdoc/>
public Capabilities Capabilities => _console.Capabilities;
/// <inheritdoc/>
public Encoding Encoding => _console.Encoding;
/// <inheritdoc/>
public int Width => _console.Width;
/// <inheritdoc/>
public int Height => _console.Height;
/// <summary>
/// Initializes a new instance of the <see cref="Recorder"/> class.
/// </summary>
/// <param name="console">The console to record output for.</param>
public Recorder(IAnsiConsole console)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_recorded = new List<Segment>();
}
/// <inheritdoc/>
public void Dispose()
{
// Only used for scoping.
}
/// <inheritdoc/>
public void Write(Segment segment)
{
_recorded.Add(segment);
_console.Write(segment);
}
/// <summary>
/// Exports the recorded data.
/// </summary>
/// <param name="encoder">The encoder.</param>
/// <returns>The recorded data represented as a string.</returns>
public string Export(IAnsiConsoleEncoder encoder)
{
if (encoder is null)
{
throw new ArgumentNullException(nameof(encoder));
}
return encoder.Encode(_recorded);
}
}
}