Compare commits

...

3 Commits

Author SHA1 Message Date
Patrik Svensson
9aa36c4cf0 Do not include cell separators in grid
Closes #40
2020-08-11 11:53:34 +02:00
Patrik Svensson
22d4af4482 Preserve line breaks 2020-08-10 11:42:34 +02:00
Patrik Svensson
f4d1796e40 Do not split lines if width is 0
Closes #32
2020-08-09 11:53:56 +02:00
9 changed files with 100 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ namespace Spectre.Console.Tests
public Color Background { get; set; }
public StringWriter Writer { get; }
public string RawOutput => Writer.ToString();
public string Output => Writer.ToString().TrimEnd('\n');
public IReadOnlyList<string> Lines => Output.Split(new char[] { '\n' });

View File

@@ -99,13 +99,12 @@ namespace Spectre.Console.Tests.Unit.Composition
}
[Fact]
public void Should_Render_Grid_Column_Padding_Correctly()
public void Should_Use_Default_Padding()
{
// Given
var console = new PlainConsole(width: 80);
var grid = new Grid();
grid.AddColumn(new GridColumn { Padding = new Padding(3, 0) });
grid.AddColumns(2);
grid.AddColumns(3);
grid.AddRow("Foo", "Bar", "Baz");
grid.AddRow("Qux", "Corgi", "Waldo");
grid.AddRow("Grault", "Garply", "Fred");
@@ -115,9 +114,32 @@ namespace Spectre.Console.Tests.Unit.Composition
// Then
console.Lines.Count.ShouldBe(3);
console.Lines[0].ShouldBe(" Foo Bar Baz ");
console.Lines[1].ShouldBe(" Qux Corgi Waldo");
console.Lines[2].ShouldBe(" Grault Garply Fred ");
console.Lines[0].ShouldBe("Foo Bar Baz ");
console.Lines[1].ShouldBe("Qux Corgi Waldo");
console.Lines[2].ShouldBe("Grault Garply Fred ");
}
[Fact]
public void Should_Render_Explicit_Grid_Column_Padding_Correctly()
{
// Given
var console = new PlainConsole(width: 80);
var grid = new Grid();
grid.AddColumn(new GridColumn { Padding = new Padding(3, 0) });
grid.AddColumn(new GridColumn { Padding = new Padding(0, 0) });
grid.AddColumn(new GridColumn { Padding = new Padding(0, 3) });
grid.AddRow("Foo", "Bar", "Baz");
grid.AddRow("Qux", "Corgi", "Waldo");
grid.AddRow("Grault", "Garply", "Fred");
// When
console.Render(grid);
// Then
console.Lines.Count.ShouldBe(3);
console.Lines[0].ShouldBe(" Foo Bar Baz ");
console.Lines[1].ShouldBe(" Qux Corgi Waldo ");
console.Lines[2].ShouldBe(" GraultGarplyFred ");
}
[Fact]

View File

@@ -254,9 +254,9 @@ namespace Spectre.Console.Tests.Unit.Composition
// Then
console.Lines.Count.ShouldBe(3);
console.Lines[0].ShouldBe("Foo Bar Baz ");
console.Lines[1].ShouldBe("Qux Corgi Waldo");
console.Lines[2].ShouldBe("Grault Garply Fred ");
console.Lines[0].ShouldBe("Foo Bar Baz ");
console.Lines[1].ShouldBe("Qux Corgi Waldo");
console.Lines[2].ShouldBe("Grault Garply Fred ");
}
[Fact]
@@ -307,5 +307,24 @@ namespace Spectre.Console.Tests.Unit.Composition
console.Lines[5].ShouldBe("│ Grault │ Garply │ Fred │");
console.Lines[6].ShouldBe("└────────┴────────┴──────────┘");
}
[Fact]
public void Should_Render_Table_Without_Footer_If_No_Rows_Are_Added()
{
// Given
var console = new PlainConsole(width: 80);
var table = new Table();
table.AddColumns("Foo", "Bar");
table.AddColumn(new TableColumn("Baz") { Padding = new Padding(3, 2) });
// When
console.Render(table);
// Then
console.Lines.Count.ShouldBe(3);
console.Lines[0].ShouldBe("┌─────┬─────┬────────┐");
console.Lines[1].ShouldBe("│ Foo │ Bar │ Baz │");
console.Lines[2].ShouldBe("└─────┴─────┴────────┘");
}
}
}

View File

@@ -21,6 +21,20 @@ namespace Spectre.Console.Tests.Unit
.ShouldBe("Hello World");
}
[Fact]
public void Should_Write_Line_Breaks()
{
// Given
var fixture = new PlainConsole(width: 5);
var text = Text.New("\n\n");
// When
fixture.Render(text);
// Then
fixture.RawOutput.ShouldBe("\n\n");
}
[Fact]
public void Should_Split_Unstyled_Text_To_New_Lines_If_Width_Exceeds_Console_Width()
{

View File

@@ -21,6 +21,7 @@ namespace Spectre.Console
Border = BorderKind.None,
ShowHeaders = false,
IsGrid = true,
PadRightCell = false,
};
}
@@ -55,11 +56,14 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(column));
}
// Only pad the most right cell if we've explicitly set a padding.
_table.PadRightCell = column.Padding != null;
_table.AddColumn(new TableColumn(string.Empty)
{
Width = column.Width,
NoWrap = column.NoWrap,
Padding = column.Padding,
Padding = column.Padding ?? new Padding(0, 2),
Alignment = column.Alignment,
});
}

View File

@@ -20,7 +20,7 @@ namespace Spectre.Console
/// <summary>
/// Gets or sets the padding of the column.
/// </summary>
public Padding Padding { get; set; } = new Padding(0, 1);
public Padding? Padding { get; set; } = null;
/// <summary>
/// Gets or sets the alignment of the column.

View File

@@ -61,7 +61,7 @@ namespace Spectre.Console
.Where(x => x.allowWrap)
.Max(x => x.width);
var secondMaxColumn = widths.Zip(wrappable, (width, allowWrap) => allowWrap && width != maxColumn ? width : 0).Max();
var secondMaxColumn = widths.Zip(wrappable, (width, allowWrap) => allowWrap && width != maxColumn ? width : 1).Max();
var columnDifference = maxColumn - secondMaxColumn;
var ratios = widths.Zip(wrappable, (width, allowWrap) => width == maxColumn && allowWrap ? 1 : 0).ToList();
@@ -96,9 +96,15 @@ namespace Spectre.Console
var minWidths = new List<int>();
var maxWidths = new List<int>();
// Include columns in measurement
var measure = ((IRenderable)column.Text).Measure(options, maxWidth);
minWidths.Add(measure.Min);
maxWidths.Add(measure.Max);
foreach (var row in rows)
{
var measure = ((IRenderable)row).Measure(options, maxWidth);
measure = ((IRenderable)row).Measure(options, maxWidth);
minWidths.Add(measure.Min);
maxWidths.Add(measure.Max);
}

View File

@@ -53,8 +53,14 @@ namespace Spectre.Console
/// </summary>
public bool SafeBorder { get; set; } = true;
// Whether this is a grid or not.
internal bool IsGrid { get; set; } = false;
// Whether or not the most right cell should be padded.
// This is almost always the case, unless we're rendering
// a grid without explicit padding in the last cell.
internal bool PadRightCell { get; set; } = true;
/// <summary>
/// Initializes a new instance of the <see cref="Table"/> class.
/// </summary>
@@ -178,6 +184,7 @@ namespace Spectre.Console
var showBorder = Border != BorderKind.None;
var hideBorder = Border == BorderKind.None;
var hasRows = _rows.Count > 0;
var maxWidth = width;
if (Width != null)
@@ -278,7 +285,7 @@ namespace Spectre.Console
}
// Pad column on the right side
if (showBorder || (hideBorder && !lastCell) || (IsGrid && !lastCell))
if (showBorder || (hideBorder && !lastCell) || (hideBorder && lastCell && IsGrid && PadRightCell))
{
var rightPadding = _columns[cellIndex].Padding.Right;
if (rightPadding > 0)
@@ -292,7 +299,7 @@ namespace Spectre.Console
// Add right column edge
result.Add(new Segment(border.GetPart(BorderPart.CellRight)));
}
else if (showBorder || (hideBorder && !lastCell))
else if (showBorder)
{
// Add column separator
result.Add(new Segment(border.GetPart(BorderPart.CellSeparator)));
@@ -303,7 +310,7 @@ namespace Spectre.Console
}
// Show header separator?
if (firstRow && showBorder && ShowHeaders)
if (firstRow && showBorder && ShowHeaders && hasRows)
{
result.Add(new Segment(border.GetPart(BorderPart.HeaderBottomLeft)));
foreach (var (columnIndex, first, lastColumn, columnWidth) in columnWidths.Enumerate())

View File

@@ -136,7 +136,12 @@ namespace Spectre.Console
/// <inheritdoc/>
IEnumerable<Segment> IRenderable.Render(RenderContext context, int width)
{
if (string.IsNullOrWhiteSpace(_text))
if (string.IsNullOrEmpty(_text))
{
return Array.Empty<Segment>();
}
if (width == 0)
{
return Array.Empty<Segment>();
}
@@ -187,7 +192,7 @@ namespace Spectre.Console
}
}
if (!last)
if (!last || line.Count == 0)
{
result.Add(Segment.LineBreak());
}
@@ -209,7 +214,10 @@ namespace Spectre.Console
var index = segment.Text.IndexOf("\n", StringComparison.OrdinalIgnoreCase);
if (index == -1)
{
result.Add(segment);
if (!string.IsNullOrEmpty(segment.Text))
{
result.Add(segment);
}
}
else
{