Add support for markup text in panel header

This commit is contained in:
Patrik Svensson 2020-11-07 03:17:31 +01:00 committed by Patrik Svensson
parent be3350a411
commit b1da5e7ba8
12 changed files with 121 additions and 142 deletions

View File

@ -21,7 +21,7 @@ namespace BordersExample
static IRenderable CreatePanel(string name, BoxBorder border)
{
return new Panel($"This is a panel with\nthe [yellow]{name}[/] border.")
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.Header($" [blue]{name}[/] ", Justify.Center)
.Border(border)
.BorderStyle(Style.Parse("grey"));
}
@ -53,7 +53,7 @@ namespace BordersExample
table.AddRow("Cell", "Cell");
return new Panel(table)
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.Header($" [blue]{name}[/] ", Justify.Center)
.NoBorder();
}

View File

@ -1,4 +1,3 @@
using System;
using Spectre.Console;
namespace Cursor

View File

@ -20,16 +20,14 @@ namespace PanelExample
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
.Expand()
.SquareBorder()
.Header("Left")
.HeaderStyle("red"));
.Header("[red]Left[/]"));
// Centered ASCII panel with text
AnsiConsole.Render(
new Panel(new Text("Centered\nCenter").Centered())
.Expand()
.AsciiBorder()
.Header("Center")
.HeaderStyle("green")
.Header("[green]Center[/]")
.HeaderAlignment(Justify.Center));
// Right adjusted, rounded panel with text
@ -37,8 +35,7 @@ namespace PanelExample
new Panel(new Text("Right adjusted\nRight").RightAligned())
.Expand()
.RoundedBorder()
.Header("Right")
.HeaderStyle("blue")
.Header("[blue]Right[/]")
.HeaderAlignment(Justify.Right));
}
}

View File

@ -10,18 +10,21 @@ namespace EmojiExample
WrapInPanel(
new Rule()
.RuleStyle(Style.Parse("yellow"))
.AsciiBorder()
.LeftAligned());
// Left aligned title
WrapInPanel(
new Rule("[blue]Left aligned[/]")
.RuleStyle(Style.Parse("red"))
.DoubleBorder()
.LeftAligned());
// Centered title
WrapInPanel(
new Rule("[green]Centered[/]")
.RuleStyle(Style.Parse("green"))
.HeavyBorder()
.Centered());
// Right aligned title

View File

@ -316,14 +316,14 @@ namespace Spectre.Console.Tests.Unit
var panel = new Panel(grid)
.Expand().RoundedBorder()
.BorderStyle(new Style().Foreground(Color.Grey))
.Header("Short paths ", new Style().Foreground(Color.Grey));
.Header("[grey]Short paths[/]");
// When
console.Render(panel);
// Then
console.Lines.Count.ShouldBe(4);
console.Lines[0].ShouldBe("╭─Short paths ─────────────────────────────────────────────────────────────────────╮");
console.Lines[0].ShouldBe("╭─Short paths─────────────────────────────────────────────────────────────────────╮");
console.Lines[1].ShouldBe("│ at System.Runtime.CompilerServices.TaskAwaiter. │");
console.Lines[2].ShouldBe("│ HandleNonSuccessAndDebuggerNotification(Task task) │");
console.Lines[3].ShouldBe("╰──────────────────────────────────────────────────────────────────────────────────╯");

View File

@ -19,6 +19,34 @@ namespace Spectre.Console.Tests.Unit
console.Lines[0].ShouldBe("────────────────────────────────────────");
}
[Fact]
public void Should_Render_Default_Rule_With_Specified_Box()
{
// Given
var console = new PlainConsole(width: 40);
// When
console.Render(new Rule().DoubleBorder());
// Then
console.Lines.Count.ShouldBe(1);
console.Lines[0].ShouldBe("════════════════════════════════════════");
}
[Fact]
public void Should_Render_With_Specified_Box()
{
// Given
var console = new PlainConsole(width: 40);
// When
console.Render(new Rule("Hello World").DoubleBorder());
// Then
console.Lines.Count.ShouldBe(1);
console.Lines[0].ShouldBe("═════════════ Hello World ══════════════");
}
[Fact]
public void Should_Render_Default_Rule_With_Title_Centered_By_Default()
{

View File

@ -30,10 +30,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text));
}
style ??= panel.Header?.Style;
alignment ??= panel.Header?.Alignment;
return SetHeader(panel, new PanelHeader(text, style, alignment));
return SetHeader(panel, new PanelHeader(text, alignment));
}
/// <summary>
@ -54,5 +52,18 @@ namespace Spectre.Console
panel.Header = header;
return panel;
}
/// <summary>
/// Sets the panel header style.
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="style">The header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup in header instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Panel HeaderStyle(this Panel panel, Style style)
{
return panel;
}
}
}

View File

@ -12,10 +12,9 @@ namespace Spectre.Console
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="text">The header text.</param>
/// <param name="style">The header style.</param>
/// <param name="alignment">The header alignment.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Panel Header(this Panel panel, string text, Style? style = null, Justify? alignment = null)
public static Panel Header(this Panel panel, string text, Justify? alignment = null)
{
if (panel is null)
{
@ -27,42 +26,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text));
}
style ??= panel.Header?.Style;
alignment ??= panel.Header?.Alignment;
return Header(panel, new PanelHeader(text, style, alignment));
}
/// <summary>
/// Sets the panel header style.
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="style">The header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Panel HeaderStyle(this Panel panel, Style style)
{
if (panel is null)
{
throw new ArgumentNullException(nameof(panel));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
if (panel.Header != null)
{
// Update existing style
panel.Header.Style = style;
}
else
{
// Create header
Header(panel, string.Empty, style, null);
}
return panel;
return Header(panel, new PanelHeader(text, alignment));
}
/// <summary>
@ -86,7 +51,7 @@ namespace Spectre.Console
else
{
// Create header
Header(panel, string.Empty, null, alignment);
Header(panel, string.Empty, alignment);
}
return panel;

View File

@ -3,7 +3,7 @@ namespace Spectre.Console
/// <summary>
/// Represents something that has a box border.
/// </summary>
public interface IHasBoxBorder : IHasBorder
public interface IHasBoxBorder
{
/// <summary>
/// Gets or sets the box.

View File

@ -9,7 +9,7 @@ namespace Spectre.Console
/// <summary>
/// A renderable panel.
/// </summary>
public sealed class Panel : Renderable, IHasBoxBorder, IExpandable, IPaddable
public sealed class Panel : Renderable, IHasBoxBorder, IHasBorder, IExpandable, IPaddable
{
private const int EdgeWidth = 2;
@ -123,62 +123,35 @@ namespace Spectre.Console
}
// Panel bottom
AddBottomBorder(result, border, borderStyle, panelWidth);
return result;
}
private static void AddBottomBorder(List<Segment> result, BoxBorder border, Style borderStyle, int panelWidth)
{
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomLeft), borderStyle));
result.Add(new Segment(border.GetPart(BoxBorderPart.Bottom).Repeat(panelWidth - EdgeWidth), borderStyle));
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomRight), borderStyle));
result.Add(Segment.LineBreak);
return result;
}
private void AddTopBorder(List<Segment> segments, RenderContext context, BoxBorder border, Style borderStyle, int panelWidth)
private void AddTopBorder(List<Segment> result, RenderContext context, BoxBorder border, Style borderStyle, int panelWidth)
{
segments.Add(new Segment(border.GetPart(BoxBorderPart.TopLeft), borderStyle));
if (Header != null)
var rule = new Rule
{
var leftSpacing = 0;
var rightSpacing = 0;
Style = borderStyle,
Border = border,
TitlePadding = 1,
TitleSpacing = 0,
Title = Header?.Text,
Alignment = Header?.Alignment ?? Justify.Left,
};
var headerWidth = panelWidth - (EdgeWidth * 2);
var header = Segment.TruncateWithEllipsis(Header.Text, Header.Style ?? borderStyle, context, headerWidth);
// Top left border
result.Add(new Segment(border.GetPart(BoxBorderPart.TopLeft), borderStyle));
var excessWidth = headerWidth - header.CellCount(context);
if (excessWidth > 0)
{
switch (Header.Alignment ?? Justify.Left)
{
case Justify.Left:
leftSpacing = 0;
rightSpacing = excessWidth;
break;
case Justify.Right:
leftSpacing = excessWidth;
rightSpacing = 0;
break;
case Justify.Center:
leftSpacing = excessWidth / 2;
rightSpacing = (excessWidth / 2) + (excessWidth % 2);
break;
}
}
// Top border (and header text if specified)
result.AddRange(((IRenderable)rule).Render(context, panelWidth - 2).Where(x => !x.IsLineBreak));
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(leftSpacing + 1), borderStyle));
segments.Add(header);
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(rightSpacing + 1), borderStyle));
}
else
{
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(panelWidth - EdgeWidth), borderStyle));
}
segments.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
segments.Add(Segment.LineBreak);
// Top right border
result.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
result.Add(Segment.LineBreak);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
namespace Spectre.Console
{
@ -12,11 +13,6 @@ namespace Spectre.Console
/// </summary>
public string Text { get; }
/// <summary>
/// Gets or sets the panel header style.
/// </summary>
public Style? Style { get; set; }
/// <summary>
/// Gets or sets the panel header alignment.
/// </summary>
@ -26,12 +22,10 @@ namespace Spectre.Console
/// Initializes a new instance of the <see cref="PanelHeader"/> class.
/// </summary>
/// <param name="text">The panel header text.</param>
/// <param name="style">The panel header style.</param>
/// <param name="alignment">The panel header alignment.</param>
public PanelHeader(string text, Style? style = null, Justify? alignment = null)
public PanelHeader(string text, Justify? alignment = null)
{
Text = text ?? throw new ArgumentNullException(nameof(text));
Style = style;
Alignment = alignment;
}
@ -40,9 +34,10 @@ namespace Spectre.Console
/// </summary>
/// <param name="style">The panel header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public PanelHeader SetStyle(Style? style)
{
Style = style ?? Style.Plain;
return this;
}
@ -51,14 +46,10 @@ namespace Spectre.Console
/// </summary>
/// <param name="style">The panel header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public PanelHeader SetStyle(string style)
{
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
Style = Style.Parse(style);
return this;
}

View File

@ -9,7 +9,7 @@ namespace Spectre.Console
/// <summary>
/// A renderable horizontal rule.
/// </summary>
public sealed class Rule : Renderable, IAlignable
public sealed class Rule : Renderable, IAlignable, IHasBoxBorder
{
/// <summary>
/// Gets or sets the rule title markup text.
@ -26,6 +26,12 @@ namespace Spectre.Console
/// </summary>
public Justify? Alignment { get; set; }
/// <inheritdoc/>
public BoxBorder Border { get; set; } = BoxBorder.Square;
internal int TitlePadding { get; set; } = 2;
internal int TitleSpacing { get; set; } = 1;
/// <summary>
/// Initializes a new instance of the <see cref="Rule"/> class.
/// </summary>
@ -45,21 +51,23 @@ namespace Spectre.Console
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
if (Title == null || maxWidth <= 6)
var extraLength = (2 * TitlePadding) + (2 * TitleSpacing);
if (Title == null || maxWidth <= extraLength)
{
return GetLineWithoutTitle(maxWidth);
return GetLineWithoutTitle(context, maxWidth);
}
// Get the title and make sure it fits.
var title = GetTitleSegments(context, Title, maxWidth - 6);
if (Segment.CellCount(context, title) > maxWidth - 6)
var title = GetTitleSegments(context, Title, maxWidth - extraLength);
if (Segment.CellCount(context, title) > maxWidth - extraLength)
{
// Truncate the title
title = Segment.TruncateWithEllipsis(title, context, maxWidth - 6);
title = Segment.TruncateWithEllipsis(title, context, maxWidth - extraLength);
if (!title.Any())
{
// We couldn't fit the title at all.
return GetLineWithoutTitle(maxWidth);
return GetLineWithoutTitle(context, maxWidth);
}
}
@ -74,9 +82,11 @@ namespace Spectre.Console
return segments;
}
private IEnumerable<Segment> GetLineWithoutTitle(int maxWidth)
private IEnumerable<Segment> GetLineWithoutTitle(RenderContext context, int maxWidth)
{
var text = new string('─', maxWidth);
var border = Border.GetSafeBorder(context.LegacyConsole || !context.Unicode);
var text = border.GetPart(BoxBorderPart.Top).Repeat(maxWidth);
return new[]
{
new Segment(text, Style ?? Style.Plain),
@ -84,49 +94,51 @@ namespace Spectre.Console
};
}
private (Segment Left, Segment Right) GetLineSegments(RenderContext context, int maxWidth, IEnumerable<Segment> title)
private IEnumerable<Segment> GetTitleSegments(RenderContext context, string title, int width)
{
var alignment = Alignment ?? Justify.Center;
title = title.NormalizeLineEndings().Replace("\n", " ").Trim();
var markup = new Markup(title, Style);
return ((IRenderable)markup).Render(context.WithSingleLine(), width);
}
private (Segment Left, Segment Right) GetLineSegments(RenderContext context, int width, IEnumerable<Segment> title)
{
var titleLength = Segment.CellCount(context, title);
var border = Border.GetSafeBorder(context.LegacyConsole || !context.Unicode);
var borderPart = border.GetPart(BoxBorderPart.Top);
var alignment = Alignment ?? Justify.Center;
if (alignment == Justify.Left)
{
var left = new Segment(new string('─', 2) + " ", Style ?? Style.Plain);
var left = new Segment(borderPart.Repeat(TitlePadding) + new string(' ', TitleSpacing), Style ?? Style.Plain);
var rightLength = maxWidth - titleLength - left.CellCount(context) - 1;
var right = new Segment(" " + new string('─', rightLength), Style ?? Style.Plain);
var rightLength = width - titleLength - left.CellCount(context) - TitleSpacing;
var right = new Segment(new string(' ', TitleSpacing) + borderPart.Repeat(rightLength), Style ?? Style.Plain);
return (left, right);
}
else if (alignment == Justify.Center)
{
var leftLength = ((maxWidth - titleLength) / 2) - 1;
var left = new Segment(new string('─', leftLength) + " ", Style ?? Style.Plain);
var leftLength = ((width - titleLength) / 2) - TitleSpacing;
var left = new Segment(borderPart.Repeat(leftLength) + new string(' ', TitleSpacing), Style ?? Style.Plain);
var rightLength = maxWidth - titleLength - left.CellCount(context) - 1;
var right = new Segment(" " + new string('─', rightLength), Style ?? Style.Plain);
var rightLength = width - titleLength - left.CellCount(context) - TitleSpacing;
var right = new Segment(new string(' ', TitleSpacing) + borderPart.Repeat(rightLength), Style ?? Style.Plain);
return (left, right);
}
else if (alignment == Justify.Right)
{
var right = new Segment(" " + new string('─', 2), Style ?? Style.Plain);
var right = new Segment(new string(' ', TitleSpacing) + borderPart.Repeat(TitlePadding), Style ?? Style.Plain);
var leftLength = maxWidth - titleLength - right.CellCount(context) - 1;
var left = new Segment(new string('─', leftLength) + " ", Style ?? Style.Plain);
var leftLength = width - titleLength - right.CellCount(context) - TitleSpacing;
var left = new Segment(borderPart.Repeat(leftLength) + new string(' ', TitleSpacing), Style ?? Style.Plain);
return (left, right);
}
throw new NotSupportedException("Unsupported alignment.");
}
private IEnumerable<Segment> GetTitleSegments(RenderContext context, string title, int width)
{
title = title.NormalizeLineEndings().Replace("\n", " ").Trim();
var markup = new Markup(title, Style);
return ((IRenderable)markup).Render(context.WithSingleLine(), width - 6);
}
}
}