Preserve line breaks

This commit is contained in:
Patrik Svensson 2020-08-10 11:35:01 +02:00 committed by Patrik Svensson
parent f4d1796e40
commit 22d4af4482
3 changed files with 21 additions and 3 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

@ -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

@ -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);