mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-01 01:25:27 +08:00 
			
		
		
		
	Add JSON text renderer (#1086)
* Add JsonText widget to render highlighted JSON Closes #1051
This commit is contained in:
		| @@ -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)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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[/]?") | ||||
|   | ||||
| @@ -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)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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")); | ||||
|         } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <OutputType>Exe</OutputType> | ||||
| @@ -51,8 +51,9 @@ | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> | ||||
|     <ProjectReference Include="..\..\..\src\Spectre.Console.Cli\Spectre.Console.Cli.csproj" /> | ||||
|     <ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" /> | ||||
|     <ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" /> | ||||
|     <ProjectReference Include="..\..\..\src\Spectre.Console.Json\Spectre.Console.Json.csproj" /> | ||||
|   </ItemGroup> | ||||
| </Project> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Patrik Svensson
					Patrik Svensson