PHP code example of clue / stdio-react

1. Go to this page and download the library: Download clue/stdio-react library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clue / stdio-react example snippets




o = new Clue\React\Stdio\Stdio();
$stdio->setPrompt('Input > ');

$stdio->on('data', function ($line) use ($stdio) {
    $line = rtrim($line, "\r\n");
    $stdio->write('Your input: ' . $line . PHP_EOL);

    if ($line === 'quit') {
        $stdio->end();
    }
});

$stdio = new Clue\React\Stdio\Stdio();

$stdio->write('hello');
$stdio->write(" world\n");

$logger->pipe($stdio);

$stdio->on('data', function ($line) {
    if ($line === "start\n") {
        doSomething();
    }
});

$stdio->on('data', function ($line) {
    $line = rtrim($line, "\r\n");
    if ($line === "start") {
        doSomething();
    }
});

$stdio->pipe($logger);

$stdio->setPrompt('Input: ');

$stdio->setPrompt('');

assert($stdio->getPrompt() === '');

$stdio->setEcho(false);

$stdio->setEcho('*');

$stdio->setEcho(true);

$stdio->addInput('hello');

$stdio->setInput('lastpass');

$buffer = $stdio->getInput();

$stdio->setMove(false);

$stdio->setMove(true);

$position = $stdio->getCursorPosition();

$cell = $stdio->getCursorCell();

$stdio->moveCursorTo(0);

$stdio->moveCursorBy(-1);

$stdio->on('data', function ($line) use ($stdio) {
    $line = rtrim($line);
    $all = $stdio->listHistory();

    // skip empty line and duplicate of previous line
    if ($line !== '' && $line !== end($all)) {
        $stdio->addHistory($line);
    }
});

$list = $stdio->listHistory();

assert(count($list) === 0);

$stdio->addHistory('a');
$stdio->addHistory('b');

$list = $stdio->listHistory();
assert($list === array('a', 'b'));

$stdio->clearHistory();

$list = $stdio->listHistory();
assert(count($list) === 0);

$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);
}

$stdio->setAutocomplete(function () {
    return array(
        'exit',
        'echo',
        'help',
    );
});

> e [TAB]
exit  echo
> e

$stdio->setAutocomplete(function ($word, $offset) {
    if ($offset <= 1) {
        // autocomplete root commands at offset=0/1 only
        return array('cat', 'rm', 'stat');
    } else {
        // autocomplete all command arguments as glob pattern
        return glob($word . '*', GLOB_MARK);
    }
});

$stdio->setAutocomplete(function () use ($stdio) {
    if ($stdio->getInput() === 'run') {
        $stdio->setInput('run --test --value=42');
        $stdio->moveCursorBy(-2);
    }

    // return empty array so normal autocompletion doesn't kick in
    return array();
});

$stdio->setAutocomplete(null);

$stdio->on('?', function () use ($stdio) {
     $stdio->write('Here\'s some help: …' . PHP_EOL);
});

$stdio->on('ä', function () use ($stdio) {
     $stdio->addInput('a');
});

$stdio->on("\033[A", function () use ($stdio) {
     $stdio->setInput(strtoupper($stdio->getInput()));
});

$stdio->setBell(false);

// deprecated
$readline = $stdio->getReadline();

// deprecated
$readline->setPrompt('> ');

// new
$stdio->setPrompt('> ');

// echo 'hello world!' . PHP_EOL;
$stdio->write('hello world!' . PHP_EOL);

ob_start(function ($chunk) use ($stdio) {
    // forward write event to Stdio instead
    $stdio->write($chunk);

    // discard data from normal output handling
    return '';
}, 1);