Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
Chris | 08ab24d665 | |
Chris | fdd1814875 | |
Chris | 75c624520d | |
Chris | ec60970b5d | |
Chris | 7c76928c3b | |
Chris | ff9845e23e | |
Chris | e5b328b822 |
|
@ -36,9 +36,9 @@ $myShell->setPrompt("test>");
|
|||
|
||||
// Create an anonymous context and add a command
|
||||
$ctx = $myShell->createContext("root");
|
||||
$ctx->addCommand("hello", function () {
|
||||
echo "Hello World!\n";
|
||||
});
|
||||
$ctx->addCommand("hello", function ($who="World") {
|
||||
echo "Hello {$who}!\n";
|
||||
}, [ "help"=>"Say hello", "args"=>"who" ]);
|
||||
|
||||
// Run the shell
|
||||
$myShell->run();
|
||||
|
|
|
@ -6,21 +6,46 @@ use NoccyLabs\Shell\Shell;
|
|||
use NoccyLabs\Shell\Context;
|
||||
|
||||
$shell = new Shell();
|
||||
$shell->addListener("prompt", function ($event, $shell) {
|
||||
$name = $shell->getContextPath();
|
||||
$shell->setPrompt("shell{$name}> ");
|
||||
$shell->addListener(Shell::EVT_UPDATE_PROMPT, function ($event) {
|
||||
$name = $event->shell->getContextPath();
|
||||
$event->shell->setPrompt("shell{$name}> ");
|
||||
});
|
||||
|
||||
$root = new Context();
|
||||
$root->addCommand("test", function () {
|
||||
echo "It works!\n";
|
||||
});
|
||||
$root->addCommand("context", function ($name) {
|
||||
$context = new Context($name);
|
||||
$context->name = $name;
|
||||
return $context;
|
||||
});
|
||||
$shell->pushContext($root);
|
||||
class MyContext extends Context
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
// Remember to call the parent constructor if you want to use
|
||||
// the doccomment syntax to mark commands, as demonstrated
|
||||
// at the end of this class for the bar command.
|
||||
parent::__construct();
|
||||
$this->addCommand("foo",[$this,"foo"],[
|
||||
'help' => "Foo command"
|
||||
]);
|
||||
}
|
||||
|
||||
public function onEnter()
|
||||
{
|
||||
echo "Entering context!\n";
|
||||
}
|
||||
|
||||
public function foo()
|
||||
{
|
||||
echo "Foo!\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @command bar
|
||||
* @args
|
||||
* @help Bar command
|
||||
*/
|
||||
public function bar()
|
||||
{
|
||||
echo "Bar!\n";
|
||||
}
|
||||
}
|
||||
|
||||
$shell->pushContext(new MyContext());
|
||||
|
||||
|
||||
$shell->run();
|
||||
|
|
|
@ -66,6 +66,11 @@ class Context
|
|||
$this->findCommands();
|
||||
}
|
||||
|
||||
public function onEnter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected function findCommands()
|
||||
{
|
||||
$refl = new \ReflectionClass(get_called_class());
|
||||
|
|
|
@ -90,6 +90,16 @@ class Shell
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current context
|
||||
*
|
||||
* @return Context The current context
|
||||
*/
|
||||
public function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a new primary context, saving the previous contexts on a stack.
|
||||
*
|
||||
|
@ -104,6 +114,7 @@ class Shell
|
|||
$context->setShell($this);
|
||||
$this->context = $context;
|
||||
$this->dispatchEvent(self::EVT_CONTEXT_CHANGED);
|
||||
$context->onEnter();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -407,19 +418,30 @@ class Shell
|
|||
}
|
||||
}
|
||||
ksort($ghelp);
|
||||
printf("Commands in current context:\n");
|
||||
$_ = function($command,$args,$info) {
|
||||
printf(" \e[96m%s\e[0m \e[0;3m%s\e[0m \e[30G\e[36m%s\e[0m\n", $command, $args, $info);
|
||||
};
|
||||
printf("\e[1mCommands:\e[0m\n");
|
||||
foreach ($help as $command=>$info) {
|
||||
printf(" %-20s %s\n", $command, $info);
|
||||
if (strpos($command," ")!==false) {
|
||||
list($command,$args) = explode(" ",$command,2);
|
||||
} else $args=null;
|
||||
$_($command, $args,$info);
|
||||
}
|
||||
if (count($ghelp)) {
|
||||
printf("\nImported from parent contexts:\n");
|
||||
foreach ($ghelp as $command=>$info) {
|
||||
printf(" %-20s %s\n", $command, $info);
|
||||
printf("\e[1mCommands from parent contexts:\e[0m\n");
|
||||
if (strpos($command," ")!==false) {
|
||||
list($command,$args) = explode(" ",$command,2);
|
||||
} else $args=null;
|
||||
if (!array_key_exists($command,$ghelp)) continue;
|
||||
$_($command, $args,$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");
|
||||
printf("\e[1mGlobal commands:\e[0m\n");
|
||||
$_("exit", null, "Leave the shell");
|
||||
$_(".", null, "Show the context tree");
|
||||
$_("..", null, "Discard the current context and go to parent");
|
||||
break;
|
||||
case 'exit':
|
||||
$this->stop();
|
||||
|
|
Loading…
Reference in New Issue