Add border support for panels

Closes #11
This commit is contained in:
Patrik Svensson 2020-08-05 15:24:18 +02:00 committed by Patrik Svensson
parent 108e56c229
commit 6d197c5140
7 changed files with 38 additions and 21 deletions

View File

@ -51,12 +51,18 @@ namespace Sample
// Nest some panels and text
AnsiConsole.Foreground = Color.Maroon;
AnsiConsole.Render(new Panel(new Panel(new Panel(new Panel(
Text.New(
"[underline]I[/] heard [underline on blue]you[/] like 📦\n\n\n\n" +
"So I put a 📦 in a 📦\nin a 📦 in a 📦\n\n" +
"😅",
foreground: Color.White), content: Justify.Center)))));
AnsiConsole.Render(
new Panel(
new Panel(
new Panel(
new Panel(
Text.New(
"[underline]I[/] heard [underline on blue]you[/] like 📦\n\n\n\n" +
"So I put a 📦 in a 📦\nin a 📦 in a 📦\n\n" +
"😅", foreground: Color.White),
content: Justify.Center,
border: BorderKind.Rounded))),
border: BorderKind.Ascii));
// Reset colors
AnsiConsole.ResetColors();

View File

@ -73,7 +73,7 @@ namespace Spectre.Console.Composition
/// <summary>
/// The right part of a cell.
/// </summary>
ColumnRight,
CellRight,
/// <summary>
/// The bottom left part of a footer.

View File

@ -25,7 +25,7 @@ namespace Spectre.Console.Composition
BorderPart.HeaderBottomRight => "|",
BorderPart.CellLeft => "|",
BorderPart.CellSeparator => "|",
BorderPart.ColumnRight => "|",
BorderPart.CellRight => "|",
BorderPart.FooterBottomLeft => "+",
BorderPart.FooterBottom => "-",
BorderPart.FooterBottomSeparator => "-",

View File

@ -25,7 +25,7 @@ namespace Spectre.Console.Composition
BorderPart.HeaderBottomRight => "┤",
BorderPart.CellLeft => "│",
BorderPart.CellSeparator => "│",
BorderPart.ColumnRight => "│",
BorderPart.CellRight => "│",
BorderPart.FooterBottomLeft => "╰",
BorderPart.FooterBottom => "─",
BorderPart.FooterBottomSeparator => "┴",

View File

@ -25,7 +25,7 @@ namespace Spectre.Console.Composition
BorderPart.HeaderBottomRight => "┤",
BorderPart.CellLeft => "│",
BorderPart.CellSeparator => "│",
BorderPart.ColumnRight => "│",
BorderPart.CellRight => "│",
BorderPart.FooterBottomLeft => "└",
BorderPart.FooterBottom => "─",
BorderPart.FooterBottomSeparator => "┴",

View File

@ -13,6 +13,7 @@ namespace Spectre.Console
private readonly IRenderable _child;
private readonly bool _fit;
private readonly Justify _content;
private readonly Border _border;
/// <summary>
/// Initializes a new instance of the <see cref="Panel"/> class.
@ -20,11 +21,17 @@ namespace Spectre.Console
/// <param name="child">The child.</param>
/// <param name="fit">Whether or not to fit the panel to it's parent.</param>
/// <param name="content">The justification of the panel content.</param>
public Panel(IRenderable child, bool fit = false, Justify content = Justify.Left)
/// <param name="border">The border to use.</param>
public Panel(
IRenderable child,
bool fit = false,
Justify content = Justify.Left,
BorderKind border = BorderKind.Square)
{
_child = child;
_child = child ?? throw new System.ArgumentNullException(nameof(child));
_fit = fit;
_content = content;
_border = Border.GetBorder(border);
}
/// <inheritdoc/>
@ -46,9 +53,9 @@ namespace Spectre.Console
var result = new List<Segment>();
var panelWidth = childWidth + 2;
result.Add(new Segment("┌"));
result.Add(new Segment(new string('─', panelWidth)));
result.Add(new Segment("┐"));
result.Add(new Segment(_border.GetPart(BorderPart.HeaderTopLeft)));
result.Add(new Segment(_border.GetPart(BorderPart.HeaderTop, panelWidth)));
result.Add(new Segment(_border.GetPart(BorderPart.HeaderTopRight)));
result.Add(new Segment("\n"));
// Render the child.
@ -58,13 +65,15 @@ namespace Spectre.Console
var lines = Segment.SplitLines(childSegments, childWidth);
foreach (var line in lines)
{
result.Add(new Segment("│ "));
result.Add(new Segment(_border.GetPart(BorderPart.CellLeft)));
result.Add(new Segment(" ")); // Left padding
var content = new List<Segment>();
var length = line.Sum(segment => segment.CellLength(encoding));
if (length < childWidth)
{
// Justify right side
if (_content == Justify.Right)
{
var diff = childWidth - length;
@ -82,6 +91,7 @@ namespace Spectre.Console
content.Add(segment.StripLineEndings());
}
// Justify left side
if (length < childWidth)
{
if (_content == Justify.Left)
@ -104,13 +114,14 @@ namespace Spectre.Console
result.AddRange(content);
result.Add(new Segment(" │"));
result.Add(new Segment(" "));
result.Add(new Segment(_border.GetPart(BorderPart.CellRight)));
result.Add(new Segment("\n"));
}
result.Add(new Segment("└"));
result.Add(new Segment(new string('─', panelWidth)));
result.Add(new Segment("┘"));
result.Add(new Segment(_border.GetPart(BorderPart.FooterBottomLeft)));
result.Add(new Segment(_border.GetPart(BorderPart.FooterBottom, panelWidth)));
result.Add(new Segment(_border.GetPart(BorderPart.FooterBottomRight)));
result.Add(new Segment("\n"));
return result;

View File

@ -224,7 +224,7 @@ namespace Spectre.Console
if (lastCell && showBorder)
{
// Add right column edge
result.Add(new Segment(_border.GetPart(BorderPart.ColumnRight)));
result.Add(new Segment(_border.GetPart(BorderPart.CellRight)));
}
else if (showBorder || (hideBorder && !lastCell))
{