react-shell/README.md

1.4 KiB

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