Compare commits

...

2 Commits

Author SHA1 Message Date
Patrik Svensson
4cfe55cc27 Emit native line breaks 2020-08-16 13:47:57 +02:00
Patrik Svensson
5b33f80213 Fix line ending problem with text 2020-08-16 12:26:51 +02:00
6 changed files with 42 additions and 15 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

@@ -48,18 +48,20 @@ namespace Spectre.Console.Tests.Unit
.ShouldBe("Hello World");
}
[Fact]
public void Should_Write_Line_Breaks()
[Theory]
[InlineData("Hello\n\nWorld\n\n")]
[InlineData("Hello\r\n\r\nWorld\r\n\r\n")]
public void Should_Write_Line_Breaks(string input)
{
// Given
var fixture = new PlainConsole(width: 5);
var text = new Text("Hello\n\nWorld");
var text = new Text(input);
// When
fixture.Render(text);
// Then
fixture.RawOutput.ShouldBe("Hello\n\nWorld");
fixture.RawOutput.ShouldBe("Hello\n\nWorld\n\n");
}
[Theory]

View File

@@ -39,7 +39,7 @@ namespace Spectre.Console.Composition
/// <summary>
/// Gets a segment representing a line break.
/// </summary>
public static Segment LineBreak { get; } = new Segment("\n", Style.Plain, true);
public static Segment LineBreak { get; } = new Segment(Environment.NewLine, Style.Plain, true);
/// <summary>
/// Gets an empty segment.
@@ -95,7 +95,7 @@ namespace Spectre.Console.Composition
/// <returns>A new segment without any trailing line endings.</returns>
public Segment StripLineEndings()
{
return new Segment(Text.TrimEnd('\n'), Style);
return new Segment(Text.TrimEnd('\n').TrimEnd('\r'), Style);
}
/// <summary>

View File

@@ -80,7 +80,7 @@ namespace Spectre.Console
new Segment(border.GetPart(BorderPart.HeaderTopLeft)),
new Segment(border.GetPart(BorderPart.HeaderTop, panelWidth)),
new Segment(border.GetPart(BorderPart.HeaderTopRight)),
new Segment("\n"),
Segment.LineBreak,
};
// Render the child.
@@ -118,14 +118,14 @@ namespace Spectre.Console
}
result.Add(new Segment(border.GetPart(BorderPart.CellRight)));
result.Add(new Segment("\n"));
result.Add(Segment.LineBreak);
}
// Panel bottom
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomLeft)));
result.Add(new Segment(border.GetPart(BorderPart.FooterBottom, panelWidth)));
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomRight)));
result.Add(new Segment("\n"));
result.Add(Segment.LineBreak);
return result;
}

View File

@@ -86,9 +86,10 @@ 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));
@@ -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)
{

View File

@@ -70,6 +70,11 @@ namespace Spectre.Console.Internal
public static int GetCellLength(Encoding encoding, char rune)
{
if (rune == '\r' || rune == '\n')
{
return 0;
}
// Is it represented by a single byte?
// In that case we don't have to calculate the
// actual cell width.