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

View File

@ -35,9 +35,9 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
private int $promptWidth = 0;
private ?string $promptStyle = "36;1";
private ?string $promptStyle = "2";
private ?string $inputStyle = "36";
private ?string $inputStyle = "1";
private array $history = [];
@ -56,6 +56,18 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
Loop::futureTick($this->redrawPrompt(...));
Loop::addSignal(SIGINT, function () {
Loop::futureTick(function () {
if (count($this->listeners("abort")) == 0)
$this->end("Received ^C\n");
else
$this->emit("abort", [ $this ]);
});
});
// Loop::addSignal(SIGWINCH, function () {
// Loop::futureTick($this->redrawPrompt(...));
// });
// Save terminal settings and disable inupt buffering
$this->oldStty = exec("stty -g");
exec("stty -icanon min 1 time 0 -echo");
@ -77,8 +89,11 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
switch ($v) {
case "\x03":
exit(0);
case "\x03":
if (count($this->listeners("abort")) == 0)
$this->end("Received ^C\n");
else
$this->emit("abort", [ $this ]);
case "\n":
// Update the history
@ -152,7 +167,7 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
break;
default:
if (mb_strlen($v) == 1 && ord($v) >= 32) {
if (mb_strlen($v) == 1 && mb_ord($v) >= 32) {
$this->buffer = mb_substr($this->buffer, 0, $this->cursorPos) .
$v .
mb_substr($this->buffer, $this->cursorPos);
@ -188,12 +203,17 @@ class Shell implements WritableStreamInterface, EventEmitterInterface
public function end($data = null)
{
// NOP
$this->hidePrompt();
$this->ostream->write($data);
$this->emit('end', [ $this ]);
Loop::futureTick(fn() => exit(1));
}
public function close()
{
// NOP
$this->hidePrompt();
$this->emit('end', [ $this ]);
Loop::futureTick(fn() => exit(0));
}