Add option to show separator between table rows (#1304)

* Add option to show separator between table rows
* Panels should show header if borders are not shown

Closes #835
This commit is contained in:
Patrik Svensson
2023-09-16 18:49:12 +02:00
committed by GitHub
parent 037e109699
commit c82d8c4523
33 changed files with 272 additions and 43 deletions

View File

@ -132,6 +132,15 @@ public sealed class Panel : Renderable, IHasBoxBorder, IHasBorder, IExpandable,
{
AddTopBorder(result, options, border, borderStyle, panelWidth);
}
else
{
// Not showing border, but we have a header text?
// Use a invisible border to draw the top border
if (Header?.Text != null)
{
AddTopBorder(result, options, BoxBorder.None, borderStyle, panelWidth);
}
}
// Split the child segments into lines.
var childSegments = ((IRenderable)child).Render(options with { Height = height }, innerWidth);

View File

@ -31,6 +31,11 @@ public sealed class Table : Renderable, IHasTableBorder, IExpandable, IAlignable
/// </summary>
public bool ShowHeaders { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether or not row separators should be shown.
/// </summary>
public bool ShowRowSeparators { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not table footers should be shown.
/// </summary>

View File

@ -36,7 +36,9 @@ internal static class TableRenderer
// Show top of header?
if (isFirstRow && context.ShowBorder)
{
var separator = Aligner.Align(context.Border.GetColumnRow(TablePart.Top, columnWidths, context.Columns), context.Alignment, context.MaxWidth);
var separator = Aligner.Align(
context.Border.GetColumnRow(TablePart.Top, columnWidths, context.Columns),
context.Alignment, context.MaxWidth);
result.Add(new Segment(separator, context.BorderStyle));
result.Add(Segment.LineBreak);
}
@ -66,7 +68,9 @@ internal static class TableRenderer
if (isFirstCell && context.ShowBorder)
{
// Show left column edge
var part = isFirstRow && context.ShowHeaders ? TableBorderPart.HeaderLeft : TableBorderPart.CellLeft;
var part = isFirstRow && context.ShowHeaders
? TableBorderPart.HeaderLeft
: TableBorderPart.CellLeft;
rowResult.Add(new Segment(context.Border.GetPart(part), context.BorderStyle));
}
@ -91,7 +95,8 @@ internal static class TableRenderer
}
// Pad column on the right side
if (context.ShowBorder || (context.HideBorder && !isLastCell) || (context.HideBorder && isLastCell && context.IsGrid && context.PadRightCell))
if (context.ShowBorder || (context.HideBorder && !isLastCell) ||
(context.HideBorder && isLastCell && context.IsGrid && context.PadRightCell))
{
var rightPadding = context.Columns[cellIndex].Padding.GetRightSafe();
if (rightPadding > 0)
@ -103,13 +108,17 @@ internal static class TableRenderer
if (isLastCell && context.ShowBorder)
{
// Add right column edge
var part = isFirstRow && context.ShowHeaders ? TableBorderPart.HeaderRight : TableBorderPart.CellRight;
var part = isFirstRow && context.ShowHeaders
? TableBorderPart.HeaderRight
: TableBorderPart.CellRight;
rowResult.Add(new Segment(context.Border.GetPart(part), context.BorderStyle));
}
else if (context.ShowBorder)
{
// Add column separator
var part = isFirstRow && context.ShowHeaders ? TableBorderPart.HeaderSeparator : TableBorderPart.CellSeparator;
var part = isFirstRow && context.ShowHeaders
? TableBorderPart.HeaderSeparator
: TableBorderPart.CellSeparator;
rowResult.Add(new Segment(context.Border.GetPart(part), context.BorderStyle));
}
}
@ -133,15 +142,40 @@ internal static class TableRenderer
// Show header separator?
if (isFirstRow && context.ShowBorder && context.ShowHeaders && context.HasRows)
{
var separator = Aligner.Align(context.Border.GetColumnRow(TablePart.HeaderSeparator, columnWidths, context.Columns), context.Alignment, context.MaxWidth);
var separator =
Aligner.Align(
context.Border.GetColumnRow(TablePart.HeaderSeparator, columnWidths, context.Columns),
context.Alignment, context.MaxWidth);
result.Add(new Segment(separator, context.BorderStyle));
result.Add(Segment.LineBreak);
}
// Show row separator?
if (context.Border.SupportsRowSeparator && context.ShowRowSeparators
&& !isFirstRow && !isLastRow)
{
var hasVisibleFootes = context is { ShowFooters: true, HasFooters: true };
var isNextLastLine = index == context.Rows.Count - 2;
var isRenderingFooter = hasVisibleFootes && isNextLastLine;
if (!isRenderingFooter)
{
var separator =
Aligner.Align(
context.Border.GetColumnRow(TablePart.RowSeparator, columnWidths, context.Columns),
context.Alignment, context.MaxWidth);
result.Add(new Segment(separator, context.BorderStyle));
result.Add(Segment.LineBreak);
}
}
// Show bottom of footer?
if (isLastRow && context.ShowBorder)
{
var separator = Aligner.Align(context.Border.GetColumnRow(TablePart.Bottom, columnWidths, context.Columns), context.Alignment, context.MaxWidth);
var separator =
Aligner.Align(
context.Border.GetColumnRow(TablePart.Bottom, columnWidths, context.Columns),
context.Alignment, context.MaxWidth);
result.Add(new Segment(separator, context.BorderStyle));
result.Add(Segment.LineBreak);
}
@ -151,7 +185,8 @@ internal static class TableRenderer
return result;
}
private static IEnumerable<Segment> RenderAnnotation(TableRendererContext context, TableTitle? header, Style defaultStyle)
private static IEnumerable<Segment> RenderAnnotation(TableRendererContext context, TableTitle? header,
Style defaultStyle)
{
if (header == null)
{

View File

@ -10,6 +10,7 @@ internal sealed class TableRendererContext : TableAccessor
public TableBorder Border { get; }
public Style BorderStyle { get; }
public bool ShowBorder { get; }
public bool ShowRowSeparators { get; }
public bool HasRows { get; }
public bool HasFooters { get; }
@ -47,6 +48,7 @@ internal sealed class TableRendererContext : TableAccessor
HasFooters = Rows.Any(column => column.IsFooter);
Border = table.Border.GetSafeBorder(!options.Unicode && table.UseSafeBorder);
BorderStyle = table.BorderStyle ?? Style.Plain;
ShowRowSeparators = table.ShowRowSeparators;
TableWidth = tableWidth;
MaxWidth = maxWidth;