using System;
using System.Collections.Generic;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
///
/// Represents a border.
///
public abstract partial class TableBorder
{
///
/// Gets a value indicating whether or not the border is visible.
///
public virtual bool Visible { get; } = true;
///
/// Gets the safe border for this border or null if none exist.
///
public virtual TableBorder? SafeBorder { get; }
///
/// Gets the string representation of a specified table border part.
///
/// The part to get the character representation for.
/// A character representation of the specified border part.
public abstract string GetPart(TableBorderPart part);
///
/// Gets a whole column row for the specific column row part.
///
/// The column row part.
/// The column widths.
/// The columns.
/// A string representing the column row.
public virtual string GetColumnRow(TablePart part, IReadOnlyList widths, IReadOnlyList columns)
{
if (widths is null)
{
throw new ArgumentNullException(nameof(widths));
}
if (columns is null)
{
throw new ArgumentNullException(nameof(columns));
}
var (left, center, separator, right) = GetTableParts(part);
var builder = new StringBuilder();
builder.Append(left);
foreach (var (columnIndex, _, lastColumn, columnWidth) in widths.Enumerate())
{
var padding = columns[columnIndex].Padding;
var centerWidth = padding.GetLeftSafe() + columnWidth + padding.GetRightSafe();
builder.Append(center.Repeat(centerWidth));
if (!lastColumn)
{
builder.Append(separator);
}
}
builder.Append(right);
return builder.ToString();
}
///
/// Gets the table parts used to render a specific table row.
///
/// The table part.
/// The table parts used to render the specific table row.
protected (string Left, string Center, string Separator, string Right) GetTableParts(TablePart part)
{
return part switch
{
// Top part
TablePart.Top =>
(GetPart(TableBorderPart.HeaderTopLeft), GetPart(TableBorderPart.HeaderTop),
GetPart(TableBorderPart.HeaderTopSeparator), GetPart(TableBorderPart.HeaderTopRight)),
// Separator between header and cells
TablePart.HeaderSeparator =>
(GetPart(TableBorderPart.HeaderBottomLeft), GetPart(TableBorderPart.HeaderBottom),
GetPart(TableBorderPart.HeaderBottomSeparator), GetPart(TableBorderPart.HeaderBottomRight)),
// Separator between footer and cells
TablePart.FooterSeparator =>
(GetPart(TableBorderPart.FooterTopLeft), GetPart(TableBorderPart.FooterTop),
GetPart(TableBorderPart.FooterTopSeparator), GetPart(TableBorderPart.FooterTopRight)),
// Bottom part
TablePart.Bottom =>
(GetPart(TableBorderPart.FooterBottomLeft), GetPart(TableBorderPart.FooterBottom),
GetPart(TableBorderPart.FooterBottomSeparator), GetPart(TableBorderPart.FooterBottomRight)),
// Unknown
_ => throw new NotSupportedException("Unknown column row part"),
};
}
}
}