Fix line ending problem with text

This commit is contained in:
Patrik Svensson 2020-08-16 12:26:51 +02:00
parent d7bbaf4a85
commit 5b33f80213
3 changed files with 41 additions and 7 deletions

View File

@ -95,6 +95,29 @@ namespace Spectre.Console.Tests.Unit
console.Lines[1].ShouldBe(" ");
console.Lines[2].ShouldBe("Qux Corgi");
}
[Fact]
public void Should_Add_Empty_Row_At_The_End()
{
// Given
var console = new PlainConsole(width: 80);
var grid = new Grid();
grid.AddColumns(2);
grid.AddRow("Foo", "Bar");
grid.AddEmptyRow();
grid.AddRow("Qux", "Corgi");
grid.AddEmptyRow();
// When
console.Render(grid);
// Then
console.Lines.Count.ShouldBe(4);
console.Lines[0].ShouldBe("Foo Bar ");
console.Lines[1].ShouldBe(" ");
console.Lines[2].ShouldBe("Qux Corgi");
console.Lines[3].ShouldBe(" ");
}
}
[Fact]

View File

@ -62,6 +62,20 @@ namespace Spectre.Console.Tests.Unit
fixture.RawOutput.ShouldBe("Hello\n\nWorld");
}
[Fact]
public void Should_Write_Line_Breaks_At_End()
{
// Given
var fixture = new PlainConsole(width: 5);
var text = new Text("Hello\n\nWorld\n\n");
// When
fixture.Render(text);
// Then
fixture.RawOutput.ShouldBe("Hello\n\nWorld\n\n");
}
[Theory]
[InlineData(5, "Hello World", "Hello\nWorld")]
[InlineData(10, "Hello Sweet Nice World", "Hello \nSweet Nice\nWorld")]

View File

@ -86,12 +86,13 @@ namespace Spectre.Console
return Array.Empty<Segment>();
}
var justification = context.Justification ?? Alignment;
var lines = SplitLines(context, maxWidth);
// Justify lines
var justification = context.Justification ?? Alignment;
foreach (var (_, _, last, line) in lines.Enumerate())
{
var length = line.Sum(l => l.StripLineEndings().CellLength(context.Encoding));
var length = line.Sum(l => l.CellLength(context.Encoding));
if (length < maxWidth)
{
// Justify right side
@ -135,10 +136,6 @@ namespace Spectre.Console
foreach (var (_, first, last, part) in text.SplitLines().Enumerate())
{
var current = part;
if (string.IsNullOrEmpty(current) && last)
{
break;
}
if (first)
{