spectre.console/src/Spectre.Console/Extensions/AnsiConsoleExtensions.Markup.cs
Phil Scott 1d8154f9b0
Introduce MarkupInterpolated and MarkupLineInterpolated extensions (#761)
* Introduce MarkupInterpolated and MarkupLineInterpolated extensions

These new methods enable easily writing markup with a nice and intuitive syntax without having to worry about escaping the markup.

```csharp
string input = args[0];
string output = Process(input);
AnsiConsole.MarkupLineInterpolated($"[blue]{input}[/] -> [green]{output}[/]");

```

The `Interpolated` suffix was inspired by the Entity Framework Core [FromSqlInterpolated][1] method.

[1]: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#passing-parameters

* Fixing whitespace to match file scoped namespaces

* Adding FromInterpolated helper to Markup widget

Allows automatic handling of interpolated strings to be used on the Markup widget. This would be helpful for people working with Tables and the Tree control who would not be using the MarkupInterpolated methods.

* Documentation for markup interpolated methods.

Co-authored-by: Cédric Luthi <cedric.luthi@gmail.com>
2022-03-22 22:34:25 +01:00

151 lines
6.5 KiB
C#

namespace Spectre.Console;
/// <summary>
/// Contains extension methods for <see cref="IAnsiConsole"/>.
/// </summary>
public static partial class AnsiConsoleExtensions
{
/// <summary>
/// Writes the specified markup to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">An array of objects to write.</param>
public static void Markup(this IAnsiConsole console, string format, params object[] args)
{
Markup(console, CultureInfo.CurrentCulture, format, args);
}
/// <summary>
/// Writes the specified markup to the console.
/// <para/>
/// All interpolation holes which contain a string are automatically escaped so you must not call <see cref="StringExtensions.EscapeMarkup"/>.
/// </summary>
/// <example>
/// <code>
/// string input = args[0];
/// string output = Process(input);
/// console.MarkupInterpolated($"[blue]{input}[/] -> [green]{output}[/]");
/// </code>
/// </example>
/// <param name="console">The console to write to.</param>
/// <param name="value">The interpolated string value to write.</param>
public static void MarkupInterpolated(this IAnsiConsole console, FormattableString value)
{
MarkupInterpolated(console, CultureInfo.CurrentCulture, value);
}
/// <summary>
/// Writes the specified markup to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">An array of objects to write.</param>
public static void Markup(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args)
{
Markup(console, string.Format(provider, format, args));
}
/// <summary>
/// Writes the specified markup to the console.
/// <para/>
/// All interpolation holes which contain a string are automatically escaped so you must not call <see cref="StringExtensions.EscapeMarkup"/>.
/// </summary>
/// <example>
/// <code>
/// string input = args[0];
/// string output = Process(input);
/// console.MarkupInterpolated(CultureInfo.InvariantCulture, $"[blue]{input}[/] -> [green]{output}[/]");
/// </code>
/// </example>
/// <param name="console">The console to write to.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="value">The interpolated string value to write.</param>
public static void MarkupInterpolated(this IAnsiConsole console, IFormatProvider provider, FormattableString value)
{
Markup(console, Console.Markup.EscapeInterpolated(provider, value));
}
/// <summary>
/// Writes the specified markup to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="value">The value to write.</param>
public static void Markup(this IAnsiConsole console, string value)
{
console.Write(MarkupParser.Parse(value));
}
/// <summary>
/// Writes the specified markup, followed by the current line terminator, to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">An array of objects to write.</param>
public static void MarkupLine(this IAnsiConsole console, string format, params object[] args)
{
MarkupLine(console, CultureInfo.CurrentCulture, format, args);
}
/// <summary>
/// Writes the specified markup, followed by the current line terminator, to the console.
/// <para/>
/// All interpolation holes which contain a string are automatically escaped so you must not call <see cref="StringExtensions.EscapeMarkup"/>.
/// </summary>
/// <example>
/// <code>
/// string input = args[0];
/// string output = Process(input);
/// console.MarkupLineInterpolated($"[blue]{input}[/] -> [green]{output}[/]");
/// </code>
/// </example>
/// <param name="console">The console to write to.</param>
/// <param name="value">The interpolated string value to write.</param>
public static void MarkupLineInterpolated(this IAnsiConsole console, FormattableString value)
{
MarkupLineInterpolated(console, CultureInfo.CurrentCulture, value);
}
/// <summary>
/// Writes the specified markup, followed by the current line terminator, to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="value">The value to write.</param>
public static void MarkupLine(this IAnsiConsole console, string value)
{
Markup(console, value + Environment.NewLine);
}
/// <summary>
/// Writes the specified markup, followed by the current line terminator, to the console.
/// </summary>
/// <param name="console">The console to write to.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">An array of objects to write.</param>
public static void MarkupLine(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args)
{
Markup(console, provider, format + Environment.NewLine, args);
}
/// <summary>
/// Writes the specified markup, followed by the current line terminator, to the console.
/// <para/>
/// All interpolation holes which contain a string are automatically escaped so you must not call <see cref="StringExtensions.EscapeMarkup"/>.
/// </summary>
/// <example>
/// <code>
/// string input = args[0];
/// string output = Process(input);
/// console.MarkupLineInterpolated(CultureInfo.InvariantCulture, $"[blue]{input}[/] -> [green]{output}[/]");
/// </code>
/// </example>
/// <param name="console">The console to write to.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="value">The interpolated string value to write.</param>
public static void MarkupLineInterpolated(this IAnsiConsole console, IFormatProvider provider, FormattableString value)
{
MarkupLine(console, Console.Markup.EscapeInterpolated(provider, value));
}
}