Add Layout widget (#1041)

* Add width to panels
* Add height to panels
* Replace RenderContext with RenderOptions
* Remove exclusivity from alternative buffer
* Add Layout widget
* Add Align widget
This commit is contained in:
Patrik Svensson
2022-11-15 10:12:17 +01:00
committed by GitHub
parent 9ce3b99cd6
commit c3ec6a7363
137 changed files with 2651 additions and 387 deletions

View File

@ -84,7 +84,7 @@ public static class Program
private static void HorizontalRule(string title)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.WriteLine();
}
}

View File

@ -40,7 +40,7 @@ public static class Program
private static void Render(IRenderable canvas, string title)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule($"[yellow]{title}[/]").LeftAligned().RuleStyle("grey"));
AnsiConsole.Write(new Rule($"[yellow]{title}[/]").LeftJustified().RuleStyle("grey"));
AnsiConsole.WriteLine();
AnsiConsole.Write(canvas);
}

View File

@ -23,7 +23,7 @@ public static class Program
{
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.WriteLine();
for (var i = 0; i < 8; i++)
@ -46,7 +46,7 @@ public static class Program
{
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++)
@ -69,7 +69,7 @@ public static class Program
{
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++)
@ -96,7 +96,7 @@ public static class Program
{
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.Write(new ColorBox(width: 80, height: 15));

View File

@ -19,17 +19,17 @@ public static class Program
catch (Exception ex)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Default").LeftAligned());
AnsiConsole.Write(new Rule("Default").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex);
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Compact").LeftAligned());
AnsiConsole.Write(new Rule("Compact").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Compact + Custom colors").LeftAligned());
AnsiConsole.Write(new Rule("Compact + Custom colors").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, new ExceptionSettings
{
@ -56,7 +56,7 @@ public static class Program
catch (Exception ex)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Async").LeftAligned());
AnsiConsole.Write(new Rule("Async").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenPaths);
}

View File

@ -6,8 +6,8 @@ public static class Program
{
public static void Main(string[] args)
{
AnsiConsole.Write(new FigletText("Left aligned").LeftAligned().Color(Color.Red));
AnsiConsole.Write(new FigletText("Left aligned").LeftJustified().Color(Color.Red));
AnsiConsole.Write(new FigletText("Centered").Centered().Color(Color.Green));
AnsiConsole.Write(new FigletText("Right aligned").RightAligned().Color(Color.Blue));
AnsiConsole.Write(new FigletText("Right aligned").RightJustified().Color(Color.Blue));
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExampleTitle>Layout</ExampleTitle>
<ExampleDescription>Demonstrates how to use layouts.</ExampleDescription>
<ExampleGroup>Widgets</ExampleGroup>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,60 @@
using System;
using Spectre.Console;
namespace Layouts;
public static class Program
{
public static void Main()
{
var layout = CreateLayout();
AnsiConsole.Write(layout);
Console.ReadKey(true);
}
private static Layout CreateLayout()
{
var layout = new Layout();
layout.SplitRows(
new Layout("Top")
.SplitColumns(
new Layout("Left")
.SplitRows(
new Layout("LeftTop"),
new Layout("LeftBottom")),
new Layout("Right").Ratio(2),
new Layout("RightRight").Size(3)),
new Layout("Bottom"));
layout["LeftBottom"].Update(
new Panel("[blink]PRESS ANY KEY TO QUIT[/]")
.Expand()
.BorderColor(Color.Yellow)
.Padding(0, 0));
layout["Right"].Update(
new Panel(
new Table()
.AddColumns("[blue]Qux[/]", "[green]Corgi[/]")
.AddRow("9", "8")
.AddRow("7", "6")
.Expand())
.Header("A [yellow]Table[/] in a [blue]Panel[/] (Ratio=2)")
.Expand());
layout["RightRight"].Update(
new Panel("Explicit-size-is-[yellow]3[/]")
.BorderColor(Color.Yellow)
.Padding(0, 0));
layout["Bottom"].Update(
new Panel(
new FigletText("Hello World"))
.Header("Some [green]Figlet[/] text")
.Expand());
return layout;
}
}

View File

@ -17,7 +17,7 @@ public static class Program
// Left adjusted panel with text
AnsiConsole.Write(
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
new Panel(new Text("Left adjusted\nLeft").LeftJustified())
.Expand()
.SquareBorder()
.Header("[red]Left[/]"));
@ -32,7 +32,7 @@ public static class Program
// Right adjusted, rounded panel with text
AnsiConsole.Write(
new Panel(new Text("Right adjusted\nRight").RightAligned())
new Panel(new Text("Right adjusted\nRight").RightJustified())
.Expand()
.RoundedBorder()
.Header("[blue]Right[/]")

View File

@ -58,9 +58,9 @@ public static class Program
var table = new Table().BorderColor(Color.Grey).Title("Aligned").RoundedBorder();
table.AddColumns("[grey]Alignment[/]", "[grey]Path[/]");
table.AddRow(new Text("Left"), new TextPath(path).LeftAligned());
table.AddRow(new Text("Left"), new TextPath(path).LeftJustified());
table.AddRow(new Text("Center"), new TextPath(path).Centered());
table.AddRow(new Text("Right"), new TextPath(path).RightAligned());
table.AddRow(new Text("Right"), new TextPath(path).RightJustified());
AnsiConsole.Write(table);
}

View File

@ -46,7 +46,7 @@ namespace Prompt
// Summary
AnsiConsole.WriteLine();
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)
@ -63,7 +63,7 @@ namespace Prompt
private static void WriteDivider(string text)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule($"[yellow]{text}[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Write(new Rule($"[yellow]{text}[/]").RuleStyle("grey").LeftJustified());
}
public static bool AskConfirmation()

View File

@ -11,14 +11,14 @@ public static class Program
new Rule()
.RuleStyle(Style.Parse("yellow"))
.AsciiBorder()
.LeftAligned());
.LeftJustified());
// Left aligned title
Render(
new Rule("[blue]Left aligned[/]")
.RuleStyle(Style.Parse("red"))
.DoubleBorder()
.LeftAligned());
.LeftJustified());
// Centered title
Render(
@ -31,7 +31,7 @@ public static class Program
Render(
new Rule("[red]Right aligned[/]")
.RuleStyle(Style.Parse("blue"))
.RightAligned());
.RightJustified());
}
private static void Render(Rule rule)