Skip to content

Commit 1803cdb

Browse files
committed
Prefer Stdio as input stream instead of advanced Readline
1 parent 55462b6 commit 1803cdb

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ $readline = $stdio->getReadline();
154154
```
155155

156156
See above for waiting for user input.
157+
157158
Alternatively, the `Readline` is also a well-behaving readable stream
158159
(implementing ReactPHP's `ReadableStreamInterface`) that emits each complete
159-
line as a `data` event (without the trailing newline). This is considered
160-
advanced usage.
160+
line as a `data` event (without the trailing newline).
161+
This is considered advanced usage.
161162

162163
#### Prompt
163164

@@ -312,11 +313,12 @@ If you want to automatically add everything from the user input to the history,
312313
you may want to use something like this:
313314

314315
```php
315-
$readline->on('data', function ($line) use ($readline) {
316+
$stdio->on('data', function ($line) use ($readline) {
317+
$line = rtrim($line);
316318
$all = $readline->listHistory();
317319

318320
// skip empty line and duplicate of previous line
319-
if (trim($line) !== '' && $line !== end($all)) {
321+
if ($line !== '' && $line !== end($all)) {
320322
$readline->addHistory($line);
321323
}
322324
});

examples/02-interactive.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@
2121
$readline->limitHistory($limit);
2222
}
2323

24-
// add all lines from input to history
25-
$readline->on('data', function ($line) use ($readline) {
26-
$all = $readline->listHistory();
27-
28-
// skip empty line and duplicate of previous line
29-
if (trim($line) !== '' && $line !== end($all)) {
30-
$readline->addHistory($line);
31-
}
32-
});
33-
3424
// autocomplete the following commands (at offset=0/1 only)
3525
$readline->setAutocomplete(function ($_, $offset) {
3626
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
@@ -39,8 +29,17 @@
3929
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4030

4131
// react to commands the user entered
42-
$stdio->on('data', function ($line) use ($stdio) {
43-
$stdio->write('you just said: ' . addcslashes($line, "\0..\037") . ' (' . strlen($line) . ')' . PHP_EOL);
32+
$stdio->on('data', function ($line) use ($stdio, $readline) {
33+
$line = rtrim($line, "\r\n");
34+
35+
// add all lines from input to history
36+
// skip empty line and duplicate of previous line
37+
$all = $readline->listHistory();
38+
if ($line !== '' && $line !== end($all)) {
39+
$readline->addHistory($line);
40+
}
41+
42+
$stdio->write('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
4443

4544
if (in_array(trim($line), array('quit', 'exit'))) {
4645
$stdio->end();

0 commit comments

Comments
 (0)