react-shell/examples/aborting.php

45 lines
1.3 KiB
PHP
Raw Normal View History

<?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();
}
});