Yaml Syntax Highlighting

The AnsiConsoleYamlExtensions class provides extension methods for writing Yaml content to the console with syntax highlighting.

Methods

WriteYamlAsync (Stream, default styles)

Writes Yaml content from a stream to the console with default styling asynchronously.

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

Parameters:

Returns: The Yaml content as a string.

WriteYamlAsync (Stream, custom styles)

Writes Yaml content from a stream to the console with custom styling asynchronously.

Task<string> WriteYamlAsync(this IAnsiConsole ansiConsole, Stream stream, YamlStyles yamlStyles)

Parameters:

Returns: The Yaml content as a string.

WriteYaml (Stream, default styles)

Writes Yaml content from a stream to the console with default styling synchronously.

string WriteYaml(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The Yaml content as a string.

WriteYaml (Stream, custom styles)

Writes Yaml content from a stream to the console with custom styling synchronously.

string WriteYaml(this IAnsiConsole ansiConsole, Stream stream, YamlStyles yamlStyles)

Parameters:

Returns: The Yaml content as a string.

WriteYaml (string, default styles)

Writes Yaml content from a string to the console with default styling.

void WriteYaml(this IAnsiConsole ansiConsole, string value)

Parameters:

WriteYaml (string, custom styles)

Writes Yaml content from a string to the console with custom styling.

void WriteYaml(this IAnsiConsole ansiConsole, string value, YamlStyles yamlStyles)

Parameters:

Example Usage

Basic Usage with String

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

var yamlString = """
    ---
    # A sample yaml file
    company: spacelift
    domain:
        - indeed
    tutorial:
        - yaml:
            name: "This is a string"
            type: awesome
            born: 2001
    """;

AnsiConsole.Console.WriteYaml(yamlString);

Custom Styles

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

var customYamlStyles = YamlStyles.Default;
customYamlStyles.PropertyName = new Style(Color.Orange1);

AnsiConsole.Console.WriteYaml(yamlString, customYamlStyles);

Async with Stream

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

await AnsiConsole.Console.WriteYamlAsync(stream);