Skip to content

Commit

Permalink
Fixes TextPath rendering bugs (#1308)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
patriksvensson authored Sep 18, 2023
1 parent 2af3f7f commit 943c045
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
9 changes: 3 additions & 6 deletions src/Spectre.Console/Widgets/TextPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Measurement Measure(RenderOptions options, int maxWidth)

return new Measurement(
Math.Min(length, maxWidth),
Math.Max(length, maxWidth));
Math.Min(length, maxWidth));
}

/// <inheritdoc/>
Expand Down Expand Up @@ -119,9 +119,6 @@ public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
// Align the result
Aligner.Align(parts, Justification, maxWidth);

// Insert a line break
parts.Add(Segment.LineBreak);

return parts;
}

Expand All @@ -134,7 +131,7 @@ private string[] Fit(RenderOptions options, int maxWidth)
}

// 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;
}
Expand All @@ -159,7 +156,7 @@ private string[] Fit(RenderOptions options, int maxWidth)
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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
┌─────┐ ┌─────┐
│ Baz │ │ Qux │
└─────┘ └─────┘
45 changes: 21 additions & 24 deletions test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Spectre.Console.Tests.Unit;

[UsesVerify]
[ExpectationPath("Widgets/TextPath")]
public sealed class TextPathTests
{
[Theory]
Expand All @@ -14,8 +16,7 @@ public void Should_Use_Last_Segments_If_Less_Than_Three(int width, string input,
console.Write(new TextPath(input));

// Then
console.Output.TrimEnd()
.ShouldBe(expected);
console.Output.ShouldBe(expected);
}

[Theory]
Expand All @@ -31,8 +32,7 @@ public void Should_Render_Full_Path_If_Possible(string input, string expected)
console.Write(new TextPath(input));

// Then
console.Output.TrimEnd()
.ShouldBe(expected);
console.Output.ShouldBe(expected);
}

[Theory]
Expand All @@ -48,53 +48,50 @@ public void Should_Pop_Segments_From_Left(int width, string input, string expect
console.Write(new TextPath(input));

// Then
console.Output.TrimEnd()
.ShouldBe(expected);
console.Output.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)
[Fact]
public void Should_Right_Align_Correctly()
{
// Given
var console = new TestConsole().Width(80);
var console = new TestConsole().Width(40);

// When
console.Write(new TextPath(input));
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightJustified());

// Then
console.Output.ShouldEndWith("\n");
console.Output.ShouldBe(" C:/My documents/Bar/Baz.txt");
}

[Fact]
public void Should_Right_Align_Correctly()
public void Should_Center_Align_Correctly()
{
// Given
var console = new TestConsole().Width(40);

// When
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightJustified());
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]
public void Should_Center_Align_Correctly()
[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 TextPath("C:/My documents/Bar/Baz.txt").Centered());
console.Write(
new Columns(
new Panel(new Text("Baz")),
new Panel(new TextPath("Qux"))));

// Then
console.Output.TrimEnd('\n')
.ShouldBe(" C:/My documents/Bar/Baz.txt ");
return Verifier.Verify(console.Output);
}
}
12 changes: 12 additions & 0 deletions test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 943c045

Please sign in to comment.