Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
0e5a25567c | |||
bdec60717f | |||
43a6475192 |
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.
|
||||||
|
|
||||||
|
|
||||||
|
20
examples/catchall.php
Normal file
20
examples/catchall.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__."/../vendor/autoload.php";
|
||||||
|
|
||||||
|
use NoccyLabs\Shell\Shell;
|
||||||
|
use NoccyLabs\Shell\Context;
|
||||||
|
|
||||||
|
class CatchAllContext extends Context
|
||||||
|
{
|
||||||
|
public function execute($cmd, ...$arg)
|
||||||
|
{
|
||||||
|
printf("Executing: %s %s\n", $cmd, join(" ",$arg));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$myShell = new Shell();
|
||||||
|
$myShell->setPrompt("test>");
|
||||||
|
$myShell->pushContext(new CatchAllContext());
|
||||||
|
$myShell->run();
|
@ -146,6 +146,23 @@ class Context
|
|||||||
return array_key_exists('global', $info);
|
return array_key_exists('global', $info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catch-all handler for commands not defined in context, globally or builtin.
|
||||||
|
* Override this function and return true if the command is handled ok.
|
||||||
|
*
|
||||||
|
* @param string $command The command to execute
|
||||||
|
* @param string[] $args The arguments to the command
|
||||||
|
* @return bool True if the command was handled
|
||||||
|
*/
|
||||||
|
public function execute($command, ...$args)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the context
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
|
@ -161,22 +161,33 @@ class LineRead
|
|||||||
if ($this->posHistory == 0) {
|
if ($this->posHistory == 0) {
|
||||||
$this->stashedBuffer = $this->buffer;
|
$this->stashedBuffer = $this->buffer;
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == strlen($this->buffer)) {
|
||||||
|
$this->posCursor = -1;
|
||||||
|
}
|
||||||
if ($this->posHistory < count($this->history)) {
|
if ($this->posHistory < count($this->history)) {
|
||||||
$this->posHistory++;
|
$this->posHistory++;
|
||||||
$this->buffer = $this->history[$this->posHistory-1];
|
$this->buffer = $this->history[$this->posHistory-1];
|
||||||
$this->redraw();
|
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == -1) {
|
||||||
|
$this->posCursor = strlen($this->buffer);
|
||||||
|
}
|
||||||
|
$this->redraw();
|
||||||
break;
|
break;
|
||||||
case "\e[B": // down
|
case "\e[B": // down
|
||||||
|
if ($this->posCursor == strlen($this->buffer)) {
|
||||||
|
$this->posCursor = -1;
|
||||||
|
}
|
||||||
if ($this->posHistory > 1) {
|
if ($this->posHistory > 1) {
|
||||||
$this->posHistory--;
|
$this->posHistory--;
|
||||||
$this->buffer = $this->history[$this->posHistory-1];
|
$this->buffer = $this->history[$this->posHistory-1];
|
||||||
$this->redraw();
|
|
||||||
} elseif ($this->posHistory > 0) {
|
} elseif ($this->posHistory > 0) {
|
||||||
$this->posHistory--;
|
$this->posHistory--;
|
||||||
$this->buffer = $this->stashedBuffer;
|
$this->buffer = $this->stashedBuffer;
|
||||||
$this->redraw();
|
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == -1) {
|
||||||
|
$this->posCursor = strlen($this->buffer);
|
||||||
|
}
|
||||||
|
$this->redraw();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(STDERR, "\n%s\n", substr($code,1));
|
fprintf(STDERR, "\n%s\n", substr($code,1));
|
||||||
|
@ -223,6 +223,12 @@ class Shell
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call 'execute' on the current context
|
||||||
|
if ($this->context->execute($command, ...$args)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Throw error if the command could not be found
|
// Throw error if the command could not be found
|
||||||
throw new Exception\BadCommandException("Command {$command} not found");
|
throw new Exception\BadCommandException("Command {$command} not found");
|
||||||
}
|
}
|
||||||
@ -315,57 +321,62 @@ class Shell
|
|||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$this->lineReader = new LineRead();
|
try {
|
||||||
|
$this->lineReader = new LineRead();
|
||||||
|
|
||||||
$this->lineReader->setPromptText($this->prompt);
|
$this->lineReader->setPromptText($this->prompt);
|
||||||
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
||||||
$this->lineReader->setCommandStyle(new Style(Style::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) {
|
while ($this->running) {
|
||||||
// Update the input stuff, sleep if nothing to do.
|
// Update the input stuff, sleep if nothing to do.
|
||||||
if (!($buffer = $this->lineReader->update())) {
|
if (!($buffer = $this->lineReader->update())) {
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
}
|
}
|
||||||
// Escape is handy too...
|
// Escape is handy too...
|
||||||
if ($buffer == "\e") {
|
if ($buffer == "\e") {
|
||||||
$this->dispatchEvent("shell.abort");
|
$this->dispatchEvent("shell.abort");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// we get a ^C on ^C, so deal with the ^C.
|
// we get a ^C on ^C, so deal with the ^C.
|
||||||
if ($buffer == "\x03") {
|
if ($buffer == "\x03") {
|
||||||
$this->dispatchEvent("shell.stop");
|
$this->dispatchEvent("shell.stop");
|
||||||
$this->stop();
|
$this->stop();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Execute the buffer
|
// Execute the buffer
|
||||||
ob_start();
|
ob_start();
|
||||||
$this->dispatchEvent("update");
|
$this->dispatchEvent("update");
|
||||||
foreach ($this->timers as $timer) {
|
foreach ($this->timers as $timer) {
|
||||||
$timer->update();
|
$timer->update();
|
||||||
}
|
}
|
||||||
if ($buffer) {
|
if ($buffer) {
|
||||||
$this->executeBuffer($buffer);
|
$this->executeBuffer($buffer);
|
||||||
}
|
}
|
||||||
$output = ob_get_contents();
|
$output = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
if (trim($output)) {
|
if (trim($output)) {
|
||||||
$this->lineReader->erase();
|
$this->lineReader->erase();
|
||||||
echo rtrim($output)."\n";
|
echo rtrim($output)."\n";
|
||||||
$this->lineReader->redraw();
|
$this->lineReader->redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->context) {
|
||||||
|
$this->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($buffer) {
|
||||||
|
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->context) {
|
} catch (\Exception $e) {
|
||||||
$this->stop();
|
fprintf(STDERR, "\e[31;1mFatal: Unhandled exception\e[0m\n\n%s\n", $e);
|
||||||
}
|
|
||||||
|
|
||||||
if ($buffer) {
|
|
||||||
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->lineReader = null;
|
$this->lineReader = null;
|
||||||
|
Reference in New Issue
Block a user