mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +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
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
@ -7,6 +8,32 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
public static partial class AnsiConsoleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a recorder for the specified console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to record.</param>
|
||||
/// <returns>A recorder for the specified console.</returns>
|
||||
public static Recorder CreateRecorder(this IAnsiConsole console)
|
||||
{
|
||||
return new Recorder(console);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string value to the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to write to.</param>
|
||||
/// <param name="text">The text to write.</param>
|
||||
/// <param name="style">The text style.</param>
|
||||
public static void Write(this IAnsiConsole console, string text, Style style)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
console.Write(new Segment(text, style));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an empty line to the console.
|
||||
/// </summary>
|
||||
@ -34,7 +61,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
console.Write(text, style);
|
||||
console.Write(new Segment(text, style));
|
||||
console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
44
src/Spectre.Console/Extensions/RecorderExtensions.cs
Normal file
44
src/Spectre.Console/Extensions/RecorderExtensions.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="Recorder"/>.
|
||||
/// </summary>
|
||||
public static class RecorderExtensions
|
||||
{
|
||||
private static readonly TextEncoder _textEncoder = new TextEncoder();
|
||||
private static readonly HtmlEncoder _htmlEncoder = new HtmlEncoder();
|
||||
|
||||
/// <summary>
|
||||
/// Exports the recorded content as text.
|
||||
/// </summary>
|
||||
/// <param name="recorder">The recorder.</param>
|
||||
/// <returns>The recorded content as text.</returns>
|
||||
public static string ExportText(this Recorder recorder)
|
||||
{
|
||||
if (recorder is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(recorder));
|
||||
}
|
||||
|
||||
return recorder.Export(_textEncoder);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports the recorded content as HTML.
|
||||
/// </summary>
|
||||
/// <param name="recorder">The recorder.</param>
|
||||
/// <returns>The recorded content as HTML.</returns>
|
||||
public static string ExportHtml(this Recorder recorder)
|
||||
{
|
||||
if (recorder is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(recorder));
|
||||
}
|
||||
|
||||
return recorder.Export(_htmlEncoder);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user