Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
b6727bed80 | |||
0e5a25567c | |||
bdec60717f |
12
README.md
12
README.md
@ -0,0 +1,12 @@
|
||||
NoccyLabs Shell Core
|
||||
====================
|
||||
|
||||
This library helps make elegant command line applications that spawn an isolated shell.
|
||||
It uses a standalone implementation for buffered input with support for arrow keys to
|
||||
navigate the history and more.
|
||||
|
||||
Note that this library requirements a fully ANSI compatible terminal with UTF-8 support
|
||||
in order to use colors, control the cursor position etc. As it uses `stty` to configure
|
||||
input buffering, it will likely not work on Windows.
|
||||
|
||||
|
||||
|
19
examples/errors.php
Normal file
19
examples/errors.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
use NoccyLabs\Shell\Shell;
|
||||
use NoccyLabs\Shell\Context;
|
||||
|
||||
class CatchAllContext extends Context
|
||||
{
|
||||
public function execute($cmd, ...$arg)
|
||||
{
|
||||
throw new \Exception("Uh-oh! Error!");
|
||||
}
|
||||
}
|
||||
|
||||
$myShell = new Shell();
|
||||
$myShell->setPrompt("test>");
|
||||
$myShell->pushContext(new CatchAllContext());
|
||||
$myShell->run();
|
@ -30,9 +30,15 @@ class MyShell extends Shell
|
||||
$this->pushContext($context);
|
||||
$this->updatePrompt();
|
||||
|
||||
$this->addTimer(5000, function () {
|
||||
$t1 = $this->addTimer(5000, function () {
|
||||
echo "5 seconds\n";
|
||||
});
|
||||
$app = $this;
|
||||
$t2 = $this->addTimer(15000, function () use ($t1, $app) {
|
||||
echo "Removing timers...\n";
|
||||
$app->removeTimer($t1);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected function updatePrompt()
|
||||
|
@ -161,22 +161,33 @@ class LineRead
|
||||
if ($this->posHistory == 0) {
|
||||
$this->stashedBuffer = $this->buffer;
|
||||
}
|
||||
if ($this->posCursor == strlen($this->buffer)) {
|
||||
$this->posCursor = -1;
|
||||
}
|
||||
if ($this->posHistory < count($this->history)) {
|
||||
$this->posHistory++;
|
||||
$this->buffer = $this->history[$this->posHistory-1];
|
||||
$this->redraw();
|
||||
}
|
||||
if ($this->posCursor == -1) {
|
||||
$this->posCursor = strlen($this->buffer);
|
||||
}
|
||||
$this->redraw();
|
||||
break;
|
||||
case "\e[B": // down
|
||||
if ($this->posCursor == strlen($this->buffer)) {
|
||||
$this->posCursor = -1;
|
||||
}
|
||||
if ($this->posHistory > 1) {
|
||||
$this->posHistory--;
|
||||
$this->buffer = $this->history[$this->posHistory-1];
|
||||
$this->redraw();
|
||||
} elseif ($this->posHistory > 0) {
|
||||
$this->posHistory--;
|
||||
$this->buffer = $this->stashedBuffer;
|
||||
$this->redraw();
|
||||
}
|
||||
if ($this->posCursor == -1) {
|
||||
$this->posCursor = strlen($this->buffer);
|
||||
}
|
||||
$this->redraw();
|
||||
break;
|
||||
default:
|
||||
fprintf(STDERR, "\n%s\n", substr($code,1));
|
||||
|
100
lib/Shell.php
100
lib/Shell.php
@ -137,6 +137,7 @@ class Shell
|
||||
private $userdata;
|
||||
public function __construct($interval, callable $handler, array $userdata) {
|
||||
$this->interval = $interval / 1000;
|
||||
$this->next = microtime(true) + $this->interval;
|
||||
$this->handler = $handler;
|
||||
$this->userdata = $userdata;
|
||||
}
|
||||
@ -157,9 +158,11 @@ class Shell
|
||||
*
|
||||
* @param Timer $timer
|
||||
*/
|
||||
public function removeTimer(Timer $timer)
|
||||
public function removeTimer($timer)
|
||||
{
|
||||
|
||||
$this->timers = array_filter($this->timers, function ($v) use ($timer) {
|
||||
return ($v !== $timer);
|
||||
});
|
||||
}
|
||||
|
||||
public function setPrompt($text)
|
||||
@ -321,57 +324,62 @@ class Shell
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->lineReader = new LineRead();
|
||||
try {
|
||||
$this->lineReader = new LineRead();
|
||||
|
||||
$this->lineReader->setPromptText($this->prompt);
|
||||
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
||||
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
|
||||
$this->lineReader->setPromptText($this->prompt);
|
||||
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
||||
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
|
||||
|
||||
$this->running = true;
|
||||
$this->running = true;
|
||||
|
||||
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
||||
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
||||
|
||||
while ($this->running) {
|
||||
// Update the input stuff, sleep if nothing to do.
|
||||
if (!($buffer = $this->lineReader->update())) {
|
||||
usleep(10000);
|
||||
}
|
||||
// Escape is handy too...
|
||||
if ($buffer == "\e") {
|
||||
$this->dispatchEvent("shell.abort");
|
||||
continue;
|
||||
}
|
||||
// we get a ^C on ^C, so deal with the ^C.
|
||||
if ($buffer == "\x03") {
|
||||
$this->dispatchEvent("shell.stop");
|
||||
$this->stop();
|
||||
continue;
|
||||
}
|
||||
// Execute the buffer
|
||||
ob_start();
|
||||
$this->dispatchEvent("update");
|
||||
foreach ($this->timers as $timer) {
|
||||
$timer->update();
|
||||
}
|
||||
if ($buffer) {
|
||||
$this->executeBuffer($buffer);
|
||||
}
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
while ($this->running) {
|
||||
// Update the input stuff, sleep if nothing to do.
|
||||
if (!($buffer = $this->lineReader->update())) {
|
||||
usleep(10000);
|
||||
}
|
||||
// Escape is handy too...
|
||||
if ($buffer == "\e") {
|
||||
$this->dispatchEvent("shell.abort");
|
||||
continue;
|
||||
}
|
||||
// we get a ^C on ^C, so deal with the ^C.
|
||||
if ($buffer == "\x03") {
|
||||
$this->dispatchEvent("shell.stop");
|
||||
$this->stop();
|
||||
continue;
|
||||
}
|
||||
// Execute the buffer
|
||||
ob_start();
|
||||
$this->dispatchEvent("update");
|
||||
foreach ($this->timers as $timer) {
|
||||
$timer->update();
|
||||
}
|
||||
if ($buffer) {
|
||||
$this->executeBuffer($buffer);
|
||||
}
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
if (trim($output)) {
|
||||
$this->lineReader->erase();
|
||||
echo rtrim($output)."\n";
|
||||
$this->lineReader->redraw();
|
||||
if (trim($output)) {
|
||||
$this->lineReader->erase();
|
||||
echo rtrim($output)."\n";
|
||||
$this->lineReader->redraw();
|
||||
}
|
||||
|
||||
if (!$this->context) {
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
if ($buffer) {
|
||||
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->context) {
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
if ($buffer) {
|
||||
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
fprintf(STDERR, "\e[31;1mFatal: Unhandled exception\e[0m\n\n%s\n", $e);
|
||||
}
|
||||
|
||||
$this->lineReader = null;
|
||||
|
Reference in New Issue
Block a user