Skip to content

Commit b997dec

Browse files
committed
Merge pull request #22 from clue-labs/home
Add support for HOME and END keys
2 parents 17bb319 + 6056c0a commit b997dec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Readline.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ class Readline extends EventEmitter
1515
const ESC_RIGHT = "C";
1616
const ESC_UP = "A";
1717
const ESC_DOWN = "B";
18-
const ESC_DEL = "3~";
18+
const ESC_HOME = "1~";
1919
const ESC_INS = "2~";
20+
const ESC_DEL = "3~";
21+
const ESC_END = "4~";
2022

2123
const ESC_F10 = "20~";
2224

@@ -45,8 +47,10 @@ public function __construct($output)
4547
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_RIGHT, array($this, 'onKeyRight'));
4648
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_UP, array($this, 'onKeyUp'));
4749
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_DOWN, array($this, 'onKeyDown'));
48-
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_DEL, array($this, 'onKeyDelete'));
50+
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_HOME, array($this, 'onKeyHome'));
4951
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_INS, array($this, 'onKeyInsert'));
52+
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_DEL, array($this, 'onKeyDelete'));
53+
$this->sequencer->addSequence(self::ESC_SEQUENCE . self::ESC_END, array($this, 'onKeyEnd'));
5054

5155
$expect = 0;
5256
$char = '';
@@ -417,6 +421,18 @@ public function onKeyInsert()
417421
// TODO: toggle insert mode
418422
}
419423

424+
/** @internal */
425+
public function onKeyHome()
426+
{
427+
$this->moveCursorTo(0);
428+
}
429+
430+
/** @internal */
431+
public function onKeyEnd()
432+
{
433+
$this->moveCursorTo($this->strlen($this->linebuffer));
434+
}
435+
420436
/** @internal */
421437
public function onKeyTab()
422438
{

tests/ReadlineTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ public function testWriteMultiByteCharWritesOnce()
173173
$this->pushInputBytes($this->readline, "\xF0\x9D\x84\x9E");
174174
}
175175

176+
public function testKeysHomeMovesToFront()
177+
{
178+
$this->readline->setInput('test');
179+
$this->readline->onKeyHome();
180+
181+
$this->assertEquals(0, $this->readline->getCursorPosition());
182+
183+
return $this->readline;
184+
}
185+
186+
/**
187+
* @depends testKeysHomeMovesToFront
188+
* @param Readline $readline
189+
*/
190+
public function testKeysEndMovesToEnd(Readline $readline)
191+
{
192+
$readline->onKeyEnd();
193+
194+
$this->assertEquals(4, $readline->getCursorPosition());
195+
}
196+
176197
public function testKeysSimpleChars()
177198
{
178199
$this->pushInputBytes($this->readline, 'hi!');

0 commit comments

Comments
 (0)