File tree Expand file tree Collapse file tree 3 files changed +31
-4
lines changed
Expand file tree Collapse file tree 3 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -291,6 +291,25 @@ UP and DOWN cursor keys on the keyboard.
291291The history will start with an empty state, thus this feature is effectively
292292disabled, as the UP and DOWN cursor keys have no function then.
293293
294+ Note that the history is not maintained automatically.
295+ Any input the user submits by hitting enter will * not* be added to the history
296+ automatically.
297+ This may seem inconvenient at first, but it actually gives you more control over
298+ what (and when) lines should be added to the history.
299+ If you want to automatically add everything from the user input to the history,
300+ you may want to use something like this:
301+
302+ ``` php
303+ $readline->on('data', function ($line) use ($readline) {
304+ $all = $readline->listHistory();
305+
306+ // skip empty line and duplicate of previous line
307+ if (trim($line) !== '' && $line !== end($all)) {
308+ $readline->addHistory($line);
309+ }
310+ });
311+ ```
312+
294313The ` listHistory(): string[] ` method can be used to
295314return an array with all lines in the history.
296315This will be an empty array until you add new entries via ` addHistory() ` .
Original file line number Diff line number Diff line change 77$ loop = React \EventLoop \Factory::create ();
88
99$ stdio = new Stdio ($ loop );
10+ $ readline = $ stdio ->getReadline ();
1011
11- $ stdio ->getReadline ()->setPrompt ('> ' );
12+ $ readline ->setPrompt ('> ' );
13+
14+ // add all lines from input to history
15+ $ readline ->on ('data ' , function ($ line ) use ($ readline ) {
16+ $ all = $ readline ->listHistory ();
17+
18+ // skip empty line and duplicate of previous line
19+ if (trim ($ line ) !== '' && $ line !== end ($ all )) {
20+ $ readline ->addHistory ($ line );
21+ }
22+ });
1223
1324$ stdio ->writeln ('Will print periodic messages until you type "quit" or "exit" ' );
1425
Original file line number Diff line number Diff line change @@ -599,9 +599,6 @@ protected function processLine()
599599 }
600600
601601 // process stored input buffer
602- if ($ line !== '' ) {
603- $ this ->addHistory ($ line );
604- }
605602 $ this ->emit ('data ' , array ($ line ));
606603 }
607604
You can’t perform that action at this time.
0 commit comments