Skip to content

Commit 85e861d

Browse files
committed
Do not automatically add user input to history
1 parent f40b5b2 commit 85e861d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,25 @@ UP and DOWN cursor keys on the keyboard.
291291
The history will start with an empty state, thus this feature is effectively
292292
disabled, 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+
294313
The `listHistory(): string[]` method can be used to
295314
return an array with all lines in the history.
296315
This will be an empty array until you add new entries via `addHistory()`.

examples/periodic.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
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

src/Readline.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)