mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add support for markup text in panel header
This commit is contained in:

committed by
Patrik Svensson

parent
be3350a411
commit
b1da5e7ba8
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user