Compare commits

...

2 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
8 changed files with 66 additions and 16 deletions

View File

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

View File

@@ -99,13 +99,35 @@ namespace Spectre.Console.Tests.Unit.Composition
} }
[Fact] [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.AddColumns(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("Grault Garply Fred ");
}
[Fact]
public void Should_Render_Explicit_Grid_Column_Padding_Correctly()
{ {
// Given // Given
var console = new PlainConsole(width: 80); var console = new PlainConsole(width: 80);
var grid = new Grid(); var grid = new Grid();
grid.AddColumn(new GridColumn { Padding = new Padding(3, 0) }); grid.AddColumn(new GridColumn { Padding = new Padding(3, 0) });
grid.AddColumns(2); 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("Foo", "Bar", "Baz");
grid.AddRow("Qux", "Corgi", "Waldo"); grid.AddRow("Qux", "Corgi", "Waldo");
grid.AddRow("Grault", "Garply", "Fred"); grid.AddRow("Grault", "Garply", "Fred");

View File

@@ -21,6 +21,20 @@ namespace Spectre.Console.Tests.Unit
.ShouldBe("Hello World"); .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] [Fact]
public void Should_Split_Unstyled_Text_To_New_Lines_If_Width_Exceeds_Console_Width() 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, Border = BorderKind.None,
ShowHeaders = false, ShowHeaders = false,
IsGrid = true, IsGrid = true,
PadRightCell = false,
}; };
} }
@@ -55,11 +56,14 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(column)); 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) _table.AddColumn(new TableColumn(string.Empty)
{ {
Width = column.Width, Width = column.Width,
NoWrap = column.NoWrap, NoWrap = column.NoWrap,
Padding = column.Padding, Padding = column.Padding ?? new Padding(0, 2),
Alignment = column.Alignment, Alignment = column.Alignment,
}); });
} }

View File

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

View File

@@ -53,8 +53,14 @@ namespace Spectre.Console
/// </summary> /// </summary>
public bool SafeBorder { get; set; } = true; public bool SafeBorder { get; set; } = true;
// Whether this is a grid or not.
internal bool IsGrid { get; set; } = false; 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> /// <summary>
/// Initializes a new instance of the <see cref="Table"/> class. /// Initializes a new instance of the <see cref="Table"/> class.
/// </summary> /// </summary>
@@ -279,7 +285,7 @@ namespace Spectre.Console
} }
// Pad column on the right side // 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; var rightPadding = _columns[cellIndex].Padding.Right;
if (rightPadding > 0) if (rightPadding > 0)
@@ -293,7 +299,7 @@ namespace Spectre.Console
// Add right column edge // Add right column edge
result.Add(new Segment(border.GetPart(BorderPart.CellRight))); result.Add(new Segment(border.GetPart(BorderPart.CellRight)));
} }
else if (showBorder || (hideBorder && !lastCell)) else if (showBorder)
{ {
// Add column separator // Add column separator
result.Add(new Segment(border.GetPart(BorderPart.CellSeparator))); result.Add(new Segment(border.GetPart(BorderPart.CellSeparator)));

View File

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