Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
46806e3d62 | |||
427cac578a | |||
ea09a15963 | |||
fe27eeb4a3 | |||
a2c1148c52 |
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 $data = [];
|
||||||
|
|
||||||
|
protected $parent;
|
||||||
|
|
||||||
|
protected $shell;
|
||||||
|
|
||||||
public function __construct($name=null, array $data=[])
|
public function __construct($name=null, array $data=[])
|
||||||
{
|
{
|
||||||
@ -20,6 +23,38 @@ class Context
|
|||||||
$this->configure();
|
$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()
|
protected function configure()
|
||||||
{
|
{
|
||||||
// Override this to do setup stuff
|
// Override this to do setup stuff
|
||||||
@ -52,6 +87,7 @@ class Context
|
|||||||
{
|
{
|
||||||
$this->commands[$command] = $handler;
|
$this->commands[$command] = $handler;
|
||||||
$this->commandInfo[$command] = $info;
|
$this->commandInfo[$command] = $info;
|
||||||
|
ksort($this->commands);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCommandHelp($command, $help)
|
public function setCommandHelp($command, $help)
|
||||||
|
@ -16,6 +16,8 @@ class LineRead
|
|||||||
|
|
||||||
protected $history = [];
|
protected $history = [];
|
||||||
|
|
||||||
|
protected $stashedBuffer = null;
|
||||||
|
|
||||||
protected $posHistory = 0;
|
protected $posHistory = 0;
|
||||||
|
|
||||||
protected $posCursor = 0;
|
protected $posCursor = 0;
|
||||||
@ -61,6 +63,11 @@ class LineRead
|
|||||||
{
|
{
|
||||||
$prompt = $this->prompt;
|
$prompt = $this->prompt;
|
||||||
$buffer = $this->buffer;
|
$buffer = $this->buffer;
|
||||||
|
|
||||||
|
if ($this->posCursor > strlen($this->buffer)) {
|
||||||
|
$this->posCursor = strlen($this->buffer);
|
||||||
|
}
|
||||||
|
|
||||||
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
|
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
|
||||||
|
|
||||||
$endStyle = "\e[0m";
|
$endStyle = "\e[0m";
|
||||||
@ -85,6 +92,10 @@ class LineRead
|
|||||||
$returnBuffer = null;
|
$returnBuffer = null;
|
||||||
while (strlen($keyBuffer)>0) {
|
while (strlen($keyBuffer)>0) {
|
||||||
if ($keyBuffer[0] == "\e") {
|
if ($keyBuffer[0] == "\e") {
|
||||||
|
if (strlen($keyBuffer)==1) {
|
||||||
|
$keyBuffer = "";
|
||||||
|
return "\e";
|
||||||
|
}
|
||||||
if ($keyBuffer[1] == "[") {
|
if ($keyBuffer[1] == "[") {
|
||||||
$ctrlChar = substr($keyBuffer, 0,3);
|
$ctrlChar = substr($keyBuffer, 0,3);
|
||||||
$keyBuffer = substr($keyBuffer, 3);
|
$keyBuffer = substr($keyBuffer, 3);
|
||||||
@ -113,6 +124,7 @@ class LineRead
|
|||||||
}
|
}
|
||||||
} elseif ($keyCode == 13) {
|
} elseif ($keyCode == 13) {
|
||||||
$returnBuffer = $this->buffer;
|
$returnBuffer = $this->buffer;
|
||||||
|
array_unshift($this->history, $this->buffer);
|
||||||
$this->buffer = null;
|
$this->buffer = null;
|
||||||
$this->posCursor = 0;
|
$this->posCursor = 0;
|
||||||
printf("\n\r");
|
printf("\n\r");
|
||||||
@ -146,7 +158,25 @@ class LineRead
|
|||||||
$this->redraw();
|
$this->redraw();
|
||||||
break;
|
break;
|
||||||
case "\e[A": // up
|
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
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(STDERR, "\n%s\n", substr($code,1));
|
fprintf(STDERR, "\n%s\n", substr($code,1));
|
||||||
|
@ -17,6 +17,8 @@ class Shell
|
|||||||
|
|
||||||
protected $listeners = [];
|
protected $listeners = [];
|
||||||
|
|
||||||
|
protected $timers = [];
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->configure();
|
$this->configure();
|
||||||
@ -35,8 +37,10 @@ class Shell
|
|||||||
public function pushContext(Context $context)
|
public function pushContext(Context $context)
|
||||||
{
|
{
|
||||||
if ($this->context) {
|
if ($this->context) {
|
||||||
|
$context->setParent($this->context);
|
||||||
array_unshift($this->contextStack, $this->context);
|
array_unshift($this->contextStack, $this->context);
|
||||||
}
|
}
|
||||||
|
$context->setShell($this);
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
|
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
|
||||||
}
|
}
|
||||||
@ -124,7 +128,26 @@ class Shell
|
|||||||
*/
|
*/
|
||||||
public function addTimer($interval, callable $handler, array $userdata=[])
|
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.
|
* Execute a command with arguments.
|
||||||
*
|
*
|
||||||
* @param string $command The command name to execute
|
* @param string $command The command name to execute
|
||||||
* @param string.. $args Arguments
|
* @param string $args Arguments
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception\BadCommandExcception
|
* @throws Exception\BadCommandExcception
|
||||||
*/
|
*/
|
||||||
@ -199,10 +222,12 @@ class Shell
|
|||||||
{
|
{
|
||||||
switch ($command) {
|
switch ($command) {
|
||||||
case '.':
|
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;
|
$level = 0;
|
||||||
foreach ($this->contextStack as $context) {
|
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;
|
break;
|
||||||
case '..':
|
case '..':
|
||||||
@ -275,14 +300,23 @@ class Shell
|
|||||||
if (!($buffer = $this->lineReader->update())) {
|
if (!($buffer = $this->lineReader->update())) {
|
||||||
usleep(10000);
|
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.
|
// we get a ^C on ^C, so deal with the ^C.
|
||||||
if ($buffer == "\x03") {
|
if ($buffer == "\x03") {
|
||||||
|
$this->dispatchEvent("shell.stop");
|
||||||
$this->stop();
|
$this->stop();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Execute the buffer
|
// Execute the buffer
|
||||||
ob_start();
|
ob_start();
|
||||||
$this->dispatchEvent("update");
|
$this->dispatchEvent("update");
|
||||||
|
foreach ($this->timers as $timer) {
|
||||||
|
$timer->update();
|
||||||
|
}
|
||||||
if ($buffer) {
|
if ($buffer) {
|
||||||
$this->executeBuffer($buffer);
|
$this->executeBuffer($buffer);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user