Skip to content

Instantly share code, notes, and snippets.

@patriksvensson
Created May 29, 2023 18:46
Show Gist options
  • Save patriksvensson/c754a8ef4bf2173e7834a6ea1f3c404f to your computer and use it in GitHub Desktop.
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
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