Add rule widget

Adds a new rule widget.
Also fixes some bugs I encountered while testing
some unrelated things in an extremely small console.
This commit is contained in:
Patrik Svensson
2020-10-20 01:07:58 +02:00
committed by Patrik Svensson
parent 1410cba6c5
commit 5a1b8a1710
16 changed files with 610 additions and 80 deletions

View File

@ -66,10 +66,15 @@ namespace Spectre.Console
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);
if (columnCount == 0)
{
// Temporary work around for extremely small consoles
return new Measurement(maxWidth, maxWidth);
}
var rows = _items.Count / columnCount;
var rows = _items.Count / Math.Max(columnCount, 1);
var greatestWidth = 0;
for (var row = 0; row < rows; row += columnCount)
for (var row = 0; row < rows; row += Math.Max(1, columnCount))
{
var widths = itemWidths.Skip(row * columnCount).Take(columnCount).ToList();
var totalWidth = widths.Sum() + (maxPadding * (widths.Count - 1));
@ -89,6 +94,11 @@ namespace Spectre.Console
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);
if (columnCount == 0)
{
// Temporary work around for extremely small consoles
columnCount = 1;
}
var table = new Table();
table.NoBorder();

View File

@ -138,7 +138,9 @@ namespace Spectre.Console
return Array.Empty<Segment>();
}
var lines = SplitLines(context, maxWidth);
var lines = context.SingleLine
? new List<SegmentLine>(_lines)
: SplitLines(context, maxWidth);
// Justify lines
var justification = context.Justification ?? Alignment ?? Justify.Left;
@ -170,6 +172,11 @@ namespace Spectre.Console
}
}
if (context.SingleLine)
{
return lines.First().Where(segment => !segment.IsLineBreak);
}
return new SegmentLineEnumerator(lines);
}

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// A renderable horizontal rule.
/// </summary>
public sealed class Rule : Renderable, IAlignable
{
/// <summary>
/// Gets or sets the rule title markup text.
/// </summary>
public string? Title { get; set; }
/// <summary>
/// Gets or sets the rule style.
/// </summary>
public Style? Style { get; set; }
/// <summary>
/// Gets or sets the rule's title alignment.
/// </summary>
public Justify? Alignment { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Rule"/> class.
/// </summary>
public Rule()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Rule"/> class.
/// </summary>
/// <param name="title">The rule title markup text.</param>
public Rule(string title)
{
Title = title ?? throw new ArgumentNullException(nameof(title));
}
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
if (Title == null || maxWidth <= 6)
{
return GetLineWithoutTitle(maxWidth);
}
// Get the title and make sure it fits.
var title = GetTitleSegments(context, Title, maxWidth - 6);
if (Segment.CellLength(context, title) > maxWidth - 6)
{
// Truncate the title
title = Segment.TruncateWithEllipsis(title, context, maxWidth - 6);
if (!title.Any())
{
// We couldn't fit the title at all.
return GetLineWithoutTitle(maxWidth);
}
}
var (left, right) = GetLineSegments(context, maxWidth, title);
var segments = new List<Segment>();
segments.Add(left);
segments.AddRange(title);
segments.Add(right);
segments.Add(Segment.LineBreak);
return segments;
}
private IEnumerable<Segment> GetLineWithoutTitle(int maxWidth)
{
var text = new string('─', maxWidth);
return new[]
{
new Segment(text, Style ?? Style.Plain),
Segment.LineBreak,
};
}
private (Segment Left, Segment Right) GetLineSegments(RenderContext context, int maxWidth, IEnumerable<Segment> title)
{
var alignment = Alignment ?? Justify.Center;
var titleLength = Segment.CellLength(context, title);
if (alignment == Justify.Left)
{
var left = new Segment(new string('─', 2) + " ", Style ?? Style.Plain);
var rightLength = maxWidth - titleLength - left.CellLength(context) - 1;
var right = new Segment(" " + new string('─', 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 rightLength = maxWidth - titleLength - left.CellLength(context) - 1;
var right = new Segment(" " + new string('─', rightLength), Style ?? Style.Plain);
return (left, right);
}
else if (alignment == Justify.Right)
{
var right = new Segment(" " + new string('─', 2), Style ?? Style.Plain);
var leftLength = maxWidth - titleLength - right.CellLength(context) - 1;
var left = new Segment(new string('─', leftLength) + " ", 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);
}
}
}