浏览代码

Fixes #4442. TextField PositionCursor doesn't treat zero width as one column (#4443)

* Fixes #4442. TextField PositionCursor doesn't treat zero width as one column

* Each character must return at least one column, with the exception of Tab.

* Add unit test for the ScrollOffset
BDisp 1 周之前
父节点
当前提交
241aec0e3d
共有 2 个文件被更改,包括 44 次插入1 次删除
  1. 2 1
      Terminal.Gui/Views/TextInput/TextField.cs
  2. 42 0
      Tests/UnitTestsParallelizable/Views/TextFieldTests.cs

+ 2 - 1
Terminal.Gui/Views/TextInput/TextField.cs

@@ -1118,7 +1118,8 @@ public class TextField : View, IDesignable
                 break;
             }
 
-            int cols = _text [idx].GetColumns ();
+            int cols = Math.Max (_text [idx].GetColumns (), 1);
+
             TextModel.SetCol (ref col, Viewport.Width - 1, cols);
         }
 

+ 42 - 0
Tests/UnitTestsParallelizable/Views/TextFieldTests.cs

@@ -632,4 +632,46 @@ public class TextFieldTests (ITestOutputHelper output) : FakeDriverBase
             return sb.ToString ();
         }
     }
+
+    [Fact]
+    public void PositionCursor_Treat_Zero_Width_As_One_Column ()
+    {
+        IDriver driver = CreateFakeDriver ();
+
+        TextField tf = new () { Width = 10, Text = "\u001B[" };
+        tf.Driver = driver;
+        tf.SetRelativeLayout (new (10, 1));
+
+        Assert.Equal (0, tf.CursorPosition);
+
+        tf.CursorPosition = 1;
+        Assert.Equal (new Point (1, 0), tf.PositionCursor ());
+
+        tf.CursorPosition = 2;
+        Assert.Equal (new Point (2, 0), tf.PositionCursor ());
+    }
+
+    [Fact]
+    public void ScrollOffset_Treat_Negative_Width_As_One_Column ()
+    {
+        View view = new () { Width = 10, Height = 1};
+        TextField tf = new () { Width = 2, Text = "\u001B[" };
+        view.Add (tf);
+        tf.SetRelativeLayout (new (10, 1));
+
+        Assert.Equal (0, tf.ScrollOffset);
+        Assert.Equal (0, tf.CursorPosition);
+
+        Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
+        Assert.Equal (0, tf.ScrollOffset);
+        Assert.Equal (1, tf.CursorPosition);
+
+        Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
+        Assert.Equal (1, tf.ScrollOffset);
+        Assert.Equal (2, tf.CursorPosition);
+
+        Assert.False (tf.NewKeyDownEvent (Key.CursorRight));
+        Assert.Equal (1, tf.ScrollOffset);
+        Assert.Equal (2, tf.CursorPosition);
+    }
 }