3 Commits

4 changed files with 79 additions and 17 deletions

View File

@ -20,6 +20,29 @@ 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()
{

View File

@ -71,7 +71,7 @@ class Context
}, explode("\n", $docblock));
$info = [];
foreach ($lines as $line) {
if (preg_match("/^@(command|help|args) (.+?)$/", $line, $match)) {
if (preg_match("/^@(command|help|args|global)\\s*(.*)$/", $line, $match)) {
list($void,$key,$value) = $match;
$info[$key] = $value;
}
@ -132,6 +132,15 @@ class Context
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);
}
public function getName()
{
return $this->name;

View File

@ -68,7 +68,7 @@ class LineRead
$this->posCursor = strlen($this->buffer);
}
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
$cursor = strlen($this->prompt) + 2 + $this->posCursor - $this->posScroll;
$endStyle = "\e[0m";

View File

@ -19,6 +19,8 @@ class Shell
protected $timers = [];
protected $prompt = ">";
public function __construct()
{
$this->configure();
@ -162,11 +164,12 @@ class Shell
public function setPrompt($text)
{
if (!$this->lineReader) {
return;
}
$this->prompt = $text;
if ($this->lineReader) {
$this->lineReader->setPromptText($text);
}
}
/**
* Find a command and return a closure.
@ -176,12 +179,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)) {
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;
}
@ -236,11 +245,32 @@ class Shell
break;
case 'help':
$help = $this->context->getCommandHelp();
printf("Commands in current context:\n\n");
$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);
}
printf("\nGlobal commands:\n\n");
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;
@ -287,7 +317,7 @@ class Shell
{
$this->lineReader = new LineRead();
$this->lineReader->setPromptText("shell>");
$this->lineReader->setPromptText($this->prompt);
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
$this->lineReader->setCommandStyle(new Style(Style::GREEN));