mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-10-25 15:19:23 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9aa36c4cf0 | ||
|
|
22d4af4482 |
@@ -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' });
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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>
|
||||
@@ -279,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)
|
||||
@@ -293,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)));
|
||||
|
||||
@@ -136,7 +136,7 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
IEnumerable<Segment> IRenderable.Render(RenderContext context, int width)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_text))
|
||||
if (string.IsNullOrEmpty(_text))
|
||||
{
|
||||
return Array.Empty<Segment>();
|
||||
}
|
||||
@@ -192,7 +192,7 @@ namespace Spectre.Console
|
||||
}
|
||||
}
|
||||
|
||||
if (!last)
|
||||
if (!last || line.Count == 0)
|
||||
{
|
||||
result.Add(Segment.LineBreak());
|
||||
}
|
||||
@@ -214,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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user