An interactive shell library for ReactPHP
Go to file
Christopher Vagnetoft fdbcc7adae Bugfixes, updated examples
* Added example of using the abort event
* Added setter/getter for allowAbbreviatedCommands
* Fixed SIGINT handling
2024-02-28 18:40:26 +01:00
examples Bugfixes, updated examples 2024-02-28 18:40:26 +01:00
src Bugfixes, updated examples 2024-02-28 18:40:26 +01:00
.gitignore Initial commit 2024-02-27 02:08:29 +01:00
composer.json Initial commit 2024-02-27 02:08:29 +01:00
LICENSE Initial commit 2024-02-27 02:08:29 +01:00
phpstan.neon Initial commit 2024-02-27 02:08:29 +01:00
README.md Updated readme and examples 2024-02-28 12:44:09 +01:00

An interactive Unicode-aware shell for ReactPHP

Installing

$ composer require noccylabs/react-shell:@dev

Features

  • Interactive line-editing including arrow keys, home, end, delete and all the commodities you are used to.
  • Automatic scrollback history, to avoid having to retype commands.
  • Input is edited on a single scrolling line.
  • Mostly Unicode-aware, meaning it will not have a nervous breakdown if you try to enter (single-width LTR) unicode characters.

Example

$shell = new NoccyLabs\React\Shell\Shell();

// Emitted whenever the prompt is fully redrawn, or redrawPrompt() is manually
// invoked. This is a good place to update a live prompt.
$shell->on('prompt', function () use ($shell) { 
    $shell->setPrompt(date("H:i:s> ")); 
});
// Emitted whenever a line is entered. The input is already parsed into tokens
// using str_getcsv, respecting double quotes and escape sequeneces.
$shell->on('input', function (array $input) use ($shell) {
    $shell->write("You entered: ".join(" ", $input)."\n");
});

// The shell is an OutputStream, so you can write to it! Doing so will hide the
// prompt, write the output, and then redraw the prompt. This means you don't
// have to care whether the prompt is visible as long as your output is written
// to the shell!
$shell->write("Hello World!\n\n");

// So, this works:
React\EventLoop\Loop::addPeriodicTimer(5, function () use ($shell) {
    $shell->write(date(DATE_ATOM)."\n");
});