Table of Contents

Method AddMessageWriter

Namespace
Ploch.CommandLine.Spectre.Output
Assembly
Ploch.CommandLine.Spectre.dll

AddMessageWriter<TMessageType, TWriter>(IServiceCollection)

Registers a message writer for a specific message type in the service collection.

public static IServiceCollection AddMessageWriter<TMessageType, TWriter>(this IServiceCollection service) where TWriter : class, IMessageWriter<TMessageType>

Parameters

service IServiceCollection

The service collection to add the writer to.

Returns

IServiceCollection

The same service collection instance to enable method chaining.

Type Parameters

TMessageType

The type of message that the writer can write.

TWriter

The type of the writer implementation.

Examples

public sealed class QuotingStringWriter(IAnsiConsole console) : TypeMessageWriter<string>
{
    public override void Write(string? message, IMessageFormatterProcessor? formatterProcessor = null, IFormatProvider? formatProvider = null) =>
        console.Write($"\"{message}\"");
}

AppBuilder.Create(args)
          .ConfigureServices(services => services.AddMessageWriter<string, QuotingStringWriter>());

Remarks

A writer registered for a type that already has one replaces it for that exact type, so this is how the rendering of a string or a FormattableString written through Write<TMessage>(TMessage, IFormatProvider?) is changed. A writer takes the IAnsiConsole it renders to, not IOutput, which depends on the writers.