Skip to content

Commit 8f81c1f

Browse files
committed
Initial commit
0 parents  commit 8f81c1f

18 files changed

+1289
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Christian Lück
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "clue/stdio-react",
3+
"description": "Async standard console input & output (STDIN, STDOUT)",
4+
"require": {
5+
"react/event-loop": "0.3.*",
6+
"react/stream": "0.3.*"
7+
},
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Christian Lück",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"autoload": {
16+
"psr-0": { "Clue\\Stdio\\React": "src/" }
17+
}
18+
}

composer.lock

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/login.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Clue\Stdio\React\Stdio;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$stdio = new Stdio($loop);
10+
11+
$stdio->getReadline()->setPrompt('Username: ');
12+
13+
$first = true;
14+
$username = null;
15+
$password = null;
16+
17+
$stdio->on('line', function ($line) use ($stdio, &$first, &$username, &$password) {
18+
if ($first) {
19+
$stdio->getReadline()->setPrompt('Password: ');
20+
$stdio->getReadline()->setEcho(false);
21+
$username = $line;
22+
$first = false;
23+
} else {
24+
$password = $line;
25+
$stdio->end();
26+
}
27+
});
28+
29+
$loop->run();
30+
31+
echo <<<EOT
32+
33+
34+
Confirmation:
35+
---------------------
36+
Username: $username
37+
Password: $password
38+
39+
EOT;

example/periodic.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Clue\Stdio\React\Stdio;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$stdio = new Stdio($loop);
10+
11+
$stdio->getReadline()->setPrompt('> ');
12+
13+
$stdio->writeln('Will print periodic messages until you type "quit" or "exit"');
14+
15+
$stdio->on('line', function ($line) use ($stdio, $loop) {
16+
$stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
17+
18+
if ($line === 'quit' || $line === 'exit') {
19+
$loop->stop();
20+
}
21+
});
22+
23+
// add some periodic noise
24+
$loop->addPeriodicTimer(2.0, function () use ($stdio) {
25+
$stdio->writeln('hello');
26+
});
27+
28+
$loop->run();

example/progress.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Clue\Stdio\React\Stdio;
4+
use Clue\Stdio\React\Helper\ProgressBar;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = React\EventLoop\Factory::create();
9+
10+
$stdio = new Stdio($loop);
11+
$stdio->getInput()->close();
12+
13+
$stdio->writeln('Will print (fake) progress and then exit');
14+
15+
$progress = new ProgressBar($stdio);
16+
$progress->setMaximum(mt_rand(20, 200));
17+
18+
$loop->addPeriodicTimer(0.2, function ($timer) use ($stdio, $progress) {
19+
$progress->advance();
20+
21+
if ($progress->isComplete()) {
22+
$stdio->overwrite();
23+
$stdio->writeln("Finished processing nothing!");
24+
25+
$stdio->end();
26+
$timer->cancel();
27+
}
28+
});
29+
30+
$loop->run();

example/spinner.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Clue\Stdio\React\Stdio;
4+
use React\EventLoop\LoopInterface;
5+
use Clue\Stdio\React\Helper\Spinner;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = React\EventLoop\Factory::create();
10+
11+
$stdio = new Stdio($loop);
12+
13+
$stdio->getReadline()->setPrompt('> ');
14+
15+
$stdio->writeln('Will print a spinner until you enter something');
16+
17+
$stdio->write(' Processing...');
18+
19+
$spinner = new Spinner($loop, $stdio);
20+
21+
$stdio->on('line', function ($line) use ($stdio, &$tid, $loop, $spinner) {
22+
$stdio->overwrite('Processing... DONE');
23+
$stdio->getReadline()->setPrompt('');
24+
25+
$stdio->end();
26+
$spinner->pause();
27+
});
28+
29+
$loop->run();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Clue\Stdio\React;
4+
5+
class Autocomplete
6+
{
7+
public function run(Readline $readline)
8+
{
9+
10+
}
11+
12+
protected function getWord(Readline $readline)
13+
{
14+
return $readline->getBuffer();
15+
}
16+
}
17+
18+
class Words extends Autocomplete
19+
{
20+
private $words;
21+
22+
public function __construct(array $words)
23+
{
24+
$this->words = $words;
25+
}
26+
27+
public function run(Readline $readline)
28+
{
29+
30+
}
31+
}

0 commit comments

Comments
 (0)