mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
Render tables with zero-width columns (#1197)
Fixes https://github.com/spectreconsole/spectre.console/issues/361
This commit is contained in:
parent
9c86391fb6
commit
cec5fb4595
@ -8,7 +8,7 @@ internal static class TableRenderer
|
|||||||
public static List<Segment> Render(TableRendererContext context, List<int> columnWidths)
|
public static List<Segment> Render(TableRendererContext context, List<int> columnWidths)
|
||||||
{
|
{
|
||||||
// Can't render the table?
|
// Can't render the table?
|
||||||
if (context.TableWidth <= 0 || context.TableWidth > context.MaxWidth || columnWidths.Any(c => c <= 0))
|
if (context.TableWidth <= 0 || context.TableWidth > context.MaxWidth || columnWidths.Any(c => c < 0))
|
||||||
{
|
{
|
||||||
return new List<Segment>(new[] { new Segment("…", context.BorderStyle ?? Style.Plain) });
|
return new List<Segment>(new[] { new Segment("…", context.BorderStyle ?? Style.Plain) });
|
||||||
}
|
}
|
||||||
@ -23,12 +23,12 @@ internal static class TableRenderer
|
|||||||
|
|
||||||
// Get the list of cells for the row and calculate the cell height
|
// Get the list of cells for the row and calculate the cell height
|
||||||
var cells = new List<List<SegmentLine>>();
|
var cells = new List<List<SegmentLine>>();
|
||||||
foreach (var (columnIndex, _, _, (rowWidth, cell)) in columnWidths.Zip(row).Enumerate())
|
foreach (var (columnIndex, _, _, (columnWidth, cell)) in columnWidths.Zip(row).Enumerate())
|
||||||
{
|
{
|
||||||
var justification = context.Columns[columnIndex].Alignment;
|
var justification = context.Columns[columnIndex].Alignment;
|
||||||
var childContext = context.Options with { Justification = justification };
|
var childContext = context.Options with { Justification = justification };
|
||||||
|
|
||||||
var lines = Segment.SplitLines(cell.Render(childContext, rowWidth));
|
var lines = Segment.SplitLines(cell.Render(childContext, columnWidth));
|
||||||
cellHeight = Math.Max(cellHeight, lines.Count);
|
cellHeight = Math.Max(cellHeight, lines.Count);
|
||||||
cells.Add(lines);
|
cells.Add(lines);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
┌──┬───┐
|
||||||
|
│ │ │
|
||||||
|
├──┼───┤
|
||||||
|
│ │ A │
|
||||||
|
│ │ B │
|
||||||
|
└──┴───┘
|
@ -573,4 +573,22 @@ public sealed class TableTests
|
|||||||
// Then
|
// Then
|
||||||
return Verifier.Verify(console.Output);
|
return Verifier.Verify(console.Output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Expectation("Render_Empty_Column")]
|
||||||
|
public Task Should_Render_Empty_Column_Correctly()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new TestConsole().Width(30);
|
||||||
|
var table = new Table();
|
||||||
|
table.AddColumns(string.Empty, string.Empty);
|
||||||
|
table.AddRow(string.Empty, "A");
|
||||||
|
table.AddRow(string.Empty, "B");
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Write(table);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
return Verifier.Verify(console.Output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user