mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
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>
This commit is contained in:
@ -24,6 +24,24 @@ public static partial class AnsiConsole
|
||||
Console.Markup(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);
|
||||
/// AnsiConsole.MarkupInterpolated($"[blue]{input}[/] -> [green]{output}[/]");
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="value">The interpolated string value to write.</param>
|
||||
public static void MarkupInterpolated(FormattableString value)
|
||||
{
|
||||
Console.MarkupInterpolated(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified markup to the console.
|
||||
/// </summary>
|
||||
@ -35,6 +53,25 @@ public static partial class AnsiConsole
|
||||
Console.Markup(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);
|
||||
/// AnsiConsole.MarkupInterpolated(CultureInfo.InvariantCulture, $"[blue]{input}[/] -> [green]{output}[/]");
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <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(IFormatProvider provider, FormattableString value)
|
||||
{
|
||||
Console.MarkupInterpolated(provider, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified markup, followed by the current line terminator, to the console.
|
||||
/// </summary>
|
||||
@ -54,6 +91,24 @@ public static partial class AnsiConsole
|
||||
Console.MarkupLine(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);
|
||||
/// AnsiConsole.MarkupLineInterpolated($"[blue]{input}[/] -> [green]{output}[/]");
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="value">The interpolated string value to write.</param>
|
||||
public static void MarkupLineInterpolated(FormattableString value)
|
||||
{
|
||||
Console.MarkupLineInterpolated(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified markup, followed by the current line terminator, to the console.
|
||||
/// </summary>
|
||||
@ -64,4 +119,23 @@ public static partial class AnsiConsole
|
||||
{
|
||||
Console.MarkupLine(provider, 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);
|
||||
/// AnsiConsole.MarkupLineInterpolated(CultureInfo.InvariantCulture, $"[blue]{input}[/] -> [green]{output}[/]");
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <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(IFormatProvider provider, FormattableString value)
|
||||
{
|
||||
Console.MarkupLineInterpolated(provider, value);
|
||||
}
|
||||
}
|
@ -16,6 +16,25 @@ public static partial class AnsiConsoleExtensions
|
||||
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>
|
||||
@ -28,6 +47,26 @@ public static partial class AnsiConsoleExtensions
|
||||
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>
|
||||
@ -49,6 +88,25 @@ public static partial class AnsiConsoleExtensions
|
||||
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>
|
||||
@ -70,4 +128,24 @@ public static partial class AnsiConsoleExtensions
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
@ -54,6 +54,29 @@ public sealed class Markup : Renderable, IAlignable, IOverflowable
|
||||
return ((IRenderable)_paragraph).Render(context, maxWidth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new instance of a Markup widget from an interpolated string.
|
||||
/// </summary>
|
||||
/// <param name="value">The interpolated string value to write.</param>
|
||||
/// <param name="style">The style of the text.</param>
|
||||
/// <returns>A new markup instance.</returns>
|
||||
public static Markup FromInterpolated(FormattableString value, Style? style = null)
|
||||
{
|
||||
return FromInterpolated(CultureInfo.CurrentCulture, value, style);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new instance of a Markup widget from an interpolated string.
|
||||
/// </summary>
|
||||
/// <param name="provider">The format provider to use.</param>
|
||||
/// <param name="value">The interpolated string value to write.</param>
|
||||
/// <param name="style">The style of the text.</param>
|
||||
/// <returns>A new markup instance.</returns>
|
||||
public static Markup FromInterpolated(IFormatProvider provider, FormattableString value, Style? style = null)
|
||||
{
|
||||
return new Markup(EscapeInterpolated(provider, value), style);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Escapes text so that it won’t be interpreted as markup.
|
||||
/// </summary>
|
||||
@ -83,4 +106,10 @@ public sealed class Markup : Renderable, IAlignable, IOverflowable
|
||||
|
||||
return text.RemoveMarkup();
|
||||
}
|
||||
|
||||
internal static string EscapeInterpolated(IFormatProvider provider, FormattableString value)
|
||||
{
|
||||
object?[] args = value.GetArguments().Select(arg => arg is string s ? s.EscapeMarkup() : arg).ToArray();
|
||||
return string.Format(provider, value.Format, args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user