|
21 | 21 | $readline->limitHistory($limit);
|
22 | 22 | }
|
23 | 23 |
|
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 |
| - |
34 | 24 | // autocomplete the following commands (at offset=0/1 only)
|
35 | 25 | $readline->setAutocomplete(function ($_, $offset) {
|
36 | 26 | return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
|
|
39 | 29 | $stdio->write('Welcome to this interactive demo' . PHP_EOL);
|
40 | 30 |
|
41 | 31 | // 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); |
44 | 43 |
|
45 | 44 | if (in_array(trim($line), array('quit', 'exit'))) {
|
46 | 45 | $stdio->end();
|
|
0 commit comments