mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-23 10:05:49 +08:00
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.
This commit is contained in:
committed by
Patrik Svensson
parent
b197f278ed
commit
cd0d182f12
66
src/Spectre.Console/AnsiConsole.Recording.cs
Normal file
66
src/Spectre.Console/AnsiConsole.Recording.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A console capable of writing ANSI escape sequences.
|
||||
/// </summary>
|
||||
public static partial class AnsiConsole
|
||||
{
|
||||
/// <summary>
|
||||
/// Starts recording the console output.
|
||||
/// </summary>
|
||||
public static void Record()
|
||||
{
|
||||
_recorder = new Recorder(_console.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports all recorded console output as text.
|
||||
/// </summary>
|
||||
/// <returns>The recorded output as text.</returns>
|
||||
public static string ExportText()
|
||||
{
|
||||
if (_recorder == null)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot export text since a recording hasn't been started.");
|
||||
}
|
||||
|
||||
return _recorder.ExportText();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports all recorded console output as HTML.
|
||||
/// </summary>
|
||||
/// <returns>The recorded output as HTML.</returns>
|
||||
public static string ExportHtml()
|
||||
{
|
||||
if (_recorder == null)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot export HTML since a recording hasn't been started.");
|
||||
}
|
||||
|
||||
return _recorder.ExportHtml();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports all recorded console output using a custom encoder.
|
||||
/// </summary>
|
||||
/// <param name="encoder">The encoder to use.</param>
|
||||
/// <returns>The recorded output.</returns>
|
||||
public static string ExportCustom(IAnsiConsoleEncoder encoder)
|
||||
{
|
||||
if (_recorder == null)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot export HTML since a recording hasn't been started.");
|
||||
}
|
||||
|
||||
if (encoder is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(encoder));
|
||||
}
|
||||
|
||||
return _recorder.Export(encoder);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user