Fix line break regression

In commit d1d94cd, we accidentally introduced a regression
since conhost requires all line breaks to be `\r\n`.
This commit is contained in:
Patrik Svensson 2021-05-10 23:20:35 +02:00 committed by Phil Scott
parent 1dfc6bdadc
commit 6549436356
12 changed files with 73 additions and 110 deletions

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Spectre.Console.Testing namespace Spectre.Console.Testing
{ {
@ -21,7 +20,7 @@ namespace Spectre.Console.Testing
} }
var result = new List<string>(); var result = new List<string>();
foreach (var line in value.Split(new[] { '\n' })) foreach (var line in value.NormalizeLineEndings().Split(new[] { '\n' }))
{ {
result.Add(line.TrimEnd()); result.Add(line.TrimEnd());
} }

View File

@ -1,43 +0,0 @@
[
[
{
Text: Foo,
Style: {
Foreground: {},
Background: {}
}
},
{
Text: Bar,
Style: {
Foreground: {},
Background: {}
}
}
],
[
{
Text: Baz,
Style: {
Foreground: {},
Background: {}
}
},
{
Text: Qux,
Style: {
Foreground: {},
Background: {}
}
}
],
[
{
Text: Corgi,
Style: {
Foreground: {},
Background: {}
}
}
]
]

View File

@ -1,45 +0,0 @@
[
[
{
Text: Foo,
Style: {
Foreground: {},
Background: {}
}
}
],
[
{
Text: Bar,
Style: {
Foreground: {},
Background: {}
}
}
],
[
{
Text: Baz,
Style: {
Foreground: {},
Background: {}
}
},
{
Text: Qux,
Style: {
Foreground: {},
Background: {}
}
}
],
[
{
Text: Corgi,
Style: {
Foreground: {},
Background: {}
}
}
]
]

View File

@ -55,8 +55,7 @@ namespace Spectre.Console.Tests.Unit
public sealed class TheSplitLinesMethod public sealed class TheSplitLinesMethod
{ {
[Fact] [Fact]
[Expectation("Segment", "Split")] public void Should_Split_Segment()
public Task Should_Split_Segment()
{ {
// Given, When // Given, When
var lines = Segment.SplitLines( var lines = Segment.SplitLines(
@ -84,12 +83,41 @@ namespace Spectre.Console.Tests.Unit
lines[2].Count.ShouldBe(1); lines[2].Count.ShouldBe(1);
lines[2][0].Text.ShouldBe("Corgi"); lines[2][0].Text.ShouldBe("Corgi");
return Verifier.Verify(lines);
} }
[Fact] [Fact]
[Expectation("Segment", "Split_Linebreak")] public void Should_Split_Segment_With_Windows_LineBreak()
public Task Should_Split_Segments_With_Linebreak_In_Text() {
// Given, When
var lines = Segment.SplitLines(
new[]
{
new Segment("Foo"),
new Segment("Bar"),
new Segment("\r\n"),
new Segment("Baz"),
new Segment("Qux"),
new Segment("\r\n"),
new Segment("Corgi"),
});
// Then
lines.Count.ShouldBe(3);
lines[0].Count.ShouldBe(2);
lines[0][0].Text.ShouldBe("Foo");
lines[0][1].Text.ShouldBe("Bar");
lines[1].Count.ShouldBe(2);
lines[1][0].Text.ShouldBe("Baz");
lines[1][1].Text.ShouldBe("Qux");
lines[2].Count.ShouldBe(1);
lines[2][0].Text.ShouldBe("Corgi");
}
[Fact]
public void Should_Split_Segments_With_Linebreak_In_Text()
{ {
// Given, Given // Given, Given
var lines = Segment.SplitLines( var lines = Segment.SplitLines(
@ -103,7 +131,20 @@ namespace Spectre.Console.Tests.Unit
}); });
// Then // Then
return Verifier.Verify(lines); lines.Count.ShouldBe(4);
lines[0].Count.ShouldBe(1);
lines[0][0].Text.ShouldBe("Foo");
lines[1].Count.ShouldBe(1);
lines[1][0].Text.ShouldBe("Bar");
lines[2].Count.ShouldBe(2);
lines[2][0].Text.ShouldBe("Baz");
lines[2][1].Text.ShouldBe("Qux");
lines[3].Count.ShouldBe(1);
lines[3][0].Text.ShouldBe("Corgi");
} }
} }
} }

View File

@ -71,7 +71,7 @@ namespace Spectre.Console.Cli
{ {
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
_content.Append('\n'); _content.Append(Environment.NewLine);
} }
return this; return this;

View File

@ -230,9 +230,9 @@ namespace Spectre.Console.Cli
var result = new List<IRenderable> var result = new List<IRenderable>
{ {
new Markup("\n"), new Markup(Environment.NewLine),
new Markup("[yellow]ARGUMENTS:[/]"), new Markup("[yellow]ARGUMENTS:[/]"),
new Markup("\n"), new Markup(Environment.NewLine),
}; };
var grid = new Grid(); var grid = new Grid();
@ -269,9 +269,9 @@ namespace Spectre.Console.Cli
var result = new List<IRenderable> var result = new List<IRenderable>
{ {
new Markup("\n"), new Markup(Environment.NewLine),
new Markup("[yellow]OPTIONS:[/]"), new Markup("[yellow]OPTIONS:[/]"),
new Markup("\n"), new Markup(Environment.NewLine),
}; };
var grid = new Grid(); var grid = new Grid();
@ -346,9 +346,9 @@ namespace Spectre.Console.Cli
var result = new List<IRenderable> var result = new List<IRenderable>
{ {
new Markup("\n"), new Markup(Environment.NewLine),
new Markup("[yellow]COMMANDS:[/]"), new Markup("[yellow]COMMANDS:[/]"),
new Markup("\n"), new Markup(Environment.NewLine),
}; };
var grid = new Grid(); var grid = new Grid();

View File

@ -59,7 +59,7 @@ namespace Spectre.Console
/// <param name="value">The value to write.</param> /// <param name="value">The value to write.</param>
public static void MarkupLine(this IAnsiConsole console, string value) public static void MarkupLine(this IAnsiConsole console, string value)
{ {
Markup(console, value + "\n"); Markup(console, value + Environment.NewLine);
} }
/// <summary> /// <summary>
@ -71,7 +71,7 @@ namespace Spectre.Console
/// <param name="args">An array of objects to write.</param> /// <param name="args">An array of objects to write.</param>
public static void MarkupLine(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args) public static void MarkupLine(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args)
{ {
Markup(console, provider, format + "\n", args); Markup(console, provider, format + Environment.NewLine, args);
} }
} }
} }

View File

@ -73,7 +73,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(console)); throw new ArgumentNullException(nameof(console));
} }
console.Write(new Text("\n", Style.Plain)); console.Write(new Text(Environment.NewLine, Style.Plain));
} }
/// <summary> /// <summary>
@ -104,7 +104,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text)); throw new ArgumentNullException(nameof(text));
} }
console.Write(text + "\n", style); console.Write(text + Environment.NewLine, style);
} }
} }
} }

View File

@ -11,6 +11,11 @@ namespace Spectre.Console
/// </summary> /// </summary>
public static class StringExtensions public static class StringExtensions
{ {
// Cache whether or not internally normalized line endings
// already are normalized. No reason to do yet another replace if it is.
private static readonly bool _alreadyNormalized
= Environment.NewLine.Equals("\n", StringComparison.OrdinalIgnoreCase);
/// <summary> /// <summary>
/// Escapes text so that it wont be interpreted as markup. /// Escapes text so that it wont be interpreted as markup.
/// </summary> /// </summary>
@ -86,6 +91,12 @@ namespace Spectre.Console
{ {
text = text?.ReplaceExact("\r\n", "\n"); text = text?.ReplaceExact("\r\n", "\n");
text ??= string.Empty; text ??= string.Empty;
if (native && !_alreadyNormalized)
{
text = text.ReplaceExact("\n", Environment.NewLine);
}
return text; return text;
} }

View File

@ -36,7 +36,7 @@ namespace Spectre.Console
if (!last) if (!last)
{ {
builder.Append('\n'); builder.Append(Environment.NewLine);
} }
} }
} }

View File

@ -44,7 +44,7 @@ namespace Spectre.Console.Rendering
/// <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, false); public static Segment LineBreak { get; } = new Segment(Environment.NewLine, Style.Plain, true, false);
/// <summary> /// <summary>
/// Gets an empty segment. /// Gets an empty segment.

View File

@ -29,7 +29,7 @@ namespace Spectre.Console
if (_lastStatus != task.Description) if (_lastStatus != task.Description)
{ {
_lastStatus = task.Description; _lastStatus = task.Description;
_renderable = new Markup(task.Description + "\n"); _renderable = new Markup(task.Description + Environment.NewLine);
return; return;
} }
} }