Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes TextPath rendering bugs #1308

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading