-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path02-interactive.php
41 lines (32 loc) · 1.17 KB
/
02-interactive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
require __DIR__ . '/../vendor/autoload.php';
$stdio = new Clue\React\Stdio\Stdio();
$stdio->setPrompt('> ');
// limit history to HISTSIZE env
$limit = getenv('HISTSIZE');
if ($limit === '' || $limit < 0) {
// empty string or negative value means unlimited
$stdio->limitHistory(null);
} elseif ($limit !== false) {
// apply any other value if given
$stdio->limitHistory($limit);
}
// autocomplete the following commands (at offset=0/1 only)
$stdio->setAutocomplete(function ($_, $offset) {
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
});
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
// react to commands the user entered
$stdio->on('data', function ($line) use ($stdio) {
$line = rtrim($line, "\r\n");
// add all lines from input to history
// skip empty line and duplicate of previous line
$all = $stdio->listHistory();
if ($line !== '' && $line !== end($all)) {
$stdio->addHistory($line);
}
$stdio->write('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
if (in_array(trim($line), array('quit', 'exit'))) {
$stdio->end();
}
});