<?php use Clue\React\Stdio\Stdio; use Clue\React\Stdio\Helper\ProgressBar; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $stdio = new Stdio($loop); $stdio->writeln('Will print (fake) progress and then exit'); $progress = new ProgressBar($stdio); $progress->setMaximum(mt_rand(20, 200)); $loop->addPeriodicTimer(0.1, function ($timer) use($stdio, $progress) { $progress->advance(); if ($progress->isComplete()) { $stdio->overwrite("Finished processing nothing!" . PHP_EOL); $stdio->end(); $timer->cancel(); } }); $loop->run();
<?php use Clue\React\Stdio\Stdio; use React\EventLoop\LoopInterface; use Clue\React\Stdio\Helper\Spinner; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $stdio = new Stdio($loop); $stdio->getReadline()->setPrompt('> '); $stdio->writeln('Will print a spinner until you enter something'); $stdio->write(' Processing...'); $spinner = new Spinner($loop, $stdio); $stdio->on('line', function ($line) use($stdio, &$tid, $loop, $spinner) { $stdio->overwrite('Processing... DONE'); $stdio->end(); $spinner->pause(); }); $loop->run();
use React\EventLoop\Factory; use React\Promise; $loop = Factory::create(); $myClientId = $argv[1]; // use this brutally from CLI params, it's just a demo $stdio = new Stdio($loop); (new Client($loop, ['host' => '192.168.33.99']))->connect()->then(function (Client $client) { return $client->channel(); })->then(function (Channel $channel) use($stdio, $loop, $myClientId) { // Set up I/O $stdio->getReadline()->setPrompt(sprintf('[%s] ', $myClientId)); $stdio->on('line', function ($line) use($loop, $channel, $myClientId) { if ($line === 'quit') { $loop->stop(); } $channel->publish($line, ['chatClientId' => $myClientId], 'bunny_chat_exchange'); }); return Promise\all([$channel, $channel->queueDeclare($myClientId, false, false, true, true), $channel->exchangeDeclare('bunny_chat_exchange', 'fanout'), $channel->queueBind($myClientId, 'bunny_chat_exchange')]); })->then(function (array $r) use($stdio, $myClientId) { /** @var Channel $channel */ $channel = $r[0]; return $channel->consume(function (Message $message, Channel $channel, Client $client) use($stdio, $myClientId) { $clientId = $message->getHeader('chatClientId'); // No local echo if ($clientId !== $myClientId) { $stdio->overwrite(sprintf("[%s] %s\n", $clientId, $message->content)); } $channel->ack($message); }, $myClientId); }); $loop->run();
public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndRestoreReadline() { $input = $this->getMock('React\\Stream\\ReadableStreamInterface'); $output = $this->getMock('React\\Stream\\WritableStreamInterface'); //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock(); $readline = new Readline($input, $output); $readline->setPrompt('> '); $readline->setInput('input'); $stdio = new Stdio($this->loop, $input, $output, $readline); $stdio->write("first\n"); $buffer = ''; $output->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use(&$buffer) { $buffer .= $data; })); $stdio->overwrite('overwrite'); $this->assertEquals("\r[K" . "overwrite\n" . "\r[K" . "> input", $buffer); }