mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Emit native line breaks
This commit is contained in:
parent
5b33f80213
commit
4cfe55cc27
@ -48,26 +48,14 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
.ShouldBe("Hello World");
|
.ShouldBe("Hello World");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void Should_Write_Line_Breaks()
|
[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
|
// Given
|
||||||
var fixture = new PlainConsole(width: 5);
|
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
[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
|
// When
|
||||||
fixture.Render(text);
|
fixture.Render(text);
|
||||||
|
@ -39,7 +39,7 @@ namespace Spectre.Console.Composition
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a segment representing a line break.
|
/// Gets a segment representing a line break.
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// Gets an empty segment.
|
/// Gets an empty segment.
|
||||||
@ -95,7 +95,7 @@ namespace Spectre.Console.Composition
|
|||||||
/// <returns>A new segment without any trailing line endings.</returns>
|
/// <returns>A new segment without any trailing line endings.</returns>
|
||||||
public Segment StripLineEndings()
|
public Segment StripLineEndings()
|
||||||
{
|
{
|
||||||
return new Segment(Text.TrimEnd('\n'), Style);
|
return new Segment(Text.TrimEnd('\n').TrimEnd('\r'), Style);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -80,7 +80,7 @@ namespace Spectre.Console
|
|||||||
new Segment(border.GetPart(BorderPart.HeaderTopLeft)),
|
new Segment(border.GetPart(BorderPart.HeaderTopLeft)),
|
||||||
new Segment(border.GetPart(BorderPart.HeaderTop, panelWidth)),
|
new Segment(border.GetPart(BorderPart.HeaderTop, panelWidth)),
|
||||||
new Segment(border.GetPart(BorderPart.HeaderTopRight)),
|
new Segment(border.GetPart(BorderPart.HeaderTopRight)),
|
||||||
new Segment("\n"),
|
Segment.LineBreak,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Render the child.
|
// Render the child.
|
||||||
@ -118,14 +118,14 @@ namespace Spectre.Console
|
|||||||
}
|
}
|
||||||
|
|
||||||
result.Add(new Segment(border.GetPart(BorderPart.CellRight)));
|
result.Add(new Segment(border.GetPart(BorderPart.CellRight)));
|
||||||
result.Add(new Segment("\n"));
|
result.Add(Segment.LineBreak);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panel bottom
|
// Panel bottom
|
||||||
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomLeft)));
|
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomLeft)));
|
||||||
result.Add(new Segment(border.GetPart(BorderPart.FooterBottom, panelWidth)));
|
result.Add(new Segment(border.GetPart(BorderPart.FooterBottom, panelWidth)));
|
||||||
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomRight)));
|
result.Add(new Segment(border.GetPart(BorderPart.FooterBottomRight)));
|
||||||
result.Add(new Segment("\n"));
|
result.Add(Segment.LineBreak);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ namespace Spectre.Console
|
|||||||
var justification = context.Justification ?? Alignment;
|
var justification = context.Justification ?? Alignment;
|
||||||
foreach (var (_, _, last, line) in lines.Enumerate())
|
foreach (var (_, _, last, line) in lines.Enumerate())
|
||||||
{
|
{
|
||||||
var length = line.Sum(l => l.CellLength(context.Encoding));
|
var length = line.Sum(l => l.StripLineEndings().CellLength(context.Encoding));
|
||||||
if (length < maxWidth)
|
if (length < maxWidth)
|
||||||
{
|
{
|
||||||
// Justify right side
|
// Justify right side
|
||||||
|
@ -70,6 +70,11 @@ namespace Spectre.Console.Internal
|
|||||||
|
|
||||||
public static int GetCellLength(Encoding encoding, char rune)
|
public static int GetCellLength(Encoding encoding, char rune)
|
||||||
{
|
{
|
||||||
|
if (rune == '\r' || rune == '\n')
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Is it represented by a single byte?
|
// Is it represented by a single byte?
|
||||||
// In that case we don't have to calculate the
|
// In that case we don't have to calculate the
|
||||||
// actual cell width.
|
// actual cell width.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user