Bugfixes, updated examples

* Added example of using the abort event
* Added setter/getter for allowAbbreviatedCommands
* Fixed SIGINT handling
This commit is contained in:
2024-02-28 18:40:26 +01:00
parent 24b7a79827
commit fdbcc7adae
4 changed files with 107 additions and 9 deletions

44
examples/aborting.php Normal file
View File

@ -0,0 +1,44 @@
<?php
use NoccyLabs\React\Shell\CommandHandler;
use React\EventLoop\Loop;
require_once __DIR__."/../vendor/autoload.php";
$operation = new class {
public bool $active = false;
};
$shell = new NoccyLabs\React\Shell\Shell();
// The prompt event is invoked before every full redraw. Call redrawPrompt()
// to trigger it manually. Set the prompt or the style here.
$shell->on('prompt', function ($shell) {
$shell->setPrompt(">> ");
});
// Input handler, parse the commands.
$shell->on('input', function ($args, $shell) use ($operation){
switch ($args[0]) {
case 'start':
$operation->active = true;
$shell->write("Operation started... Press Ctrl-C to abort\n");
break;
default:
$shell->write("Type start to start the imaginary operation, then ctrl-c to abort it. Press ctrl-c again to exit.\n");
}
});
// The abort event lets you keep going if a specific operation is the main
// context. For example, if you are copying a file ^C could stop the copy
// operation.
$shell->on('abort', function ($shell) use ($operation) {
if ($operation->active == true) {
$shell->write("Aborting operation\n");
$operation->active = false;
} else {
$shell->write("Exiting\n");
$shell->close();
}
});