Compare commits

...

5 Commits

Author SHA1 Message Date
Chris 08ab24d665 Bugfixes to help command 2017-01-30 12:10:23 +01:00
Chris fdd1814875 Moved onEnter event 2017-01-29 20:31:04 +01:00
Chris 75c624520d Updated examples, added onEnter method to contexts 2017-01-29 20:27:17 +01:00
Chris ec60970b5d Added getContext method to shell 2017-01-28 13:00:26 +01:00
Chris 7c76928c3b Even more tweaks 2017-01-27 04:43:44 +01:00
4 changed files with 67 additions and 23 deletions

View File

@ -36,9 +36,9 @@ $myShell->setPrompt("test>");
// Create an anonymous context and add a command // Create an anonymous context and add a command
$ctx = $myShell->createContext("root"); $ctx = $myShell->createContext("root");
$ctx->addCommand("hello", function () { $ctx->addCommand("hello", function ($who="World") {
echo "Hello World!\n"; echo "Hello {$who}!\n";
}); }, [ "help"=>"Say hello", "args"=>"who" ]);
// Run the shell // Run the shell
$myShell->run(); $myShell->run();

View File

@ -6,21 +6,46 @@ use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Context; use NoccyLabs\Shell\Context;
$shell = new Shell(); $shell = new Shell();
$shell->addListener("prompt", function ($event, $shell) { $shell->addListener(Shell::EVT_UPDATE_PROMPT, function ($event) {
$name = $shell->getContextPath(); $name = $event->shell->getContextPath();
$shell->setPrompt("shell{$name}> "); $event->shell->setPrompt("shell{$name}> ");
}); });
$root = new Context(); class MyContext extends Context
$root->addCommand("test", function () { {
echo "It works!\n"; public function __construct()
}); {
$root->addCommand("context", function ($name) { // Remember to call the parent constructor if you want to use
$context = new Context($name); // the doccomment syntax to mark commands, as demonstrated
$context->name = $name; // at the end of this class for the bar command.
return $context; parent::__construct();
}); $this->addCommand("foo",[$this,"foo"],[
$shell->pushContext($root); '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(); $shell->run();

View File

@ -66,6 +66,11 @@ class Context
$this->findCommands(); $this->findCommands();
} }
public function onEnter()
{
}
protected function findCommands() protected function findCommands()
{ {
$refl = new \ReflectionClass(get_called_class()); $refl = new \ReflectionClass(get_called_class());

View File

@ -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. * Push a new primary context, saving the previous contexts on a stack.
* *
@ -104,6 +114,7 @@ class Shell
$context->setShell($this); $context->setShell($this);
$this->context = $context; $this->context = $context;
$this->dispatchEvent(self::EVT_CONTEXT_CHANGED); $this->dispatchEvent(self::EVT_CONTEXT_CHANGED);
$context->onEnter();
} }
/** /**
@ -408,7 +419,7 @@ class Shell
} }
ksort($ghelp); ksort($ghelp);
$_ = function($command,$args,$info) { $_ = function($command,$args,$info) {
printf(" \e[97m%s\e[0m \e[90;3m%s\e[0m - \e[36m%s\e[0m\n", $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"); printf("\e[1mCommands:\e[0m\n");
foreach ($help as $command=>$info) { foreach ($help as $command=>$info) {
@ -418,13 +429,16 @@ class Shell
$_($command, $args,$info); $_($command, $args,$info);
} }
if (count($ghelp)) { if (count($ghelp)) {
printf("\n\e[0mCommands from parent contexts:\e[0m\n"); foreach ($ghelp as $command=>$info) {
if (strpos($command," ")!==false) { printf("\e[1mCommands from parent contexts:\e[0m\n");
list($command,$args) = explode(" ",$command,2); if (strpos($command," ")!==false) {
} else $args=null; list($command,$args) = explode(" ",$command,2);
$_($command, $args,$info); } else $args=null;
if (!array_key_exists($command,$ghelp)) continue;
$_($command, $args,$info);
}
} }
printf("\nGlobal commands:\n"); printf("\e[1mGlobal commands:\e[0m\n");
$_("exit", null, "Leave the shell"); $_("exit", null, "Leave the shell");
$_(".", null, "Show the context tree"); $_(".", null, "Show the context tree");
$_("..", null, "Discard the current context and go to parent"); $_("..", null, "Discard the current context and go to parent");