diff --git a/Terminal.Gui/Core/Autocomplete/AppendAutocomplete.cs b/Terminal.Gui/Core/Autocomplete/AppendAutocomplete.cs index 256b48c6c..bb13626ab 100644 --- a/Terminal.Gui/Core/Autocomplete/AppendAutocomplete.cs +++ b/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); } diff --git a/UnitTests/Views/AppendAutocompleteTests.cs b/UnitTests/Views/AppendAutocompleteTests.cs index 3cb9c4d4d..5eca033d8 100644 --- a/UnitTests/Views/AppendAutocompleteTests.cs +++ b/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)]