Skip to content

Feature request: ToAsciiString() - returns a string without colors and special characters #1125

Answered by patriksvensson
Sire asked this question in Q&A
Discussion options

You must be logged in to vote

This can be done by using a Recorder which was designed for this purpose.

Using the static API

// Start recording
AnsiConsole.Record();

// Write stuff to the console
AnsiConsole.Write(new Table() ... );

// Get output as string
string text = AnsiConsole.ExportText();

// Export using a custom encoder
string custom = AnsiConsole.Export(new MyOutputEncoder());

Using IAnsiConsole

using(var recorder = new Recorder(console)) 
{
	// Write stuff to the console
	console.Write(new Table() ... );

	// Get output as string
	string text = recorder.ExportText();

	// Export using a custom encoder
	string custom = recorder.Export(new MyOutputEncoder());
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@patriksvensson
Comment options

@TLevitt
Comment options

Answer selected by patriksvensson
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants
Converted from issue

This discussion was converted from issue #1124 on January 04, 2023 22:09.