Implemented contexts, optimizations
This commit is contained in:
27
examples/contexts.php
Normal file
27
examples/contexts.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
use NoccyLabs\Shell\Shell;
|
||||
use NoccyLabs\Shell\Context;
|
||||
|
||||
$shell = new Shell();
|
||||
$shell->addListener("prompt", function ($event, $shell) {
|
||||
$name = $shell->getContextPath();
|
||||
$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);
|
||||
|
||||
|
||||
$shell->run();
|
||||
|
@ -7,13 +7,9 @@ use NoccyLabs\Shell\Command;
|
||||
|
||||
class MyCommand extends Command
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return "mycommand";
|
||||
}
|
||||
public function execute()
|
||||
{
|
||||
$this->writeln("Executing command");
|
||||
echo "Executing command";
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,20 +18,19 @@ class MyShell extends Shell
|
||||
|
||||
protected $seq = 0;
|
||||
|
||||
protected function configure(array $config)
|
||||
protected function configure()
|
||||
{
|
||||
$this->addCommand("hello", function () {
|
||||
echo "world\n\rthis\n\ris\n\ra\ntest\n\r";
|
||||
$context = $this->createContext();
|
||||
$context->addCommand("hello", function () {
|
||||
echo "world\nthis\nis\na\ntest\n";
|
||||
});
|
||||
$this->addCommand(new MyCommand());
|
||||
$context->addCommand("mycommand", new MyCommand());
|
||||
$this->updatePrompt();
|
||||
}
|
||||
|
||||
protected function updatePrompt()
|
||||
{
|
||||
$this->setPrompt("test[{$this->seq}]: ");
|
||||
$fg = ($this->seq % 7) + 1;
|
||||
$this->setPromptStyle("3{$fg}");
|
||||
}
|
||||
|
||||
protected function onUpdate()
|
||||
|
Reference in New Issue
Block a user