Add JSON text renderer (#1086)

* Add JsonText widget to render highlighted JSON

Closes #1051
This commit is contained in:
Patrik Svensson
2022-12-31 19:17:15 +01:00
committed by GitHub
parent 3e6e0990c5
commit 54be64ec84
48 changed files with 1634 additions and 21 deletions

View File

@ -0,0 +1,68 @@
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"foo": true,
"bar": false,
"qux": 32,
"corgi": null,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular",
"min": 0
},
{
"id": "1002",
"type": "Chocolate",
"min": 0.32
},
{
"id": "1003",
"min": 12.32,
"type": "Blueberry"
},
{
"id": "1004",
"min": 0.32E-12,
"type": "Devil's Food"
}
]
},
"topping": [
{
"id": "5001",
"min": 0.32e-12,
"type": "None"
},
{
"id": "5002",
"min": 0.32E+12,
"type": "Glazed"
},
{
"id": "5005",
"min": 0.32e+12,
"type": "Sugar"
},
{
"id": "5007",
"min": 0.32e12,
"type": "Powdered Sugar"
},
{
"id": "5006",
"type": "Chocolate with Sprinkles"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
}

View File

@ -0,0 +1,70 @@
┌─Some JSON───────────────────────────────────┐
│ { │
│ "id": "0001", │
│ "type": "donut", │
│ "name": "Cake", │
│ "ppu": 0.55, │
│ "foo": true, │
│ "bar": false, │
│ "qux": 32, │
│ "corgi": null, │
│ "batters": { │
│ "batter": [ │
│ { │
│ "id": "1001", │
│ "type": "Regular", │
│ "min": 0 │
│ }, │
│ { │
│ "id": "1002", │
│ "type": "Chocolate", │
│ "min": 0.32 │
│ }, │
│ { │
│ "id": "1003", │
│ "min": 12.32, │
│ "type": "Blueberry" │
│ }, │
│ { │
│ "id": "1004", │
│ "min": 0.32E-12, │
│ "type": "Devil's Food" │
│ } │
│ ] │
│ }, │
│ "topping": [ │
│ { │
│ "id": "5001", │
│ "min": 0.32e-12, │
│ "type": "None" │
│ }, │
│ { │
│ "id": "5002", │
│ "min": 0.32E+12, │
│ "type": "Glazed" │
│ }, │
│ { │
│ "id": "5005", │
│ "min": 0.32e+12, │
│ "type": "Sugar" │
│ }, │
│ { │
│ "id": "5007", │
│ "min": 0.32e12, │
│ "type": "Powdered Sugar" │
│ }, │
│ { │
│ "id": "5006", │
│ "type": "Chocolate with Sprinkles" │
│ }, │
│ { │
│ "id": "5003", │
│ "type": "Chocolate" │
│ }, │
│ { │
│ "id": "5004", │
│ "type": "Maple" │
│ } │
│ ] │
│ } │
└─────────────────────────────────────────────┘

View File

@ -0,0 +1,17 @@
namespace Spectre.Console.Tests;
public static class StreamExtensions
{
public static string ReadText(this Stream stream)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}

View File

@ -10,6 +10,7 @@ global using System.Threading;
global using System.Threading.Tasks;
global using Shouldly;
global using Spectre.Console.Advanced;
global using Spectre.Console.Json;
global using Spectre.Console.Rendering;
global using Spectre.Console.Testing;
global using Spectre.Console.Tests.Data;

View File

@ -6,7 +6,9 @@
<ItemGroup>
<AdditionalFiles Include="..\..\src\stylecop.json" Link="Properties/stylecop.json" />
<None Remove="Data\example.json" />
<None Remove="Data\starwars.flf" />
<EmbeddedResource Include="Data\example.json" />
<EmbeddedResource Include="Data\starwars.flf" />
<None Remove="Data\poison.flf" />
<EmbeddedResource Include="Data\poison.flf" />
@ -29,6 +31,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Spectre.Console.Cli\Spectre.Console.Cli.csproj" />
<ProjectReference Include="..\..\src\Spectre.Console.Json\Spectre.Console.Json.csproj" />
<ProjectReference Include="..\..\src\Spectre.Console.Testing\Spectre.Console.Testing.csproj" />
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>

View File

@ -0,0 +1,23 @@
namespace Spectre.Console.Tests.Unit;
[UsesVerify]
[ExpectationPath("Widgets/Json")]
public sealed class JsonTextTests
{
[Fact]
[Expectation("Render_Json")]
public Task Should_Render_Json()
{
// Given
var console = new TestConsole().Size(new Size(80, 15));
var json = EmbeddedResourceReader
.LoadResourceStream("Spectre.Console.Tests/Data/example.json")
.ReadText();
// When
console.Write(new Panel(new JsonText(json)).Header("Some JSON"));
// Then
return Verifier.Verify(console.Output);
}
}