Created
May 29, 2023 18:46
-
-
Save patriksvensson/c754a8ef4bf2173e7834a6ea1f3c404f to your computer and use it in GitHub Desktop.
How to create a Spectre.Console console that doesn't use Console.Out and similar
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 System.Text; | |
// Create the output | |
var output = new StringWriter(); | |
// Create the console | |
var console = AnsiConsole.Create(new AnsiConsoleSettings | |
{ | |
Ansi = AnsiSupport.Yes, | |
Out = new CustomOuput(output, 80, 24), | |
Interactive = InteractionSupport.No, | |
}); | |
// Write something to the console | |
console.WriteLine("Hello, World!"); | |
public sealed class CustomOuput : IAnsiConsoleOutput | |
{ | |
public TextWriter Writer { get; } | |
public bool IsTerminal { get; } | |
public int Width { get; } | |
public int Height { get; } | |
public CustomOuput(TextWriter writer, int width, int height) | |
{ | |
Writer = writer; | |
IsTerminal = true; | |
Width = width; | |
Height = height; | |
} | |
public void SetEncoding(Encoding encoding) | |
{ | |
// TODO: Not implemented | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment