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 // Nest some panels and text
AnsiConsole.Foreground = Color.Maroon; AnsiConsole.Foreground = Color.Maroon;
AnsiConsole.Render(new Panel(new Panel(new Panel(new Panel( AnsiConsole.Render(
Text.New( new Panel(
"[underline]I[/] heard [underline on blue]you[/] like 📦\n\n\n\n" + new Panel(
"So I put a 📦 in a 📦\nin a 📦 in a 📦\n\n" + new Panel(
"😅", new Panel(
foreground: Color.White), content: Justify.Center))))); 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 // Reset colors
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -224,7 +224,7 @@ namespace Spectre.Console
if (lastCell && showBorder) if (lastCell && showBorder)
{ {
// Add right column edge // 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)) else if (showBorder || (hideBorder && !lastCell))
{ {