mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Add JSON text renderer (#1086)
* Add JsonText widget to render highlighted JSON Closes #1051
This commit is contained in:
68
test/Spectre.Console.Tests/Data/example.json
Normal file
68
test/Spectre.Console.Tests/Data/example.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
@ -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" │
|
||||
│ } │
|
||||
│ ] │
|
||||
│ } │
|
||||
└─────────────────────────────────────────────┘
|
17
test/Spectre.Console.Tests/Extensions/StreamExtensions.cs
Normal file
17
test/Spectre.Console.Tests/Extensions/StreamExtensions.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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>
|
||||
|
23
test/Spectre.Console.Tests/Unit/Widgets/JsonTextTests.cs
Normal file
23
test/Spectre.Console.Tests/Unit/Widgets/JsonTextTests.cs
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user