From 943c045fab7eda2c27e4ce345d8edec48f37bf37 Mon Sep 17 00:00:00 2001 From: Patrik Svensson Date: Mon, 18 Sep 2023 08:04:57 +0200 Subject: [PATCH] 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 --- src/Spectre.Console/Widgets/TextPath.cs | 9 ++-- .../TextPath/GH-1307.Output.verified.txt | 3 ++ .../Unit/Widgets/TextPathTests.cs | 53 +++++++++---------- .../Utilities/GitHubIssueAttribute.cs | 12 +++++ 4 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 test/Spectre.Console.Tests/Expectations/Widgets/TextPath/GH-1307.Output.verified.txt create mode 100644 test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs diff --git a/src/Spectre.Console/Widgets/TextPath.cs b/src/Spectre.Console/Widgets/TextPath.cs index db5eafc..fafafb7 100644 --- a/src/Spectre.Console/Widgets/TextPath.cs +++ b/src/Spectre.Console/Widgets/TextPath.cs @@ -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)); } /// @@ -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 diff --git a/test/Spectre.Console.Tests/Expectations/Widgets/TextPath/GH-1307.Output.verified.txt b/test/Spectre.Console.Tests/Expectations/Widgets/TextPath/GH-1307.Output.verified.txt new file mode 100644 index 0000000..06cfb5f --- /dev/null +++ b/test/Spectre.Console.Tests/Expectations/Widgets/TextPath/GH-1307.Output.verified.txt @@ -0,0 +1,3 @@ +┌─────┐ ┌─────┐ +│ Baz │ │ Qux │ +└─────┘ └─────┘ diff --git a/test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs b/test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs index d7e5513..0dda742 100644 --- a/test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs +++ b/test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs @@ -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); } } diff --git a/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs b/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs new file mode 100644 index 0000000..d7d30a7 --- /dev/null +++ b/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs @@ -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; + } +} \ No newline at end of file