Created
May 29, 2023 19:16
-
-
Save patriksvensson/1c320485b4a32e9dcc58a0abe38196bb to your computer and use it in GitHub Desktop.
Implementation of a IAnsiConsole that writes to a StringBuffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Spectre.Console; | |
using Spectre.Console.Rendering; | |
using System.Text; | |
namespace SpectreVirtual; | |
public static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
// Create the virtual terminal | |
var console = VirtualTerminal.Create(); | |
// Write some text | |
console.Write(new Panel("Hello World")); | |
// Get the output | |
var output = console.Output.ToString(); | |
} | |
} | |
public sealed class VirtualTerminal : IAnsiConsole | |
{ | |
private readonly IAnsiConsole _owner; | |
public Profile Profile => _owner.Profile; | |
public IAnsiConsoleCursor Cursor => _owner.Cursor; | |
public IAnsiConsoleInput Input { get; } | |
public StringWriter Output { get; } | |
public IExclusivityMode ExclusivityMode => _owner.ExclusivityMode; | |
public RenderPipeline Pipeline => _owner.Pipeline; | |
private VirtualTerminal(IAnsiConsole owner, StringWriter output) | |
{ | |
_owner = owner ?? throw new ArgumentNullException(nameof(owner)); | |
Input = new VirtualConsoleInput(); | |
Output = output ?? throw new ArgumentNullException(nameof(output)); | |
} | |
public static VirtualTerminal Create(int width = 80, int height = 24) | |
{ | |
var output = new StringWriter(); | |
var console = AnsiConsole.Create(new AnsiConsoleSettings | |
{ | |
Ansi = AnsiSupport.Yes, | |
Out = new VirtualConsoleOutput(output, width, height), | |
Interactive = InteractionSupport.Yes, | |
}); | |
return new VirtualTerminal(console, output); | |
} | |
public void Clear(bool home) | |
{ | |
_owner.Clear(); | |
} | |
public void Write(IRenderable renderable) | |
{ | |
_owner.Write(renderable); | |
} | |
} | |
public sealed class VirtualConsoleInput : IAnsiConsoleInput | |
{ | |
public bool IsKeyAvailable() | |
{ | |
return false; | |
} | |
public ConsoleKeyInfo? ReadKey(bool intercept) | |
{ | |
return null; | |
} | |
public Task<ConsoleKeyInfo?> ReadKeyAsync(bool intercept, CancellationToken cancellationToken) | |
{ | |
return Task.FromResult<ConsoleKeyInfo?>(null); | |
} | |
} | |
public sealed class VirtualConsoleOutput : IAnsiConsoleOutput | |
{ | |
public TextWriter Writer { get; } | |
public bool IsTerminal { get; } | |
public int Width { get; } | |
public int Height { get; } | |
public VirtualConsoleOutput(TextWriter writer, int width, int height) | |
{ | |
Writer = writer; | |
IsTerminal = true; | |
Width = width; | |
Height = height; | |
} | |
public void SetEncoding(Encoding encoding) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No output with this solution, however, instead of using a 'StringWriter' I just made a TextWriter class that redirects to console and go output:
It doesn't account for Err vs. Out, but you put me on the right path.
