18 Commits

Author SHA1 Message Date
b6727bed80 Fixed timer implementation 2017-01-22 23:22:13 +01:00
0e5a25567c Added exception handling to run() 2016-11-21 01:26:06 +01:00
bdec60717f Improved the cursor in LineRead when using history 2016-11-19 15:46:13 +01:00
43a6475192 Added an execute() method to context to catch unhandled commands 2016-11-19 14:18:53 +01:00
7bfd8453e7 Improved the context stack 2016-11-15 03:29:00 +01:00
482d8a54e5 Prompt can now be set before linereader is created 2016-11-12 16:37:51 +01:00
b2a23f992d Fixed an issue with the help command 2016-11-04 02:23:10 +01:00
455574b6a5 Fixed issue with global commands in parent contexts 2016-11-02 22:49:19 +01:00
46806e3d62 Bugfixes in lineread, implemented command history 2016-11-02 22:11:35 +01:00
427cac578a Implemented timers 2016-11-02 14:22:53 +01:00
ea09a15963 Ooops, bugfix. 2016-11-02 13:57:12 +01:00
fe27eeb4a3 Bugfixes and improvements to shell and context 2016-11-02 13:53:56 +01:00
a2c1148c52 Now shows context class with . 2016-11-01 16:27:23 +01:00
81dea747b2 Doccomment can now override context command name 2016-11-01 16:19:38 +01:00
ae17abb6c1 Improved context commands 2016-11-01 16:10:35 +01:00
01ee043bac The . builtin command now shows the stack too 2016-11-01 15:45:54 +01:00
bb74b56fc4 Armored getContextPath() 2016-11-01 15:32:53 +01:00
3cbf504aed Fixed bug in addCommands 2016-11-01 15:28:03 +01:00
11 changed files with 465 additions and 80 deletions

View File

@ -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.

12
examples/basic.php Normal file
View File

@ -0,0 +1,12 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Context;
$myShell = new Shell();
$myShell->setPrompt("test>");
$myShell->pushContext(new Context());
$myShell->run();

20
examples/catchall.php Normal file
View 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();

19
examples/errors.php Normal file
View 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();

View File

@ -4,6 +4,7 @@ require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
use NoccyLabs\Shell\Context;
class MyCommand extends Command
{
@ -13,6 +14,42 @@ class MyCommand extends Command
}
}
class MyContext extends Context
{
/**
* @command testme
* @args
* @help Useful test!
* @global
*/
public function test()
{
echo "Test\n";
}
/**
* @command context
* @help Create a new context
*/
public function context()
{
return new OtherContext("newcontext");
}
}
class OtherContext extends Context
{
/**
* @command other
* @args
* @help Other test
*/
public function test()
{
}
}
class MyShell extends Shell
{
@ -20,7 +57,8 @@ class MyShell extends Shell
protected function configure()
{
$context = $this->createContext();
$context = new MyContext();
$this->pushContext($context);
$context->addCommand("hello", function () {
echo "world\nthis\nis\na\ntest\n";
});

60
examples/timers.php Normal file
View File

@ -0,0 +1,60 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
use NoccyLabs\Shell\Context;
class MyContext extends Context
{
/**
* @command testme
* @args
* @help Useful test!
*/
public function test()
{
}
}
class MyShell extends Shell
{
protected $seq = 0;
protected function configure()
{
$context = new MyContext();
$this->pushContext($context);
$this->updatePrompt();
$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()
{
$this->setPrompt("test[{$this->seq}]: ");
}
protected function onCommand($buffer)
{
$this->seq++;
$this->updatePrompt();
parent::onCommand($buffer);
}
}
$myShell = new MyShell();
$myShell->run();
echo "Exiting\n";

View File

@ -1,14 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
class Hinter implement HinterInterface
{
public function getHints()
{
}
public function addHinter(HinterInterface $hinter)
{
}
}

View File

@ -1,7 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
interface HinterInterface
{
}

View File

@ -8,8 +8,13 @@ class Context
protected $commands = [];
protected $commandInfo = [];
protected $data = [];
protected $parent;
protected $shell;
public function __construct($name=null, array $data=[])
{
@ -18,14 +23,84 @@ class Context
$this->configure();
}
public function getContextInfo()
{
return null;
}
public function setShell(Shell $shell)
{
$this->shell = $shell;
}
public function getShell()
{
return $this->shell;
}
public function setParent(Context $parent=null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function getRoot()
{
if (!$this->parent) {
return $this;
}
$node = $this;
while ($parent = $node->getParent()) {
$node = $parent;
}
return $parent;
}
protected function configure()
{
// Override this to do setup stuff
$this->findCommands();
}
public function addCommand($command, callable $handler)
protected function findCommands()
{
$refl = new \ReflectionClass(get_called_class());
foreach ($refl->getMethods() as $method) {
$docblock = $method->getDocComment();
$lines = array_map(function ($line) {
return trim($line, "*/ \t");
}, explode("\n", $docblock));
$info = [];
foreach ($lines as $line) {
if (preg_match("/^@(command|help|args|global)\\s*(.*)$/", $line, $match)) {
list($void,$key,$value) = $match;
$info[$key] = $value;
}
if (count($info)>0) {
$cmdName = array_key_exists("command",$info)?$info["command"]:$method->getName();
$this->addCommand($cmdName, [$this, $method->getName()], $info);
}
}
}
}
public function addCommand($command, callable $handler, array $info=[])
{
$this->commands[$command] = $handler;
$this->commandInfo[$command] = $info;
ksort($this->commands);
}
public function setCommandHelp($command, $help)
{
if (!array_key_exists($command, $this->commandInfo)) {
return;
}
$this->commandInfo[$command]['help'] = $help;
}
public function addCommands(array $commands)
@ -36,7 +111,7 @@ class Context
$handler = [ $this,$handler ];
}
// Add the command to the command list
$this->addCommand($handler);
$this->addCommand($command, $handler);
}
}
@ -50,6 +125,44 @@ class Context
return $this->commands[$command];
}
public function getCommandHelp()
{
$ret = [];
foreach ($this->commands as $command=>$handler) {
$info = $this->commandInfo[$command];
$args = array_key_exists("args",$info)?$info['args']:"";
$help = array_key_exists("help",$info)?$info['help']:"";
$ret[trim("{$command} {$args}")] = $help;
}
return $ret;
}
public function isCommandGlobal($command)
{
if (strpos($command," ")!==false) {
list($command, $void) = explode(" ",$command,2);
}
$info = $this->commandInfo[$command];
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()
{
return $this->name;

View File

@ -16,6 +16,8 @@ class LineRead
protected $history = [];
protected $stashedBuffer = null;
protected $posHistory = 0;
protected $posCursor = 0;
@ -61,11 +63,16 @@ class LineRead
{
$prompt = $this->prompt;
$buffer = $this->buffer;
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
if ($this->posCursor > strlen($this->buffer)) {
$this->posCursor = strlen($this->buffer);
}
$cursor = strlen($this->prompt) + 2 + $this->posCursor - $this->posScroll;
$endStyle = "\e[0m";
fprintf(STDOUT, "\r\e[2K%s%s\e[%dG{$endStyle}", ($this->promptStyle)($prompt), ($this->commandStyle)($buffer), $cursor);
fprintf(STDOUT, "\r\e[2K%s %s\e[%dG{$endStyle}", ($this->promptStyle)($prompt), ($this->commandStyle)($buffer), $cursor);
}
protected function styleToAnsi($style)
@ -85,6 +92,10 @@ class LineRead
$returnBuffer = null;
while (strlen($keyBuffer)>0) {
if ($keyBuffer[0] == "\e") {
if (strlen($keyBuffer)==1) {
$keyBuffer = "";
return "\e";
}
if ($keyBuffer[1] == "[") {
$ctrlChar = substr($keyBuffer, 0,3);
$keyBuffer = substr($keyBuffer, 3);
@ -113,6 +124,7 @@ class LineRead
}
} elseif ($keyCode == 13) {
$returnBuffer = $this->buffer;
array_unshift($this->history, $this->buffer);
$this->buffer = null;
$this->posCursor = 0;
printf("\n\r");
@ -146,7 +158,36 @@ class LineRead
$this->redraw();
break;
case "\e[A": // up
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];
}
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];
} elseif ($this->posHistory > 0) {
$this->posHistory--;
$this->buffer = $this->stashedBuffer;
}
if ($this->posCursor == -1) {
$this->posCursor = strlen($this->buffer);
}
$this->redraw();
break;
default:
fprintf(STDERR, "\n%s\n", substr($code,1));

View File

@ -17,6 +17,10 @@ class Shell
protected $listeners = [];
protected $timers = [];
protected $prompt = ">";
public function __construct()
{
$this->configure();
@ -35,8 +39,10 @@ class Shell
public function pushContext(Context $context)
{
if ($this->context) {
$context->setParent($this->context);
array_unshift($this->contextStack, $this->context);
}
$context->setShell($this);
$this->context = $context;
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
}
@ -60,10 +66,17 @@ class Shell
public function getContextPath($separator=":")
{
// Return null if we don't have a current context
if (!$this->context)
return null;
// Assemble the contexts to walk
$stack = [ $this->context->getName() ];
foreach ($this->contextStack as $context) {
$stack[] = $context->getName();
}
// Reverse the order to make it more logical
$stack = array_reverse($stack);
return join($separator,$stack);
}
@ -117,7 +130,27 @@ class Shell
*/
public function addTimer($interval, callable $handler, array $userdata=[])
{
$timer = new class($interval, $handler, $userdata) {
private $next;
private $interval;
private $handler;
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;
}
public function update() {
$now = microtime(true);
if ($now > $this->next) {
$this->next = $now + $this->interval;
call_user_func($this->handler, $this->userdata);
}
}
};
$this->timers[] = $timer;
return $timer;
}
/**
@ -125,17 +158,20 @@ 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)
{
if (!$this->lineReader) {
return;
$this->prompt = $text;
if ($this->lineReader) {
$this->lineReader->setPromptText($text);
}
$this->lineReader->setPromptText($text);
}
/**
@ -146,12 +182,18 @@ class Shell
private function findCommand($command)
{
// Go over current context and walk through stack until finding command
foreach(array_merge([ $this->context ] , $this->contextStack) as $context) {
if ($context->hasCommand($command)) {
$handler = $context->getCommand($command);
break;
if ($this->context->hasCommand($command)) {
$handler = $this->context->getCommand($command);
} else {
foreach($this->contextStack as $context) {
if ($context->hasCommand($command) && $context->isCommandGlobal($command)) {
$handler = $context->getCommand($command);
break;
}
}
}
// No handler...
if (empty($handler)) {
return false;
}
@ -166,7 +208,7 @@ class Shell
* Execute a command with arguments.
*
* @param string $command The command name to execute
* @param string.. $args Arguments
* @param string $args Arguments
* @return mixed
* @throws Exception\BadCommandExcception
*/
@ -184,6 +226,12 @@ class Shell
}
return;
}
// Call 'execute' on the current context
if ($this->context->execute($command, ...$args)) {
return;
}
// Throw error if the command could not be found
throw new Exception\BadCommandException("Command {$command} not found");
}
@ -192,19 +240,48 @@ class Shell
{
switch ($command) {
case '.':
printf("context<%s>:\n", $this->context->getName());
echo " ".join("\n ",explode("\n",json_encode($this->context->getData(),JSON_PRETTY_PRINT)))."\n";
$type = basename(strtr(get_class($this->context), "\\", "/"));
printf("%s<%s>: %s\n", $type, $this->context->getName(), $this->context->getContextInfo());
$level = 0;
foreach ($this->contextStack as $context) {
$type = basename(strtr(get_class($context), "\\", "/"));
printf(" %s└─%s<%s>: %s\n", str_repeat(" ",$level++), $type, $context->getName(), $context->getContextInfo());
}
break;
case '..':
if (count($this->contextStack)>0)
$this->popContext();
break;
case 'help':
echo
"Built in commands:\n".
" . Show current context\n".
" .. Go to parent context\n".
" exit Exit the shell\n";
$help = $this->context->getCommandHelp();
$ghelp = [];
foreach ($this->contextStack as $context) {
$commands = $context->getCommandHelp();
foreach ($commands as $command=>$info) {
if (strpos(" ",$command)!==false) {
list ($cmd,$arg)=explode(" ",$command,2);
} else {
$cmd = $command;
}
if ($context->isCommandGlobal($cmd)) {
$ghelp[$command] = $info;
}
}
}
ksort($ghelp);
printf("Commands in current context:\n");
foreach ($help as $command=>$info) {
printf(" %-20s %s\n", $command, $info);
}
if (count($ghelp)) {
printf("\nImported from parent contexts:\n");
foreach ($ghelp as $command=>$info) {
printf(" %-20s %s\n", $command, $info);
}
}
printf("\nGlobal commands:\n");
printf(" %-20s %s\n", "exit", "Leave the shell");
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
break;
case 'exit':
$this->stop();
@ -247,48 +324,62 @@ class Shell
public function run()
{
$this->lineReader = new LineRead();
try {
$this->lineReader = new LineRead();
$this->lineReader->setPromptText("shell>");
$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);
}
// we get a ^C on ^C, so deal with the ^C.
if ($buffer == "\x03") {
$this->stop();
continue;
}
// Execute the buffer
ob_start();
$this->dispatchEvent("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;