Fixes TextPath rendering bugs (#1308)

* Do not emit line break when rendering
* Don't be greedy when measuring.
* Fix a condition that decides if the path fits in the allotted space.

Closes #1307
This commit is contained in:
Patrik Svensson 2023-09-18 08:04:57 +02:00 committed by GitHub
parent 2af3f7faeb
commit 943c045fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 34 deletions

View File

@ -74,7 +74,7 @@ public sealed class TextPath : IRenderable, IHasJustification
return new Measurement(
Math.Min(length, maxWidth),
Math.Max(length, maxWidth));
Math.Min(length, maxWidth));
}
/// <inheritdoc/>
@ -119,9 +119,6 @@ public sealed class TextPath : IRenderable, IHasJustification
// Align the result
Aligner.Align(parts, Justification, maxWidth);
// Insert a line break
parts.Add(Segment.LineBreak);
return parts;
}
@ -134,7 +131,7 @@ public sealed class TextPath : IRenderable, IHasJustification
}
// Will it fit as is?
if (_parts.Sum(p => Cell.GetCellLength(p)) + (_parts.Length - 1) < maxWidth)
if (_parts.Sum(Cell.GetCellLength) + (_parts.Length - 1) <= maxWidth)
{
return _parts;
}
@ -159,7 +156,7 @@ public sealed class TextPath : IRenderable, IHasJustification
var queueWidth =
rootLength // Root (if rooted)
+ ellipsisLength // Ellipsis
+ queue.Sum(p => Cell.GetCellLength(p)) // Middle
+ queue.Sum(Cell.GetCellLength) // Middle
+ Cell.GetCellLength(_parts.Last()) // Last
+ queue.Count + separatorCount; // Separators

View File

@ -0,0 +1,3 @@
┌─────┐ ┌─────┐
│ Baz │ │ Qux │
└─────┘ └─────┘

View File

@ -1,5 +1,7 @@
namespace Spectre.Console.Tests.Unit;
[UsesVerify]
[ExpectationPath("Widgets/TextPath")]
public sealed class TextPathTests
{
[Theory]
@ -14,8 +16,7 @@ public sealed class TextPathTests
console.Write(new TextPath(input));
// Then
console.Output.TrimEnd()
.ShouldBe(expected);
console.Output.ShouldBe(expected);
}
[Theory]
@ -31,8 +32,7 @@ public sealed class TextPathTests
console.Write(new TextPath(input));
// Then
console.Output.TrimEnd()
.ShouldBe(expected);
console.Output.ShouldBe(expected);
}
[Theory]
@ -48,26 +48,7 @@ public sealed class TextPathTests
console.Write(new TextPath(input));
// Then
console.Output.TrimEnd()
.ShouldBe(expected);
}
[Theory]
[InlineData("C:/My documents/Bar/Baz.txt")]
[InlineData("/My documents/Bar/Baz.txt")]
[InlineData("My documents/Bar/Baz.txt")]
[InlineData("Bar/Baz.txt")]
[InlineData("Baz.txt")]
public void Should_Insert_Line_Break_At_End_Of_Path(string input)
{
// Given
var console = new TestConsole().Width(80);
// When
console.Write(new TextPath(input));
// Then
console.Output.ShouldEndWith("\n");
console.Output.ShouldBe(expected);
}
[Fact]
@ -80,8 +61,7 @@ public sealed class TextPathTests
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightJustified());
// Then
console.Output.TrimEnd('\n')
.ShouldBe(" C:/My documents/Bar/Baz.txt");
console.Output.ShouldBe(" C:/My documents/Bar/Baz.txt");
}
[Fact]
@ -94,7 +74,24 @@ public sealed class TextPathTests
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").Centered());
// Then
console.Output.TrimEnd('\n')
.ShouldBe(" C:/My documents/Bar/Baz.txt ");
console.Output.ShouldBe(" C:/My documents/Bar/Baz.txt ");
}
[Fact]
[Expectation("GH-1307")]
[GitHubIssue("https://github.com/spectreconsole/spectre.console/issues/1307")]
public Task Should_Behave_As_Expected_When_Rendering_Inside_Panel_Columns()
{
// Given
var console = new TestConsole().Width(40);
// When
console.Write(
new Columns(
new Panel(new Text("Baz")),
new Panel(new TextPath("Qux"))));
// Then
return Verifier.Verify(console.Output);
}
}

View File

@ -0,0 +1,12 @@
namespace Spectre.Console.Tests;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class GitHubIssueAttribute : Attribute
{
public string Url { get; }
public GitHubIssueAttribute(string url)
{
Url = url;
}
}