Skip to content

Commit

Permalink
Add Layout widget (#1041)
Browse files Browse the repository at this point in the history
* Add width to panels
* Add height to panels
* Replace RenderContext with RenderOptions
* Remove exclusivity from alternative buffer
* Add Layout widget
* Add Align widget
  • Loading branch information
patriksvensson authored Nov 15, 2022
1 parent 9ce3b99 commit c3ec6a7
Show file tree
Hide file tree
Showing 137 changed files with 2,650 additions and 386 deletions.
2 changes: 1 addition & 1 deletion examples/Console/Borders/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static IRenderable CreateTable(string name, TableBorder border)
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();
}
}
2 changes: 1 addition & 1 deletion examples/Console/Canvas/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void Main()
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);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/Console/Colors/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void Main()
{
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++)
Expand All @@ -46,7 +46,7 @@ public static void Main()
{
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++)
Expand All @@ -69,7 +69,7 @@ public static void Main()
{
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++)
Expand All @@ -96,7 +96,7 @@ public static void Main()
{
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));
Expand Down
8 changes: 4 additions & 4 deletions examples/Console/Exceptions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public static async Task Main(string[] args)
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
{
Expand All @@ -56,7 +56,7 @@ public static async Task Main(string[] args)
catch (Exception ex)
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule("Async").LeftAligned());
AnsiConsole.Write(new Rule("Async").LeftJustified());
AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenPaths);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/Console/Figlet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
15 changes: 15 additions & 0 deletions examples/Console/Layout/Layout.csproj
Original file line number Diff line number Diff line change
@@ -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>
60 changes: 60 additions & 0 deletions examples/Console/Layout/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions examples/Console/Panels/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void Main()

// 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[/]"));
Expand All @@ -32,7 +32,7 @@ public static void Main()

// 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[/]")
Expand Down
4 changes: 2 additions & 2 deletions examples/Console/Paths/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ private static void WriteAligned(string path)
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);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/Console/Prompt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void Main(string[] args)

// 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)
Expand All @@ -63,7 +63,7 @@ public static void Main(string[] args)
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()
Expand Down
6 changes: 3 additions & 3 deletions examples/Console/Rules/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public static void Main(string[] args)
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(
Expand All @@ -31,7 +31,7 @@ public static void Main(string[] args)
Render(
new Rule("[red]Right aligned[/]")
.RuleStyle(Style.Parse("blue"))
.RightAligned());
.RightJustified());
}

private static void Render(Rule rule)
Expand Down
14 changes: 14 additions & 0 deletions examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paths", "Console\Paths\Path
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectre.Console.Cli", "..\src\Spectre.Console.Cli\Spectre.Console.Cli.csproj", "{EFAADF6A-C77D-41EC-83F5-BBB4FFC5A6D7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Layout", "Console\Layout\Layout.csproj", "{A9FDE73A-8452-4CA3-B366-3F900597E132}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -507,6 +509,18 @@ Global
{EFAADF6A-C77D-41EC-83F5-BBB4FFC5A6D7}.Release|x64.Build.0 = Release|Any CPU
{EFAADF6A-C77D-41EC-83F5-BBB4FFC5A6D7}.Release|x86.ActiveCfg = Release|Any CPU
{EFAADF6A-C77D-41EC-83F5-BBB4FFC5A6D7}.Release|x86.Build.0 = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|x64.ActiveCfg = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|x64.Build.0 = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|x86.ActiveCfg = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Debug|x86.Build.0 = Debug|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|Any CPU.Build.0 = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|x64.ActiveCfg = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|x64.Build.0 = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|x86.ActiveCfg = Release|Any CPU
{A9FDE73A-8452-4CA3-B366-3F900597E132}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions examples/Shared/ColorBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public ColorBox(int width, int height)
_width = width;
}

protected override Measurement Measure(RenderContext context, int maxWidth)
protected override Measurement Measure(RenderOptions options, int maxWidth)
{
return new Measurement(1, GetWidth(maxWidth));
}

protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
protected override IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
{
maxWidth = GetWidth(maxWidth);

Expand Down
8 changes: 4 additions & 4 deletions src/Spectre.Console.Cli/Internal/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public Composer Join(string separator, IEnumerable<string> composers)
return this;
}

public Measurement Measure(RenderContext context, int maxWidth)
public Measurement Measure(RenderOptions options, int maxWidth)
{
return ((IRenderable)new Markup(_content.ToString())).Measure(context, maxWidth);
return ((IRenderable)new Markup(_content.ToString())).Measure(options, maxWidth);
}

public IEnumerable<Segment> Render(RenderContext context, int maxWidth)
public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
{
return ((IRenderable)new Markup(_content.ToString())).Render(context, maxWidth);
return ((IRenderable)new Markup(_content.ToString())).Render(options, maxWidth);
}

public override string ToString()
Expand Down
6 changes: 3 additions & 3 deletions src/Spectre.Console.ImageSharp/CanvasImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public CanvasImage(Stream data)
}

/// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth)
protected override Measurement Measure(RenderOptions options, int maxWidth)
{
if (PixelWidth < 0)
{
Expand All @@ -88,7 +88,7 @@ protected override Measurement Measure(RenderContext context, int maxWidth)
}

/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
protected override IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
{
var image = Image;

Expand Down Expand Up @@ -138,6 +138,6 @@ protected override IEnumerable<Segment> Render(RenderContext context, int maxWid
}
}

return ((IRenderable)canvas).Render(context, maxWidth);
return ((IRenderable)canvas).Render(options, maxWidth);
}
}
14 changes: 10 additions & 4 deletions src/Spectre.Console.Testing/TestCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ public sealed class TestCapabilities : IReadOnlyCapabilities
public bool Unicode { get; set; }

/// <summary>
/// Creates a <see cref="RenderContext"/> with the same capabilities as this instace.
/// Creates a <see cref="RenderOptions"/> with the same capabilities as this instace.
/// </summary>
/// <returns>A <see cref="RenderContext"/> with the same capabilities as this instace.</returns>
public RenderContext CreateRenderContext()
/// <param name="console">The console.</param>
/// <returns>A <see cref="RenderOptions"/> with the same capabilities as this instace.</returns>
public RenderOptions CreateRenderContext(IAnsiConsole console)
{
return new RenderContext(this);
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}

return RenderOptions.Create(console, this);
}
}
25 changes: 25 additions & 0 deletions src/Spectre.Console.Testing/TestConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ public static TestConsole Width(this TestConsole console, int width)
return console;
}

/// <summary>
/// Sets the console height.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="width">The console height.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole Height(this TestConsole console, int width)
{
console.Profile.Height = width;
return console;
}

/// <summary>
/// Sets the console size.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="size">The console size.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TestConsole Size(this TestConsole console, Size size)
{
console.Profile.Width = size.Width;
console.Profile.Height = size.Height;
return console;
}

/// <summary>
/// Turns on emitting of VT/ANSI sequences.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Spectre.Console.v3.ncrunchsolution
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<SolutionConfiguration>
<Settings>
<AllowParallelTestExecution>True</AllowParallelTestExecution>
<SolutionConfigured>True</SolutionConfigured>
</Settings>
</SolutionConfiguration>
Loading

0 comments on commit c3ec6a7

Please sign in to comment.