From efa02de815de624144d27909f3c8cbb41a34008b Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 9 Jan 2023 19:55:56 +0000 Subject: [PATCH 1/2] Fixes #2282. HotKeyPos is causing black cell on center and right/bottom alignments. --- Terminal.Gui/Core/TextFormatter.cs | 12 ++++----- UnitTests/TestHelpers.cs | 10 ++++---- UnitTests/TextFormatterTests.cs | 41 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 56458b3e0..50fc9f5ae 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -1212,11 +1212,11 @@ namespace Terminal.Gui { if (isVertical) { var runesWidth = GetSumMaxCharWidth (Lines, line); x = bounds.Right - runesWidth; - CursorPosition = bounds.Width - runesWidth + hotKeyPos; + CursorPosition = bounds.Width - runesWidth + (hotKeyPos > -1 ? hotKeyPos : 0); } else { var runesWidth = GetTextWidth (ustring.Make (runes)); x = bounds.Right - runesWidth; - CursorPosition = bounds.Width - runesWidth + hotKeyPos; + CursorPosition = bounds.Width - runesWidth + (hotKeyPos > -1 ? hotKeyPos : 0); } } else if (textAlignment == TextAlignment.Left || textAlignment == TextAlignment.Justified) { if (isVertical) { @@ -1225,16 +1225,16 @@ namespace Terminal.Gui { } else { x = bounds.Left; } - CursorPosition = hotKeyPos; + CursorPosition = hotKeyPos > -1 ? hotKeyPos : 0; } else if (textAlignment == TextAlignment.Centered) { if (isVertical) { var runesWidth = GetSumMaxCharWidth (Lines, line); x = bounds.Left + line + ((bounds.Width - runesWidth) / 2); - CursorPosition = (bounds.Width - runesWidth) / 2 + hotKeyPos; + CursorPosition = (bounds.Width - runesWidth) / 2 + (hotKeyPos > -1 ? hotKeyPos : 0); } else { var runesWidth = GetTextWidth (ustring.Make (runes)); x = bounds.Left + (bounds.Width - runesWidth) / 2; - CursorPosition = (bounds.Width - runesWidth) / 2 + hotKeyPos; + CursorPosition = (bounds.Width - runesWidth) / 2 + (hotKeyPos > -1 ? hotKeyPos : 0); } } else { throw new ArgumentOutOfRangeException (); @@ -1291,7 +1291,7 @@ namespace Terminal.Gui { rune = runes [idx]; } } - if (idx == HotKeyPos) { + if (HotKeyPos > -1 && idx == HotKeyPos) { if ((isVertical && textVerticalAlignment == VerticalTextAlignment.Justified) || (!isVertical && textAlignment == TextAlignment.Justified)) { CursorPosition = idx - start; diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index ac13d56c0..13ceeb2c4 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -37,9 +37,9 @@ public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute { /// Only valid if is true. /// Only valid if == and is true. public AutoInitShutdownAttribute (bool autoInit = true, bool autoShutdown = true, - Type consoleDriverType = null, - bool useFakeClipboard = false, - bool fakeClipboardAlwaysThrowsNotSupportedException = false, + Type consoleDriverType = null, + bool useFakeClipboard = false, + bool fakeClipboardAlwaysThrowsNotSupportedException = false, bool fakeClipboardIsSupportedAlwaysTrue = false) { //Assert.True (autoInit == false && consoleDriverType == null); @@ -54,7 +54,7 @@ public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute { static bool _init = false; bool AutoInit { get; } - bool AutoShutdown { get; } + bool AutoShutdown { get; } Type DriverType; public override void Before (MethodInfo methodUnderTest) @@ -251,7 +251,7 @@ class TestHelpers { var match = expectedColors.Where (e => e.Value == val).ToList (); if (match.Count == 0) { - throw new Exception ($"Unexpected color {DescribeColor (val)} was used at row {r} and col {c} (indexes start at 0). Color value was {val} (expected colors were {string.Join (",", expectedColors.Select (c => c.Value))})"); + throw new Exception ($"Unexpected color {DescribeColor (val)} was used at row {r} and col {c} (indexes start at 0). Color value was {val} (expected colors were {string.Join (",", expectedColors.Select (c => DescribeColor (c.Value)))})"); } else if (match.Count > 1) { throw new ArgumentException ($"Bad value for expectedColors, {match.Count} Attributes had the same Value"); } diff --git a/UnitTests/TextFormatterTests.cs b/UnitTests/TextFormatterTests.cs index e2750439a..63e28290e 100644 --- a/UnitTests/TextFormatterTests.cs +++ b/UnitTests/TextFormatterTests.cs @@ -4259,5 +4259,46 @@ This TextFormatter (tf2) is rewritten. 0111000000 0000000000", expectedColors); } + + [Fact, AutoInitShutdown] + public void Colors_On_TextAlignment_Right_And_Bottom () + { + var labelRight = new Label ("Test") { + Width = 6, + Height = 1, + TextAlignment = TextAlignment.Right, + ColorScheme = Colors.Base + }; + var labelBottom = new Label ("Test", TextDirection.TopBottom_LeftRight) { + Y = 1, + Width = 1, + Height = 6, + VerticalTextAlignment = VerticalTextAlignment.Bottom, + ColorScheme = Colors.Base + }; + var top = Application.Top; + top.Add (labelRight, labelBottom); + + Application.Begin (top); + ((FakeDriver)Application.Driver).SetBufferSize (7, 7); + + TestHelpers.AssertDriverContentsWithFrameAre (@" + Test + + +T +e +s +t ", output); + + TestHelpers.AssertDriverColorsAre (@" +000000 +0 +0 +0 +0 +0 +0", new Attribute [] { Colors.Base.Normal }); + } } } \ No newline at end of file From 13c7e5911657f0bb8f57c518b3ee27af9198be79 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 15 Jan 2023 20:58:27 +0000 Subject: [PATCH 2/2] Fixes #2287. AssertDriverColorsAre is reporting wrong colors information. --- UnitTests/TestHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index 13ceeb2c4..b76da0a35 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -260,7 +260,7 @@ class TestHelpers { var userExpected = line [c]; if (colorUsed != userExpected) { - throw new Exception ($"Colors used did not match expected at row {r} and col {c} (indexes start at 0). Color index used was {DescribeColor (colorUsed)} but test expected {DescribeColor (userExpected)} (these are indexes into the expectedColors array)"); + throw new Exception ($"Colors used did not match expected at row {r} and col {c} (indexes start at 0). Color index used was {colorUsed} ({DescribeColor (val)}) but test expected {userExpected} ({DescribeColor (expectedColors [int.Parse (userExpected.ToString ())].Value)}) (these are indexes into the expectedColors array)"); } }