forked from clue/reactphp-stdio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-cursor.php
More file actions
44 lines (31 loc) · 946 Bytes
/
05-cursor.php
File metadata and controls
44 lines (31 loc) · 946 Bytes
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
42
43
44
<?php
use Clue\React\Stdio\Stdio;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$value = 10;
$stdio->on("\033[A", function () use (&$value, $stdio) {
$value++;
$stdio->setPrompt('Value: ' . $value);
});
$stdio->on("\033[B", function () use (&$value, $stdio) {
--$value;
$stdio->setPrompt('Value: ' . $value);
});
// hijack enter to just print our current value
$stdio->on("\n", function () use ($stdio, &$value) {
$stdio->write("Your choice was $value\n");
});
// quit on "q"
$stdio->on('q', function () use ($stdio) {
$stdio->end();
});
// user can still type all keys, but we simply hide user input
$stdio->setEcho(false);
// instead of showing user input, we just show a custom prompt
$stdio->setPrompt('Value: ' . $value);
$stdio->write('Welcome to this cursor demo
Use cursor UP/DOWN to change value.
Use "q" to quit
');
$loop->run();