88use React \Stream \Util ;
99use Clue \React \Utf8 \Sequencer as Utf8Sequencer ;
1010use Clue \React \Term \ControlCodeParser ;
11+ use Clue \React \Stdio \Readline \History ;
12+ use Clue \React \Stdio \Readline \MemoryHistory ;
1113
1214class Readline extends EventEmitter implements ReadableStreamInterface
1315{
@@ -17,18 +19,24 @@ class Readline extends EventEmitter implements ReadableStreamInterface
1719 private $ echo = true ;
1820 private $ autocomplete = null ;
1921 private $ move = true ;
20- private $ history = null ;
22+ private $ history ;
2123 private $ encoding = 'utf-8 ' ;
2224
2325 private $ input ;
2426 private $ output ;
2527 private $ sequencer ;
2628 private $ closed = false ;
2729
28- public function __construct (ReadableStreamInterface $ input , WritableStreamInterface $ output )
30+ public function __construct (ReadableStreamInterface $ input , WritableStreamInterface $ output, History $ history = null )
2931 {
3032 $ this ->input = $ input ;
33+
34+ if ($ history === null ) {
35+ $ history = new MemoryHistory ();
36+ }
37+
3138 $ this ->output = $ output ;
39+ $ this ->history = $ history ;
3240
3341 if (!$ this ->input ->isReadable ()) {
3442 return $ this ->close ();
@@ -311,21 +319,34 @@ public function getInput()
311319 }
312320
313321 /**
314- * set history handler to use (or none)
322+ * set history handler to use
315323 *
316324 * The history handler will be called whenever the user hits the UP or DOWN
317325 * arrow keys.
318326 *
319- * @param HistoryInterface|null $history
327+ * If you do not want to use history support, simply pass a `NullHistory` object.
328+ *
329+ * @param History $history new history handler to use
320330 * @return self
321331 */
322- public function setHistory (HistoryInterface $ history = null )
332+ public function setHistory (History $ history )
323333 {
324334 $ this ->history = $ history ;
325335
326336 return $ this ;
327337 }
328338
339+ /**
340+ * Gets the current history handler in use
341+ *
342+ * @return History
343+ * @see self::setHistory()
344+ */
345+ public function getHistory ()
346+ {
347+ return $ this ->history ;
348+ }
349+
329350 /**
330351 * set autocompletion handler to use (or none)
331352 *
@@ -468,17 +489,13 @@ public function onKeyRight()
468489 /** @internal */
469490 public function onKeyUp ()
470491 {
471- if ($ this ->history !== null ) {
472- $ this ->history ->up ();
473- }
492+ $ this ->history ->moveUp ($ this );
474493 }
475494
476495 /** @internal */
477496 public function onKeyDown ()
478497 {
479- if ($ this ->history !== null ) {
480- $ this ->history ->down ();
481- }
498+ $ this ->history ->moveDown ($ this );
482499 }
483500
484501 /**
@@ -548,9 +565,7 @@ protected function processLine()
548565 }
549566
550567 // process stored input buffer
551- if ($ this ->history !== null ) {
552- $ this ->history ->addLine ($ line );
553- }
568+ $ this ->history ->addLine ($ line );
554569 $ this ->emit ('data ' , array ($ line ));
555570 }
556571
0 commit comments