C++ Syntax Highlighting

The AnsiConsoleCppExtensions class provides extension methods for IAnsiConsole to render C++ code with syntax highlighting.

Methods

WriteCppAsync (Stream, default styles)

Writes C++ code from a stream to the console with default syntax highlighting asynchronously.

Task<string> WriteCppAsync(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WriteCppAsync (Stream, custom styles)

Writes C++ code from a stream to the console with custom syntax highlighting asynchronously.

Task<string> WriteCppAsync(this IAnsiConsole ansiConsole, Stream stream, CppStyles cppStyles)

Parameters:

Returns: A task representing the asynchronous operation with the parsed string.

WriteCpp (Stream, default styles)

Writes C++ code from a stream to the console with default syntax highlighting synchronously.

string WriteCpp(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The parsed string.

WriteCpp (Stream, custom styles)

Writes C++ code from a stream to the console with custom syntax highlighting synchronously.

string WriteCpp(this IAnsiConsole ansiConsole, Stream stream, CppStyles cppStyles)

Parameters:

Returns: The parsed string.

WriteCpp (string, default styles)

Writes C++ code from a string to the console with default syntax highlighting.

void WriteCpp(this IAnsiConsole ansiConsole, string value)

Parameters:

WriteCpp (string, custom styles)

Writes C++ code from a string to the console with custom syntax highlighting.

void WriteCpp(this IAnsiConsole ansiConsole, string value, CppStyles cppStyles)

Parameters:

Example Usage

Basic Usage with String

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

var cppCode = @"
#include <iostream>

int main() {
    std::cout << ""Hello, World!"" << std::endl;
    return 0;
}
";

AnsiConsole.Console.WriteCpp(cppCode);

Custom Styles

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;
using NTokenizers.Extensions.Spectre.Console.Styles;

var customCppStyles = CppStyles.Default;
customCppStyles.Keyword = new Style(Color.Orange1);

AnsiConsole.Console.WriteCpp(cppCode, customCppStyles);

Async with Stream

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

await AnsiConsole.Console.WriteCppAsync(stream);