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 #2002. Added feature to fill the remaining width with spaces. #2003

Merged
merged 6 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions Terminal.Gui/Core/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public List<ustring> Lines {
}

/// <summary>
/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute, Rect)"/> is called.
/// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute, Rect, bool)"/> is called.
/// If it is <c>false</c> when Draw is called, the Draw call will be faster.
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -1141,7 +1141,8 @@ public static ustring RemoveHotKeySpecifier (ustring text, int hotPos, Rune hotK
/// <param name="normalColor">The color to use for all text except the hotkey</param>
/// <param name="hotColor">The color to use to draw the hotkey</param>
/// <param name="containerBounds">Specifies the screen-relative location and maximum container size.</param>
public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect containerBounds = default)
/// <param name="fillRemaining">Determines if the bounds width will be used (default)or only the text width will be used.</param>
BDisp marked this conversation as resolved.
Show resolved Hide resolved
public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect containerBounds = default, bool fillRemaining = true)
{
// With this check, we protect against subclasses with overrides of Text (like Button)
if (ustring.IsNullOrEmpty (text)) {
Expand Down Expand Up @@ -1244,7 +1245,7 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
var size = isVertical ? bounds.Height : bounds.Width;
var current = start;
var savedClip = Application.Driver?.Clip;
if (Application.Driver != null && containerBounds != default) {
if (Application.Driver != null) {
Application.Driver.Clip = containerBounds == default
? bounds
: new Rect (Math.Max (containerBounds.X, bounds.X),
Expand All @@ -1254,10 +1255,10 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
}

for (var idx = (isVertical ? start - y : start - x); current < start + size; idx++) {
if (idx < 0) {
if (!fillRemaining && idx < 0) {
current++;
continue;
} else if (idx > runes.Length - 1) {
} else if (!fillRemaining && idx > runes.Length - 1) {
break;
}
var rune = (Rune)' ';
Expand Down Expand Up @@ -1289,7 +1290,8 @@ public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect c
} else {
current += runeWidth;
}
if (!isVertical && idx + 1 < runes.Length && current + Rune.ColumnWidth (runes [idx + 1]) > start + size) {
var nextRuneWidth = idx + 1 > -1 && idx + 1 < runes.Length ? Rune.ColumnWidth (runes [idx + 1]) : 0;
if (!isVertical && idx + 1 < runes.Length && current + nextRuneWidth > start + size) {
break;
}
}
Expand Down
29 changes: 29 additions & 0 deletions UnitTests/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3334,6 +3334,35 @@ public void Draw_Vertical_Wide_TextAlignments ()
Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
}

[Fact, AutoInitShutdown]
public void Draw_Fill_Remaining ()
{
var view = new View ("A very long text behind the TextFormatter.");

var tf = new TextFormatter ();
tf.Text = "Hello";
tf.Size = new Size (10, 1);

Application.Top.Add (view);
Application.Begin (Application.Top);

GraphViewTests.AssertDriverContentsWithFrameAre (@"
A very long text behind the TextFormatter.
", output);

tf.Draw (new Rect (0, 0, 10, 1), view.GetNormalColor (), view.ColorScheme.HotNormal, default, false);
Application.Top.Redraw (Application.Top.Bounds);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
Helloy long text behind the TextFormatter.
BDisp marked this conversation as resolved.
Show resolved Hide resolved
", output);

tf.Draw (new Rect (0, 0, 10, 1), view.GetNormalColor (), view.ColorScheme.HotNormal);
Application.Top.Redraw (Application.Top.Bounds);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
Hello g text behind the TextFormatter.
BDisp marked this conversation as resolved.
Show resolved Hide resolved
", output);
}

[Fact]
public void GetTextWidth_Simple_And_Wide_Runes ()
{
Expand Down