Reset colors before line break

Closes #28
This commit is contained in:
Patrik Svensson 2020-08-07 13:08:28 +02:00 committed by Patrik Svensson
parent 9637066927
commit d475e3b30a
3 changed files with 30 additions and 14 deletions

View File

@ -247,7 +247,7 @@ namespace Spectre.Console.Tests.Unit
public sealed class WriteLine
{
[Fact]
public void Should_Reset_Colors_Correctly()
public void Should_Reset_Colors_Correctly_After_Line_Break()
{
// Given
var fixture = new AnsiConsoleFixture(ColorSystem.Standard, AnsiSupport.Yes);
@ -263,6 +263,21 @@ namespace Spectre.Console.Tests.Unit
.ShouldBe("Hello\nWorld\n");
}
[Fact]
public void Should_Reset_Colors_Correctly_After_Line_Break_In_Text()
{
// Given
var fixture = new AnsiConsoleFixture(ColorSystem.Standard, AnsiSupport.Yes);
// When
fixture.Console.Background = ConsoleColor.Red;
fixture.Console.WriteLine("Hello\nWorld");
// Then
fixture.Output.NormalizeLineEndings()
.ShouldBe("Hello\nWorld\n");
}
[Theory]
[InlineData(AnsiSupport.Yes)]
[InlineData(AnsiSupport.No)]

View File

@ -1,6 +1,5 @@
using System;
using System.Globalization;
using Spectre.Console.Internal;
namespace Spectre.Console
{
@ -20,12 +19,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(console));
}
using (console.PushColor(Color.Default, true))
using (console.PushColor(Color.Default, false))
using (console.PushDecoration(Decoration.None))
{
console.Write(Environment.NewLine);
}
console.Write(Environment.NewLine);
}
/// <summary>

View File

@ -60,12 +60,19 @@ namespace Spectre.Console.Internal
return;
}
_out.Write(AnsiBuilder.GetAnsi(
_system,
text.NormalizeLineEndings(native: true),
Decoration,
Foreground,
Background));
var parts = text.NormalizeLineEndings().Split(new[] { '\n' });
foreach (var (_, _, last, part) in parts.Enumerate())
{
if (!string.IsNullOrEmpty(part))
{
_out.Write(AnsiBuilder.GetAnsi(_system, part, Decoration, Foreground, Background));
}
if (!last)
{
_out.Write(Environment.NewLine);
}
}
}
}
}