Browse Source

Fix suggestion overspill bounds

Thomas 2 years ago
parent
commit
86a2a2a956

+ 14 - 1
Terminal.Gui/Core/Autocomplete/AppendAutocomplete.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.Linq;
@@ -75,6 +76,18 @@ namespace Terminal.Gui {
 
 			var suggestion = this.Suggestions.ElementAt (this.SelectedIdx);
 			var fragment = suggestion.Replacement.Substring (suggestion.Remove);
+
+			int spaceAvailable = textField.Bounds.Width - textField.Text.ConsoleWidth;
+			int spaceRequired = fragment.Sum(c=>Rune.ColumnWidth(c));
+
+			if(spaceAvailable < spaceRequired)
+			{
+				fragment = new string(
+					fragment.TakeWhile(c=> (spaceAvailable -= Rune.ColumnWidth(c)) >= 0)
+					.ToArray()
+				);
+			}
+
 			Application.Driver.AddStr (fragment);
 		}
 

+ 15 - 0
UnitTests/Views/AppendAutocompleteTests.cs

@@ -102,6 +102,21 @@ namespace Terminal.Gui.ViewTests {
         }
 
 
+		[Theory, AutoInitShutdown]
+		[InlineData("ffffffffffffffffffffffffff","ffffffffff")]
+		[InlineData("fffffffffff","ffffffffff")]
+        public void TestAutoAppendRendering_ShouldNotOverspill(string overspillUsing,string expectRender)
+        {
+			var tf = GetTextFieldsInViewSuggesting(overspillUsing);
+
+			// f is typed we should only see 'f' up to size of View (10)
+			Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
+			tf.Redraw(tf.Bounds);
+			TestHelpers.AssertDriverContentsAre(expectRender,output);
+			Assert.Equal("f",tf.Text.ToString());
+        }
+
+
 		[Theory, AutoInitShutdown]
 		[InlineData(ConsoleKey.UpArrow)]
 		[InlineData(ConsoleKey.DownArrow)]