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

@ -8,9 +8,9 @@ namespace Generator.Commands.Samples
public override void Run(IAnsiConsole console)
{
console.Write(new FigletText("Left aligned").LeftAligned().Color(Color.Red));
console.Write(new FigletText("Left aligned").LeftJustified().Color(Color.Red));
console.Write(new FigletText("Centered").Centered().Color(Color.Green));
console.Write(new FigletText("Right aligned").RightAligned().Color(Color.Blue));
console.Write(new FigletText("Right aligned").RightJustified().Color(Color.Blue));
}
}
}

View File

@ -19,7 +19,7 @@ namespace Generator.Commands.Samples
console.DisplayThenType(c => password = AskPassword(c), "hunter2↲");
console.DisplayThenType(c => color = AskColor(c), "↲");
AnsiConsole.Write(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.Write(new Table().AddColumns("[grey]Question[/]", "[grey]Answer[/]")
.RoundedBorder()
.BorderColor(Color.Grey)
@ -33,7 +33,7 @@ namespace Generator.Commands.Samples
private static string AskName(IAnsiConsole console)
{
console.WriteLine();
console.Write(new Rule("[yellow]Strings[/]").RuleStyle("grey").LeftAligned());
console.Write(new Rule("[yellow]Strings[/]").RuleStyle("grey").LeftJustified());
var name = console.Ask<string>("What's your [green]name[/]?");
return name;
}
@ -42,7 +42,7 @@ namespace Generator.Commands.Samples
private static string AskSport(IAnsiConsole console)
{
console.WriteLine();
console.Write(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftAligned());
console.Write(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftJustified());
return console.Prompt(
new TextPrompt<string>("What's your [green]favorite sport[/]?")
@ -56,7 +56,7 @@ namespace Generator.Commands.Samples
private static int AskAge(IAnsiConsole console)
{
console.WriteLine();
console.Write(new Rule("[yellow]Integers[/]").RuleStyle("grey").LeftAligned());
console.Write(new Rule("[yellow]Integers[/]").RuleStyle("grey").LeftJustified());
return console.Prompt(
new TextPrompt<int>("How [green]old[/] are you?")
@ -76,7 +76,7 @@ namespace Generator.Commands.Samples
private static string AskPassword(IAnsiConsole console)
{
console.WriteLine();
console.Write(new Rule("[yellow]Secrets[/]").RuleStyle("grey").LeftAligned());
console.Write(new Rule("[yellow]Secrets[/]").RuleStyle("grey").LeftJustified());
return console.Prompt(
new TextPrompt<string>("Enter [green]password[/]?")
@ -87,7 +87,7 @@ namespace Generator.Commands.Samples
private static string AskColor(IAnsiConsole console)
{
console.WriteLine();
console.Write(new Rule("[yellow]Optional[/]").RuleStyle("grey").LeftAligned());
console.Write(new Rule("[yellow]Optional[/]").RuleStyle("grey").LeftJustified());
return console.Prompt(
new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?")

View File

@ -0,0 +1,39 @@
using Spectre.Console;
using Spectre.Console.Json;
namespace Generator.Commands.Samples
{
public class JsonSample : BaseSample
{
public override (int Cols, int Rows) ConsoleSize => (60, 20);
public override void Run(IAnsiConsole console)
{
var json = new JsonText(
"""
{
"hello": 32,
"world": {
"foo": 21,
"bar": 255,
"baz": [
0.32, 0.33e-32,
0.42e32, 0.55e+32,
{
"hello": "world",
"lol": null
}
]
}
}
""");
AnsiConsole.Write(
new Panel(json)
.Header("Some JSON in a panel")
.Collapse()
.RoundedBorder()
.BorderColor(Color.Yellow));
}
}
}

View File

@ -10,11 +10,11 @@ namespace Generator.Commands.Samples
{
console.Write(new Rule());
console.WriteLine();
console.Write(new Rule("[blue]Left aligned[/]").LeftAligned().RuleStyle("red"));
console.Write(new Rule("[blue]Left aligned[/]").LeftJustified().RuleStyle("red"));
console.WriteLine();
console.Write(new Rule("[green]Centered[/]").Centered().RuleStyle("green"));
console.WriteLine();
console.Write(new Rule("[red]Right aligned[/]").RightAligned().RuleStyle("blue"));
console.Write(new Rule("[red]Right aligned[/]").RightJustified().RuleStyle("blue"));
console.WriteLine();
}
}

View File

@ -34,9 +34,9 @@ namespace Generator.Commands.Samples
console.Write(
new Panel(
new Padder(new Rows(
new TextPath("/This/Is/A/Long/Path/That/Will/Be/Truncated.txt").LeftAligned(),
new TextPath("/This/Is/A/Long/Path/That/Will/Be/Truncated.txt").LeftJustified(),
new TextPath("/This/Is/A/Long/Path/That/Will/Be/Truncated.txt").Centered(),
new TextPath("/This/Is/A/Long/Path/That/Will/Be/Truncated.txt").RightAligned()), new Padding(0,1)))
new TextPath("/This/Is/A/Long/Path/That/Will/Be/Truncated.txt").RightJustified()), new Padding(0,1)))
.BorderStyle(new Style(foreground: Color.Grey))
.Header("Alignment"));
}