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

Added TextPath.NoLineBreak flag #1178

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/Spectre.Console/Extensions/TextPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,20 @@ public static TextPath LeafColor(this TextPath obj, Color color)
{
return LeafStyle(obj, new Style(foreground: color));
}

/// <summary>
/// Sets the <see cref="TextPath.NoLineBreak"/> flag to true.
/// </summary>
/// <param name="obj">The path.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TextPath NoLineBreak(this TextPath obj)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

obj.NoLineBreak = true;
return obj;
}
}
13 changes: 11 additions & 2 deletions src/Spectre.Console/Widgets/TextPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public sealed class TextPath : IRenderable, IHasJustification
/// </summary>
public Justify? Justification { get; set; }

/// <summary>
/// Gets or sets if no line break should be rendered after the path.
/// </summary>
public bool? NoLineBreak { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="TextPath"/> class.
/// </summary>
Expand Down Expand Up @@ -84,6 +89,7 @@ public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
var separatorStyle = SeparatorStyle ?? Style.Plain;
var stemStyle = StemStyle ?? Style.Plain;
var leafStyle = LeafStyle ?? Style.Plain;
var lineBreak = NoLineBreak != true;

var fitted = Fit(options, maxWidth);
var parts = new List<Segment>();
Expand Down Expand Up @@ -119,8 +125,11 @@ public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
// Align the result
Aligner.Align(parts, Justification, maxWidth);

// Insert a line break
parts.Add(Segment.LineBreak);
// Insert a line break (if not omitted)
if (lineBreak)
{
parts.Add(Segment.LineBreak);
}

return parts;
}
Expand Down
18 changes: 18 additions & 0 deletions test/Spectre.Console.Tests/Unit/Widgets/TextPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ public void Should_Insert_Line_Break_At_End_Of_Path(string input)
console.Output.ShouldEndWith("\n");
}

[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_Not_Insert_Line_Break_At_End_Of_Path_If_No_Line_Break_Is_Set(string input)
{
// Given
var console = new TestConsole().Width(80);

// When
console.Write(new TextPath(input).NoLineBreak());

// Then
console.Output.ShouldNotEndWith("\n");
}

[Fact]
public void Should_Right_Align_Correctly()
{
Expand Down