Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
46806e3d62 | |||
427cac578a | |||
ea09a15963 | |||
fe27eeb4a3 | |||
a2c1148c52 | |||
81dea747b2 |
@ -17,6 +17,7 @@ class MyCommand extends Command
|
||||
class MyContext extends Context
|
||||
{
|
||||
/**
|
||||
* @command testme
|
||||
* @args
|
||||
* @help Useful 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
|
||||
@ -41,7 +76,8 @@ class Context
|
||||
$info[$key] = $value;
|
||||
}
|
||||
if (count($info)>0) {
|
||||
$this->addCommand($method->getName(), [$this, $method->getName()], $info);
|
||||
$cmdName = array_key_exists("command",$info)?$info["command"]:$method->getName();
|
||||
$this->addCommand($cmdName, [$this, $method->getName()], $info);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -51,6 +87,7 @@ class Context
|
||||
{
|
||||
$this->commands[$command] = $handler;
|
||||
$this->commandInfo[$command] = $info;
|
||||
ksort($this->commands);
|
||||
}
|
||||
|
||||
public function setCommandHelp($command, $help)
|
||||
|
@ -16,6 +16,8 @@ class LineRead
|
||||
|
||||
protected $history = [];
|
||||
|
||||
protected $stashedBuffer = null;
|
||||
|
||||
protected $posHistory = 0;
|
||||
|
||||
protected $posCursor = 0;
|
||||
@ -61,6 +63,11 @@ class LineRead
|
||||
{
|
||||
$prompt = $this->prompt;
|
||||
$buffer = $this->buffer;
|
||||
|
||||
if ($this->posCursor > strlen($this->buffer)) {
|
||||
$this->posCursor = strlen($this->buffer);
|
||||
}
|
||||
|
||||
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
|
||||
|
||||
$endStyle = "\e[0m";
|
||||
@ -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));
|
||||
|
@ -17,6 +17,8 @@ class Shell
|
||||
|
||||
protected $listeners = [];
|
||||
|
||||
protected $timers = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->configure();
|
||||
@ -35,8 +37,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 +128,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -173,7 +196,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 +222,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 '..':
|
||||
@ -275,14 +300,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