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:
Phil Scott
2022-03-22 17:34:25 -04:00
committed by GitHub
parent 1f2629e2e1
commit 1d8154f9b0
6 changed files with 234 additions and 0 deletions

View File

@ -60,6 +60,10 @@ Spectre.Console will tell your terminal to use the color that is configured in t
If you are using an 8 or 24-bit color for the foreground text, it is recommended that you also set an appropriate
background color to match.
**Do** escape data when outputting any user input or any external data via Markup using the [`EscapeMarkup`](xref:M:Spectre.Console.Markup.Escape(System.String)) method on the data. Any user input containing `[` or `]` will likely cause a runtime error while rendering otherwise.
**Consider** replacing `Markup` and `MarkupLine` with [`MarkupInterpolated`](xref:M:Spectre.Console.AnsiConsole.MarkupInterpolated(System.FormattableString)) and [`MarkupLineInterpolated`](xref:M:Spectre.Console.AnsiConsole.MarkupLineInterpolated(System.FormattableString)). Both these methods will automatically escape all data in the interpolated string holes. When working with widgets such as the Table and Tree, consider using [`Markup.FromInterpolated`](xref:M:Spectre.Console.Markup.FromInterpolated(System.FormattableString,Spectre.Console.Style)) to generate an `IRenderable` from an interpolated string.
### Live-Rendering Best Practices
Spectre.Console has a variety of [live-rendering capabilities](live) widgets. These widgets can be used to display data

View File

@ -63,6 +63,15 @@ You can also use the `Markup.Escape` method.
```csharp
AnsiConsole.Markup("[red]{0}[/]", Markup.Escape("Hello [World]"));
```
## Escaping Interpolated Strings
When working with interpolated string, you can use the `MarkupInterpolated` and `MarkupInterpolatedLine` methods to automatically escape the values in the interpolated string holes.
```csharp
AnsiConsole.MarkupInterpolated("[red]{0}[/]", "Hello [World]");
```
## Setting background color
You can set the background color in markup by prefixing the color with