Skip to content
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
24 changes: 24 additions & 0 deletions TUI.Test/BorderElementRenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,28 @@
// Assert
Assert.IsEmpty(provider.WritesOf(" Title "));
}

/// <summary>
/// A title containing a line break stays on the top border row: no write carries the break to
/// the terminal, and each break is drawn as a space (ktsu-dev/TUI#151).
/// </summary>
/// <param name="title">The title under test.</param>
[TestMethod]
[DataRow("ab\ncd")]
[DataRow("ab\rcd")]
[DataRow("ab\r\ncd")]
public void TitleWithALineBreakIsDrawnOnOneRow(string title)
{
// Arrange
BorderElement element = CreateElement(width: 12, height: 4);
element.Title = title;
RecordingConsoleProvider provider = new();

// Act
element.Render(provider);

// Assert
Assert.IsFalse(provider.Writes.Any(w => w.Text.Contains('\r') || w.Text.Contains('\n')), "A write carried a line break");

Check warning on line 230 in TUI.Test/BorderElementRenderTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.DoesNotContain' instead of 'Assert.IsFalse'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TUI&issues=AaDxDI4nqUpns_kSOsNU&open=AaDxDI4nqUpns_kSOsNU&pullRequest=188
Assert.AreEqual(0, provider.WritesOf(" ab cd ").Single().Position.Y);
}
}
6 changes: 5 additions & 1 deletion TUI/Elements/Primitives/BorderElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ protected override void OnRender(IConsoleProvider provider)
if (!string.IsNullOrEmpty(Title) && dimensions.Width > 4)
{
int maxTitleWidth = dimensions.Width - 4; // Leave space for border and padding
string displayTitle = Title.Length > maxTitleWidth ? Title[..maxTitleWidth] : Title;

// A line break written to the terminal would move the rest of the title off the top
// border row and over the left edge, so each break becomes a space (ktsu-dev/TUI#151).
string singleLineTitle = Title.Replace("\r\n", " ", StringComparison.Ordinal).Replace('\r', ' ').Replace('\n', ' ');
string displayTitle = singleLineTitle.Length > maxTitleWidth ? singleLineTitle[..maxTitleWidth] : singleLineTitle;
string titleWithPadding = $" {displayTitle} ";

int titleX = TitleAlignment switch
Expand Down
Loading