mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
Add support for markup text in panel header
This commit is contained in:
parent
be3350a411
commit
b1da5e7ba8
@ -21,7 +21,7 @@ namespace BordersExample
|
|||||||
static IRenderable CreatePanel(string name, BoxBorder border)
|
static IRenderable CreatePanel(string name, BoxBorder border)
|
||||||
{
|
{
|
||||||
return new Panel($"This is a panel with\nthe [yellow]{name}[/] 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)
|
.Border(border)
|
||||||
.BorderStyle(Style.Parse("grey"));
|
.BorderStyle(Style.Parse("grey"));
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ namespace BordersExample
|
|||||||
table.AddRow("Cell", "Cell");
|
table.AddRow("Cell", "Cell");
|
||||||
|
|
||||||
return new Panel(table)
|
return new Panel(table)
|
||||||
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
|
.Header($" [blue]{name}[/] ", Justify.Center)
|
||||||
.NoBorder();
|
.NoBorder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
|
|
||||||
namespace Cursor
|
namespace Cursor
|
||||||
|
@ -20,16 +20,14 @@ namespace PanelExample
|
|||||||
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
|
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
|
||||||
.Expand()
|
.Expand()
|
||||||
.SquareBorder()
|
.SquareBorder()
|
||||||
.Header("Left")
|
.Header("[red]Left[/]"));
|
||||||
.HeaderStyle("red"));
|
|
||||||
|
|
||||||
// Centered ASCII panel with text
|
// Centered ASCII panel with text
|
||||||
AnsiConsole.Render(
|
AnsiConsole.Render(
|
||||||
new Panel(new Text("Centered\nCenter").Centered())
|
new Panel(new Text("Centered\nCenter").Centered())
|
||||||
.Expand()
|
.Expand()
|
||||||
.AsciiBorder()
|
.AsciiBorder()
|
||||||
.Header("Center")
|
.Header("[green]Center[/]")
|
||||||
.HeaderStyle("green")
|
|
||||||
.HeaderAlignment(Justify.Center));
|
.HeaderAlignment(Justify.Center));
|
||||||
|
|
||||||
// Right adjusted, rounded panel with text
|
// Right adjusted, rounded panel with text
|
||||||
@ -37,8 +35,7 @@ namespace PanelExample
|
|||||||
new Panel(new Text("Right adjusted\nRight").RightAligned())
|
new Panel(new Text("Right adjusted\nRight").RightAligned())
|
||||||
.Expand()
|
.Expand()
|
||||||
.RoundedBorder()
|
.RoundedBorder()
|
||||||
.Header("Right")
|
.Header("[blue]Right[/]")
|
||||||
.HeaderStyle("blue")
|
|
||||||
.HeaderAlignment(Justify.Right));
|
.HeaderAlignment(Justify.Right));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,18 +10,21 @@ namespace EmojiExample
|
|||||||
WrapInPanel(
|
WrapInPanel(
|
||||||
new Rule()
|
new Rule()
|
||||||
.RuleStyle(Style.Parse("yellow"))
|
.RuleStyle(Style.Parse("yellow"))
|
||||||
|
.AsciiBorder()
|
||||||
.LeftAligned());
|
.LeftAligned());
|
||||||
|
|
||||||
// Left aligned title
|
// Left aligned title
|
||||||
WrapInPanel(
|
WrapInPanel(
|
||||||
new Rule("[blue]Left aligned[/]")
|
new Rule("[blue]Left aligned[/]")
|
||||||
.RuleStyle(Style.Parse("red"))
|
.RuleStyle(Style.Parse("red"))
|
||||||
|
.DoubleBorder()
|
||||||
.LeftAligned());
|
.LeftAligned());
|
||||||
|
|
||||||
// Centered title
|
// Centered title
|
||||||
WrapInPanel(
|
WrapInPanel(
|
||||||
new Rule("[green]Centered[/]")
|
new Rule("[green]Centered[/]")
|
||||||
.RuleStyle(Style.Parse("green"))
|
.RuleStyle(Style.Parse("green"))
|
||||||
|
.HeavyBorder()
|
||||||
.Centered());
|
.Centered());
|
||||||
|
|
||||||
// Right aligned title
|
// Right aligned title
|
||||||
|
@ -316,14 +316,14 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
var panel = new Panel(grid)
|
var panel = new Panel(grid)
|
||||||
.Expand().RoundedBorder()
|
.Expand().RoundedBorder()
|
||||||
.BorderStyle(new Style().Foreground(Color.Grey))
|
.BorderStyle(new Style().Foreground(Color.Grey))
|
||||||
.Header("Short paths ", new Style().Foreground(Color.Grey));
|
.Header("[grey]Short paths[/]");
|
||||||
|
|
||||||
// When
|
// When
|
||||||
console.Render(panel);
|
console.Render(panel);
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
console.Lines.Count.ShouldBe(4);
|
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[1].ShouldBe("│ at System.Runtime.CompilerServices.TaskAwaiter. │");
|
||||||
console.Lines[2].ShouldBe("│ HandleNonSuccessAndDebuggerNotification(Task task) │");
|
console.Lines[2].ShouldBe("│ HandleNonSuccessAndDebuggerNotification(Task task) │");
|
||||||
console.Lines[3].ShouldBe("╰──────────────────────────────────────────────────────────────────────────────────╯");
|
console.Lines[3].ShouldBe("╰──────────────────────────────────────────────────────────────────────────────────╯");
|
||||||
|
@ -19,6 +19,34 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
console.Lines[0].ShouldBe("────────────────────────────────────────");
|
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]
|
[Fact]
|
||||||
public void Should_Render_Default_Rule_With_Title_Centered_By_Default()
|
public void Should_Render_Default_Rule_With_Title_Centered_By_Default()
|
||||||
{
|
{
|
||||||
|
@ -30,10 +30,8 @@ namespace Spectre.Console
|
|||||||
throw new ArgumentNullException(nameof(text));
|
throw new ArgumentNullException(nameof(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
style ??= panel.Header?.Style;
|
|
||||||
alignment ??= panel.Header?.Alignment;
|
alignment ??= panel.Header?.Alignment;
|
||||||
|
return SetHeader(panel, new PanelHeader(text, alignment));
|
||||||
return SetHeader(panel, new PanelHeader(text, style, alignment));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -54,5 +52,18 @@ namespace Spectre.Console
|
|||||||
panel.Header = header;
|
panel.Header = header;
|
||||||
return panel;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,9 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="panel">The panel.</param>
|
/// <param name="panel">The panel.</param>
|
||||||
/// <param name="text">The header text.</param>
|
/// <param name="text">The header text.</param>
|
||||||
/// <param name="style">The header style.</param>
|
|
||||||
/// <param name="alignment">The header alignment.</param>
|
/// <param name="alignment">The header alignment.</param>
|
||||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
/// <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)
|
if (panel is null)
|
||||||
{
|
{
|
||||||
@ -27,42 +26,8 @@ namespace Spectre.Console
|
|||||||
throw new ArgumentNullException(nameof(text));
|
throw new ArgumentNullException(nameof(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
style ??= panel.Header?.Style;
|
|
||||||
alignment ??= panel.Header?.Alignment;
|
alignment ??= panel.Header?.Alignment;
|
||||||
|
return Header(panel, new PanelHeader(text, 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -86,7 +51,7 @@ namespace Spectre.Console
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Create header
|
// Create header
|
||||||
Header(panel, string.Empty, null, alignment);
|
Header(panel, string.Empty, alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
return panel;
|
return panel;
|
||||||
|
@ -3,7 +3,7 @@ namespace Spectre.Console
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents something that has a box border.
|
/// Represents something that has a box border.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IHasBoxBorder : IHasBorder
|
public interface IHasBoxBorder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the box.
|
/// Gets or sets the box.
|
||||||
|
@ -9,7 +9,7 @@ namespace Spectre.Console
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A renderable panel.
|
/// A renderable panel.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Panel : Renderable, IHasBoxBorder, IExpandable, IPaddable
|
public sealed class Panel : Renderable, IHasBoxBorder, IHasBorder, IExpandable, IPaddable
|
||||||
{
|
{
|
||||||
private const int EdgeWidth = 2;
|
private const int EdgeWidth = 2;
|
||||||
|
|
||||||
@ -123,62 +123,35 @@ namespace Spectre.Console
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Panel bottom
|
// 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.BottomLeft), borderStyle));
|
||||||
result.Add(new Segment(border.GetPart(BoxBorderPart.Bottom).Repeat(panelWidth - EdgeWidth), borderStyle));
|
result.Add(new Segment(border.GetPart(BoxBorderPart.Bottom).Repeat(panelWidth - EdgeWidth), borderStyle));
|
||||||
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomRight), borderStyle));
|
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomRight), borderStyle));
|
||||||
result.Add(Segment.LineBreak);
|
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));
|
var rule = new Rule
|
||||||
|
|
||||||
if (Header != null)
|
|
||||||
{
|
{
|
||||||
var leftSpacing = 0;
|
Style = borderStyle,
|
||||||
var rightSpacing = 0;
|
Border = border,
|
||||||
|
TitlePadding = 1,
|
||||||
|
TitleSpacing = 0,
|
||||||
|
Title = Header?.Text,
|
||||||
|
Alignment = Header?.Alignment ?? Justify.Left,
|
||||||
|
};
|
||||||
|
|
||||||
var headerWidth = panelWidth - (EdgeWidth * 2);
|
// Top left border
|
||||||
var header = Segment.TruncateWithEllipsis(Header.Text, Header.Style ?? borderStyle, context, headerWidth);
|
result.Add(new Segment(border.GetPart(BoxBorderPart.TopLeft), borderStyle));
|
||||||
|
|
||||||
var excessWidth = headerWidth - header.CellCount(context);
|
// Top border (and header text if specified)
|
||||||
if (excessWidth > 0)
|
result.AddRange(((IRenderable)rule).Render(context, panelWidth - 2).Where(x => !x.IsLineBreak));
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(leftSpacing + 1), borderStyle));
|
// Top right border
|
||||||
segments.Add(header);
|
result.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
|
||||||
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(rightSpacing + 1), borderStyle));
|
result.Add(Segment.LineBreak);
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
namespace Spectre.Console
|
namespace Spectre.Console
|
||||||
{
|
{
|
||||||
@ -12,11 +13,6 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Text { get; }
|
public string Text { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the panel header style.
|
|
||||||
/// </summary>
|
|
||||||
public Style? Style { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the panel header alignment.
|
/// Gets or sets the panel header alignment.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -26,12 +22,10 @@ namespace Spectre.Console
|
|||||||
/// Initializes a new instance of the <see cref="PanelHeader"/> class.
|
/// Initializes a new instance of the <see cref="PanelHeader"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The panel header text.</param>
|
/// <param name="text">The panel header text.</param>
|
||||||
/// <param name="style">The panel header style.</param>
|
|
||||||
/// <param name="alignment">The panel header alignment.</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));
|
Text = text ?? throw new ArgumentNullException(nameof(text));
|
||||||
Style = style;
|
|
||||||
Alignment = alignment;
|
Alignment = alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,9 +34,10 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="style">The panel header style.</param>
|
/// <param name="style">The panel header style.</param>
|
||||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||||
|
[Obsolete("Use markup instead.")]
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
public PanelHeader SetStyle(Style? style)
|
public PanelHeader SetStyle(Style? style)
|
||||||
{
|
{
|
||||||
Style = style ?? Style.Plain;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,14 +46,10 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="style">The panel header style.</param>
|
/// <param name="style">The panel header style.</param>
|
||||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||||
|
[Obsolete("Use markup instead.")]
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
public PanelHeader SetStyle(string style)
|
public PanelHeader SetStyle(string style)
|
||||||
{
|
{
|
||||||
if (style is null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(style));
|
|
||||||
}
|
|
||||||
|
|
||||||
Style = Style.Parse(style);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ namespace Spectre.Console
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A renderable horizontal rule.
|
/// A renderable horizontal rule.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Rule : Renderable, IAlignable
|
public sealed class Rule : Renderable, IAlignable, IHasBoxBorder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the rule title markup text.
|
/// Gets or sets the rule title markup text.
|
||||||
@ -26,6 +26,12 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Justify? Alignment { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="Rule"/> class.
|
/// Initializes a new instance of the <see cref="Rule"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -45,21 +51,23 @@ namespace Spectre.Console
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
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.
|
// Get the title and make sure it fits.
|
||||||
var title = GetTitleSegments(context, Title, maxWidth - 6);
|
var title = GetTitleSegments(context, Title, maxWidth - extraLength);
|
||||||
if (Segment.CellCount(context, title) > maxWidth - 6)
|
if (Segment.CellCount(context, title) > maxWidth - extraLength)
|
||||||
{
|
{
|
||||||
// Truncate the title
|
// Truncate the title
|
||||||
title = Segment.TruncateWithEllipsis(title, context, maxWidth - 6);
|
title = Segment.TruncateWithEllipsis(title, context, maxWidth - extraLength);
|
||||||
if (!title.Any())
|
if (!title.Any())
|
||||||
{
|
{
|
||||||
// We couldn't fit the title at all.
|
// We couldn't fit the title at all.
|
||||||
return GetLineWithoutTitle(maxWidth);
|
return GetLineWithoutTitle(context, maxWidth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,9 +82,11 @@ namespace Spectre.Console
|
|||||||
return segments;
|
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[]
|
return new[]
|
||||||
{
|
{
|
||||||
new Segment(text, Style ?? Style.Plain),
|
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 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)
|
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 rightLength = width - titleLength - left.CellCount(context) - TitleSpacing;
|
||||||
var right = new Segment(" " + new string('─', rightLength), Style ?? Style.Plain);
|
var right = new Segment(new string(' ', TitleSpacing) + borderPart.Repeat(rightLength), Style ?? Style.Plain);
|
||||||
|
|
||||||
return (left, right);
|
return (left, right);
|
||||||
}
|
}
|
||||||
else if (alignment == Justify.Center)
|
else if (alignment == Justify.Center)
|
||||||
{
|
{
|
||||||
var leftLength = ((maxWidth - titleLength) / 2) - 1;
|
var leftLength = ((width - titleLength) / 2) - TitleSpacing;
|
||||||
var left = new Segment(new string('─', leftLength) + " ", Style ?? Style.Plain);
|
var left = new Segment(borderPart.Repeat(leftLength) + new string(' ', TitleSpacing), Style ?? Style.Plain);
|
||||||
|
|
||||||
var rightLength = maxWidth - titleLength - left.CellCount(context) - 1;
|
var rightLength = width - titleLength - left.CellCount(context) - TitleSpacing;
|
||||||
var right = new Segment(" " + new string('─', rightLength), Style ?? Style.Plain);
|
var right = new Segment(new string(' ', TitleSpacing) + borderPart.Repeat(rightLength), Style ?? Style.Plain);
|
||||||
|
|
||||||
return (left, right);
|
return (left, right);
|
||||||
}
|
}
|
||||||
else if (alignment == Justify.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 leftLength = width - titleLength - right.CellCount(context) - TitleSpacing;
|
||||||
var left = new Segment(new string('─', leftLength) + " ", Style ?? Style.Plain);
|
var left = new Segment(borderPart.Repeat(leftLength) + new string(' ', TitleSpacing), Style ?? Style.Plain);
|
||||||
|
|
||||||
return (left, right);
|
return (left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new NotSupportedException("Unsupported alignment.");
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user