mirror of
https://github.com/nsnail/spectre.console.git
synced 2026-08-15 00:57:07 +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:
co-authored by
Cédric Luthi
parent
1f2629e2e1
commit
1d8154f9b0
@@ -72,6 +72,25 @@ public sealed class MarkupTests
|
||||
// Then
|
||||
result.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Hello", "World", "\x1B[38;5;11mHello\x1B[0m \x1B[38;5;9mWorld\x1B[0m 2021-02-03")]
|
||||
[InlineData("Hello", "World [", "\x1B[38;5;11mHello\x1B[0m \x1B[38;5;9mWorld [\x1B[0m 2021-02-03")]
|
||||
[InlineData("Hello", "World ]", "\x1B[38;5;11mHello\x1B[0m \x1B[38;5;9mWorld ]\x1B[0m 2021-02-03")]
|
||||
[InlineData("[Hello]", "World", "\x1B[38;5;11m[Hello]\x1B[0m \x1B[38;5;9mWorld\x1B[0m 2021-02-03")]
|
||||
[InlineData("[[Hello]]", "[World]", "\x1B[38;5;11m[[Hello]]\x1B[0m \x1B[38;5;9m[World]\x1B[0m 2021-02-03")]
|
||||
public void Should_Escape_Markup_When_Using_MarkupInterpolated(string input1, string input2, string expected)
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole().EmitAnsiSequences();
|
||||
var date = new DateTime(2021, 2, 3);
|
||||
|
||||
// When
|
||||
console.MarkupInterpolated($"[yellow]{input1}[/] [red]{input2}[/] {date:yyyy-MM-dd}");
|
||||
|
||||
// Then
|
||||
console.Output.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -134,4 +153,25 @@ public sealed class MarkupTests
|
||||
console.Output.NormalizeLineEndings()
|
||||
.ShouldBe("{\n");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Can_Use_Interpolated_Markup_As_IRenderable()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
const string Num = "[value[";
|
||||
var table = new Table().AddColumns("First Column");
|
||||
table.AddRow(Markup.FromInterpolated($"Result: {Num}"));
|
||||
|
||||
// When
|
||||
console.Write(table);
|
||||
|
||||
// Then
|
||||
console.Output.NormalizeLineEndings().ShouldBe(@"┌─────────────────┐
|
||||
│ First Column │
|
||||
├─────────────────┤
|
||||
│ Result: [value[ │
|
||||
└─────────────────┘
|
||||
".NormalizeLineEndings());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user