Clean up border code

Removed caching that really didn't do anything anymore.
This commit is contained in:
Patrik Svensson 2020-10-01 22:56:44 +02:00 committed by Patrik Svensson
parent a2f8652575
commit b1db8a9403
28 changed files with 44 additions and 156 deletions

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace Spectre.Console namespace Spectre.Console
@ -11,70 +7,16 @@ namespace Spectre.Console
/// </summary> /// </summary>
public abstract partial class BoxBorder public abstract partial class BoxBorder
{ {
private readonly Dictionary<BoxBorderPart, string> _lookup;
/// <summary> /// <summary>
/// Gets the safe border for this border or <c>null</c> if none exist. /// Gets the safe border for this border or <c>null</c> if none exist.
/// </summary> /// </summary>
public virtual BoxBorder? SafeBorder { get; } public virtual BoxBorder? SafeBorder { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BoxBorder"/> class. /// Gets the string representation of the specified border part.
/// </summary>
protected BoxBorder()
{
_lookup = Initialize();
}
private Dictionary<BoxBorderPart, string> Initialize()
{
var lookup = new Dictionary<BoxBorderPart, string>();
foreach (BoxBorderPart? part in Enum.GetValues(typeof(BoxBorderPart)))
{
if (part == null)
{
continue;
}
var text = GetBorderPart(part.Value);
if (text.Length > 1)
{
throw new InvalidOperationException("A box part cannot contain more than one character.");
}
lookup.Add(part.Value, GetBorderPart(part.Value));
}
return lookup;
}
/// <summary>
/// Gets the string representation of a specific border part.
/// </summary>
/// <param name="part">The part to get a string representation for.</param>
/// <param name="count">The number of repetitions.</param>
/// <returns>A string representation of the specified border part.</returns>
public string GetPart(BoxBorderPart part, int count)
{
// TODO: This need some optimization...
return string.Join(string.Empty, Enumerable.Repeat(GetBorderPart(part)[0], count));
}
/// <summary>
/// Gets the string representation of a specific border part.
/// </summary>
/// <param name="part">The part to get a string representation for.</param>
/// <returns>A string representation of the specified border part.</returns>
public string GetPart(BoxBorderPart part)
{
return _lookup[part].ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the character representing the specified border part.
/// </summary> /// </summary>
/// <param name="part">The part to get the character representation for.</param> /// <param name="part">The part to get the character representation for.</param>
/// <returns>A character representation of the specified border part.</returns> /// <returns>A character representation of the specified border part.</returns>
protected abstract string GetBorderPart(BoxBorderPart part); public abstract string GetPart(BoxBorderPart part);
} }
} }

View File

@ -81,7 +81,7 @@ namespace Spectre.Console.Internal
return result.ToArray(); return result.ToArray();
} }
public static string Multiply(this string text, int count) public static string Repeat(this string text, int count)
{ {
if (text is null) if (text is null)
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class AsciiBoxBorder : BoxBorder public sealed class AsciiBoxBorder : BoxBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class DoubleBoxBorder : BoxBorder public sealed class DoubleBoxBorder : BoxBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override BoxBorder? SafeBorder => BoxBorder.Square; public override BoxBorder? SafeBorder => BoxBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -6,7 +6,7 @@ namespace Spectre.Console.Rendering
public sealed class NoBoxBorder : BoxBorder public sealed class NoBoxBorder : BoxBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return " "; return " ";
} }

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override BoxBorder? SafeBorder => BoxBorder.Square; public override BoxBorder? SafeBorder => BoxBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class SquareBoxBorder : BoxBorder public sealed class SquareBoxBorder : BoxBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(BoxBorderPart part) public override string GetPart(BoxBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class Ascii2TableBorder : TableBorder public sealed class Ascii2TableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class AsciiDoubleHeadTableBorder : TableBorder public sealed class AsciiDoubleHeadTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class AsciiTableBorder : TableBorder public sealed class AsciiTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class DoubleEdgeTableBorder : TableBorder public sealed class DoubleEdgeTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class DoubleTableBorder : TableBorder public sealed class DoubleTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Square; public override TableBorder? SafeBorder => TableBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Square; public override TableBorder? SafeBorder => TableBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Square; public override TableBorder? SafeBorder => TableBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class HorizontalTableBorder : TableBorder public sealed class HorizontalTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public sealed class MarkdownTableBorder : TableBorder public sealed class MarkdownTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {
@ -57,39 +57,39 @@ namespace Spectre.Console.Rendering
if (padding.Left > 0) if (padding.Left > 0)
{ {
// Left padding // Left padding
builder.Append(" ".Multiply(padding.Left)); builder.Append(" ".Repeat(padding.Left));
} }
var justification = columns[columnIndex].Alignment; var justification = columns[columnIndex].Alignment;
if (justification == null) if (justification == null)
{ {
// No alignment // No alignment
builder.Append(center.Multiply(columnWidth)); builder.Append(center.Repeat(columnWidth));
} }
else if (justification.Value == Justify.Left) else if (justification.Value == Justify.Left)
{ {
// Left // Left
builder.Append(':'); builder.Append(':');
builder.Append(center.Multiply(columnWidth - 1)); builder.Append(center.Repeat(columnWidth - 1));
} }
else if (justification.Value == Justify.Center) else if (justification.Value == Justify.Center)
{ {
// Centered // Centered
builder.Append(':'); builder.Append(':');
builder.Append(center.Multiply(columnWidth - 2)); builder.Append(center.Repeat(columnWidth - 2));
builder.Append(':'); builder.Append(':');
} }
else if (justification.Value == Justify.Right) else if (justification.Value == Justify.Right)
{ {
// Right // Right
builder.Append(center.Multiply(columnWidth - 1)); builder.Append(center.Repeat(columnWidth - 1));
builder.Append(':'); builder.Append(':');
} }
// Right padding // Right padding
if (padding.Right > 0) if (padding.Right > 0)
{ {
builder.Append(" ".Multiply(padding.Right)); builder.Append(" ".Repeat(padding.Right));
} }
if (!lastColumn) if (!lastColumn)

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class MinimalDoubleHeadTableBorder : TableBorder public sealed class MinimalDoubleHeadTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Minimal; public override TableBorder? SafeBorder => TableBorder.Minimal;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class MinimalTableBorder : TableBorder public sealed class MinimalTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -9,7 +9,7 @@ namespace Spectre.Console.Rendering
public override bool Visible => false; public override bool Visible => false;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return " "; return " ";
} }

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Square; public override TableBorder? SafeBorder => TableBorder.Square;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -11,7 +11,7 @@ namespace Spectre.Console.Rendering
public override TableBorder? SafeBorder => TableBorder.Simple; public override TableBorder? SafeBorder => TableBorder.Simple;
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class SimpleTableBorder : TableBorder public sealed class SimpleTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Rendering
public sealed class SquareTableBorder : TableBorder public sealed class SquareTableBorder : TableBorder
{ {
/// <inheritdoc/> /// <inheritdoc/>
protected override string GetBorderPart(TableBorderPart part) public override string GetPart(TableBorderPart part)
{ {
return part switch return part switch
{ {

View File

@ -13,8 +13,6 @@ namespace Spectre.Console
/// </summary> /// </summary>
public abstract partial class TableBorder public abstract partial class TableBorder
{ {
private readonly Dictionary<TableBorderPart, string> _lookup;
/// <summary> /// <summary>
/// Gets a value indicating whether or not the border is visible. /// Gets a value indicating whether or not the border is visible.
/// </summary> /// </summary>
@ -26,46 +24,11 @@ namespace Spectre.Console
public virtual TableBorder? SafeBorder { get; } public virtual TableBorder? SafeBorder { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TableBorder"/> class. /// Gets the string representation of a specified table border part.
/// </summary> /// </summary>
protected TableBorder() /// <param name="part">The part to get the character representation for.</param>
{ /// <returns>A character representation of the specified border part.</returns>
_lookup = Initialize(); public abstract string GetPart(TableBorderPart part);
}
private Dictionary<TableBorderPart, string> Initialize()
{
var lookup = new Dictionary<TableBorderPart, string>();
foreach (TableBorderPart? part in Enum.GetValues(typeof(TableBorderPart)))
{
if (part == null)
{
continue;
}
var text = GetBorderPart(part.Value);
if (text.Length > 1)
{
throw new InvalidOperationException("A box part cannot contain more than one character.");
}
lookup.Add(part.Value, GetBorderPart(part.Value));
}
return lookup;
}
/// <summary>
/// Gets the string representation of a specific border part.
/// </summary>
/// <param name="part">The part to get a string representation for.</param>
/// <param name="count">The number of repetitions.</param>
/// <returns>A string representation of the specified border part.</returns>
public string GetPart(TableBorderPart part, int count)
{
// TODO: This need some optimization...
return string.Join(string.Empty, Enumerable.Repeat(GetBorderPart(part)[0], count));
}
/// <summary> /// <summary>
/// Gets a whole column row for the specific column row part. /// Gets a whole column row for the specific column row part.
@ -95,7 +58,7 @@ namespace Spectre.Console
{ {
var padding = columns[columnIndex].Padding; var padding = columns[columnIndex].Padding;
var centerWidth = padding.Left + columnWidth + padding.Right; var centerWidth = padding.Left + columnWidth + padding.Right;
builder.Append(center.Multiply(centerWidth)); builder.Append(center.Repeat(centerWidth));
if (!lastColumn) if (!lastColumn)
{ {
@ -107,30 +70,12 @@ namespace Spectre.Console
return builder.ToString(); return builder.ToString();
} }
/// <summary>
/// Gets the string representation of a specific border part.
/// </summary>
/// <param name="part">The part to get a string representation for.</param>
/// <returns>A string representation of the specified border part.</returns>
public string GetPart(TableBorderPart part)
{
return _lookup[part].ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the character representing the specified border part.
/// </summary>
/// <param name="part">The part to get the character representation for.</param>
/// <returns>A character representation of the specified border part.</returns>
protected abstract string GetBorderPart(TableBorderPart part);
/// <summary> /// <summary>
/// Gets the table parts used to render a specific table row. /// Gets the table parts used to render a specific table row.
/// </summary> /// </summary>
/// <param name="part">The table part.</param> /// <param name="part">The table part.</param>
/// <returns>The table parts used to render the specific table row.</returns> /// <returns>The table parts used to render the specific table row.</returns>
protected (string Left, string Center, string Separator, string Right) protected (string Left, string Center, string Separator, string Right) GetTableParts(TablePart part)
GetTableParts(TablePart part)
{ {
return part switch return part switch
{ {

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
namespace Spectre.Console namespace Spectre.Console
@ -123,7 +124,7 @@ namespace Spectre.Console
private static void AddBottomBorder(List<Segment> result, BoxBorder border, Style borderStyle, int panelWidth) 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, 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);
} }
@ -160,13 +161,13 @@ namespace Spectre.Console
} }
} }
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top, leftSpacing + 1), borderStyle)); segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(leftSpacing + 1), borderStyle));
segments.Add(header); segments.Add(header);
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top, rightSpacing + 1), borderStyle)); segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(rightSpacing + 1), borderStyle));
} }
else else
{ {
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top, panelWidth - EdgeWidth), borderStyle)); segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(panelWidth - EdgeWidth), borderStyle));
} }
segments.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle)); segments.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));