Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
482d8a54e5 | |||
b2a23f992d | |||
455574b6a5 | |||
46806e3d62 | |||
427cac578a | |||
ea09a15963 | |||
fe27eeb4a3 | |||
a2c1148c52 |
@ -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()
|
||||
{
|
||||
|
54
examples/timers.php
Normal file
54
examples/timers.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?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();
|
||||
|
||||
$this->addTimer(5000, function () {
|
||||
echo "5 seconds\n";
|
||||
});
|
||||
}
|
||||
|
||||
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";
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Shell\AutoComplete;
|
||||
|
||||
class Hinter implement HinterInterface
|
||||
{
|
||||
public function getHints()
|
||||
{
|
||||
}
|
||||
|
||||
public function addHinter(HinterInterface $hinter)
|
||||
{
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Shell\AutoComplete;
|
||||
|
||||
interface HinterInterface
|
||||
{
|
||||
}
|
@ -12,6 +12,9 @@ class Context
|
||||
|
||||
protected $data = [];
|
||||
|
||||
protected $parent;
|
||||
|
||||
protected $shell;
|
||||
|
||||
public function __construct($name=null, array $data=[])
|
||||
{
|
||||
@ -20,6 +23,38 @@ class Context
|
||||
$this->configure();
|
||||
}
|
||||
|
||||
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
|
||||
@ -36,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;
|
||||
}
|
||||
@ -52,6 +87,7 @@ class Context
|
||||
{
|
||||
$this->commands[$command] = $handler;
|
||||
$this->commandInfo[$command] = $info;
|
||||
ksort($this->commands);
|
||||
}
|
||||
|
||||
public function setCommandHelp($command, $help)
|
||||
@ -96,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;
|
||||
|
@ -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,25 @@ class LineRead
|
||||
$this->redraw();
|
||||
break;
|
||||
case "\e[A": // up
|
||||
if ($this->posHistory == 0) {
|
||||
$this->stashedBuffer = $this->buffer;
|
||||
}
|
||||
if ($this->posHistory < count($this->history)) {
|
||||
$this->posHistory++;
|
||||
$this->buffer = $this->history[$this->posHistory-1];
|
||||
$this->redraw();
|
||||
}
|
||||
break;
|
||||
case "\e[B": // down
|
||||
if ($this->posHistory > 1) {
|
||||
$this->posHistory--;
|
||||
$this->buffer = $this->history[$this->posHistory-1];
|
||||
$this->redraw();
|
||||
} elseif ($this->posHistory > 0) {
|
||||
$this->posHistory--;
|
||||
$this->buffer = $this->stashedBuffer;
|
||||
$this->redraw();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(STDERR, "\n%s\n", substr($code,1));
|
||||
|
100
lib/Shell.php
100
lib/Shell.php
@ -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 ]);
|
||||
}
|
||||
@ -124,7 +130,26 @@ 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->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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -139,10 +164,11 @@ class Shell
|
||||
|
||||
public function setPrompt($text)
|
||||
{
|
||||
if (!$this->lineReader) {
|
||||
return;
|
||||
$this->prompt = $text;
|
||||
|
||||
if ($this->lineReader) {
|
||||
$this->lineReader->setPromptText($text);
|
||||
}
|
||||
$this->lineReader->setPromptText($text);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,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)) {
|
||||
$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;
|
||||
}
|
||||
@ -173,7 +205,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
|
||||
*/
|
||||
@ -199,10 +231,12 @@ class Shell
|
||||
{
|
||||
switch ($command) {
|
||||
case '.':
|
||||
printf("context<%s>: %s\n", $this->context->getName(), json_encode($this->context->getData()));
|
||||
$type = basename(strtr(get_class($this->context), "\\", "/"));
|
||||
printf("%s<%s>: %s\n", $type, $this->context->getName(), json_encode($this->context->getData()));
|
||||
$level = 0;
|
||||
foreach ($this->contextStack as $context) {
|
||||
printf(" %s- context<%s>\n", str_repeat(" ",$level++), $context->getName());
|
||||
$type = basename(strtr(get_class($context), "\\", "/"));
|
||||
printf(" %s- %s<%s>\n", str_repeat(" ",$level++), $type, $context->getName());
|
||||
}
|
||||
break;
|
||||
case '..':
|
||||
@ -211,13 +245,34 @@ class Shell
|
||||
break;
|
||||
case 'help':
|
||||
$help = $this->context->getCommandHelp();
|
||||
printf("Commands in current context:\n\n");
|
||||
foreach ($help as $command=>$info) {
|
||||
printf(" %-20s %s\n", $command, $info);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("\nGlobal commands:\n\n");
|
||||
printf(" %-20s %s\n", "exit", "Leave the shell");
|
||||
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
|
||||
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();
|
||||
@ -262,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));
|
||||
|
||||
@ -275,14 +330,23 @@ class Shell
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user