File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,21 @@ public function setMove($move)
110
110
return $ this ;
111
111
}
112
112
113
+ /**
114
+ * get current cursor position
115
+ *
116
+ * cursor position is measured in number of text characters
117
+ *
118
+ * @return int
119
+ * @see self::moveCursorTo() to move the cursor to a given position
120
+ * @see self::moveCursorBy() to move the cursor by given number of characters
121
+ * @see self::setMove() to toggle whether the user can move the cursor position
122
+ */
123
+ public function getCursorPosition ()
124
+ {
125
+ return $ this ->linepos ;
126
+ }
127
+
113
128
/**
114
129
* set current text input buffer
115
130
*
@@ -323,11 +338,12 @@ public function deleteChar($n)
323
338
* zero or out of range moves are simply ignored
324
339
*
325
340
* @param int $n
341
+ * @return self
326
342
* @uses self::moveCursorTo()
327
343
*/
328
344
public function moveCursorBy ($ n )
329
345
{
330
- $ this ->moveCursorTo ($ this ->linepos + $ n );
346
+ return $ this ->moveCursorTo ($ this ->linepos + $ n );
331
347
}
332
348
333
349
/**
@@ -336,6 +352,7 @@ public function moveCursorBy($n)
336
352
* out of range (exceeding current input buffer) are simply ignored
337
353
*
338
354
* @param int $n
355
+ * @return self
339
356
* @uses self::redraw()
340
357
*/
341
358
public function moveCursorTo ($ n )
@@ -346,6 +363,8 @@ public function moveCursorTo($n)
346
363
347
364
$ this ->linepos = $ n ;
348
365
$ this ->redraw ();
366
+
367
+ return $ this ;
349
368
}
350
369
351
370
/**
Original file line number Diff line number Diff line change @@ -20,11 +20,29 @@ public function testSettersReturnSelf()
20
20
public function testInputStartsEmpty ()
21
21
{
22
22
$ this ->assertEquals ('' , $ this ->readline ->getInput ());
23
+ $ this ->assertEquals (0 , $ this ->readline ->getCursorPosition ());
23
24
}
24
25
25
26
public function testGetInputAfterSetting ()
26
27
{
27
28
$ this ->assertSame ($ this ->readline , $ this ->readline ->setInput ('hello ' ));
28
29
$ this ->assertEquals ('hello ' , $ this ->readline ->getInput ());
30
+ $ this ->assertEquals (5 , $ this ->readline ->getCursorPosition ());
31
+ }
32
+
33
+ public function testSettingInputMovesCursorToEnd ()
34
+ {
35
+ $ this ->readline ->setInput ('hello ' );
36
+ $ this ->readline ->moveCursorTo (3 );
37
+
38
+ $ this ->readline ->setInput ('testing ' );
39
+ $ this ->assertEquals (7 , $ this ->readline ->getCursorPosition ());
40
+ }
41
+
42
+ public function testMultiByteInput ()
43
+ {
44
+ $ this ->readline ->setInput ('täst ' );
45
+ $ this ->assertEquals ('täst ' , $ this ->readline ->getInput ());
46
+ $ this ->assertEquals (4 , $ this ->readline ->getCursorPosition ());
29
47
}
30
48
}
You can’t perform that action at this time.
0 commit comments