Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
b6727bed80 | |||
0e5a25567c | |||
bdec60717f | |||
43a6475192 | |||
7bfd8453e7 | |||
482d8a54e5 | |||
b2a23f992d | |||
455574b6a5 |
12
README.md
12
README.md
@ -0,0 +1,12 @@
|
|||||||
|
NoccyLabs Shell Core
|
||||||
|
====================
|
||||||
|
|
||||||
|
This library helps make elegant command line applications that spawn an isolated shell.
|
||||||
|
It uses a standalone implementation for buffered input with support for arrow keys to
|
||||||
|
navigate the history and more.
|
||||||
|
|
||||||
|
Note that this library requirements a fully ANSI compatible terminal with UTF-8 support
|
||||||
|
in order to use colors, control the cursor position etc. As it uses `stty` to configure
|
||||||
|
input buffering, it will likely not work on Windows.
|
||||||
|
|
||||||
|
|
||||||
|
12
examples/basic.php
Normal file
12
examples/basic.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__."/../vendor/autoload.php";
|
||||||
|
|
||||||
|
use NoccyLabs\Shell\Shell;
|
||||||
|
use NoccyLabs\Shell\Context;
|
||||||
|
|
||||||
|
|
||||||
|
$myShell = new Shell();
|
||||||
|
$myShell->setPrompt("test>");
|
||||||
|
$myShell->pushContext(new Context());
|
||||||
|
$myShell->run();
|
20
examples/catchall.php
Normal file
20
examples/catchall.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__."/../vendor/autoload.php";
|
||||||
|
|
||||||
|
use NoccyLabs\Shell\Shell;
|
||||||
|
use NoccyLabs\Shell\Context;
|
||||||
|
|
||||||
|
class CatchAllContext extends Context
|
||||||
|
{
|
||||||
|
public function execute($cmd, ...$arg)
|
||||||
|
{
|
||||||
|
printf("Executing: %s %s\n", $cmd, join(" ",$arg));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$myShell = new Shell();
|
||||||
|
$myShell->setPrompt("test>");
|
||||||
|
$myShell->pushContext(new CatchAllContext());
|
||||||
|
$myShell->run();
|
19
examples/errors.php
Normal file
19
examples/errors.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__."/../vendor/autoload.php";
|
||||||
|
|
||||||
|
use NoccyLabs\Shell\Shell;
|
||||||
|
use NoccyLabs\Shell\Context;
|
||||||
|
|
||||||
|
class CatchAllContext extends Context
|
||||||
|
{
|
||||||
|
public function execute($cmd, ...$arg)
|
||||||
|
{
|
||||||
|
throw new \Exception("Uh-oh! Error!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$myShell = new Shell();
|
||||||
|
$myShell->setPrompt("test>");
|
||||||
|
$myShell->pushContext(new CatchAllContext());
|
||||||
|
$myShell->run();
|
@ -20,6 +20,29 @@ class MyContext extends Context
|
|||||||
* @command testme
|
* @command testme
|
||||||
* @args
|
* @args
|
||||||
* @help Useful test!
|
* @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()
|
public function test()
|
||||||
{
|
{
|
||||||
|
@ -30,9 +30,15 @@ class MyShell extends Shell
|
|||||||
$this->pushContext($context);
|
$this->pushContext($context);
|
||||||
$this->updatePrompt();
|
$this->updatePrompt();
|
||||||
|
|
||||||
$this->addTimer(5000, function () {
|
$t1 = $this->addTimer(5000, function () {
|
||||||
echo "5 seconds\n";
|
echo "5 seconds\n";
|
||||||
});
|
});
|
||||||
|
$app = $this;
|
||||||
|
$t2 = $this->addTimer(15000, function () use ($t1, $app) {
|
||||||
|
echo "Removing timers...\n";
|
||||||
|
$app->removeTimer($t1);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updatePrompt()
|
protected function updatePrompt()
|
||||||
|
@ -23,6 +23,11 @@ class Context
|
|||||||
$this->configure();
|
$this->configure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getContextInfo()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function setShell(Shell $shell)
|
public function setShell(Shell $shell)
|
||||||
{
|
{
|
||||||
$this->shell = $shell;
|
$this->shell = $shell;
|
||||||
@ -71,7 +76,7 @@ class Context
|
|||||||
}, explode("\n", $docblock));
|
}, explode("\n", $docblock));
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($lines as $line) {
|
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;
|
list($void,$key,$value) = $match;
|
||||||
$info[$key] = $value;
|
$info[$key] = $value;
|
||||||
}
|
}
|
||||||
@ -132,6 +137,32 @@ class Context
|
|||||||
return $ret;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catch-all handler for commands not defined in context, globally or builtin.
|
||||||
|
* Override this function and return true if the command is handled ok.
|
||||||
|
*
|
||||||
|
* @param string $command The command to execute
|
||||||
|
* @param string[] $args The arguments to the command
|
||||||
|
* @return bool True if the command was handled
|
||||||
|
*/
|
||||||
|
public function execute($command, ...$args)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the context
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
|
@ -68,11 +68,11 @@ class LineRead
|
|||||||
$this->posCursor = strlen($this->buffer);
|
$this->posCursor = strlen($this->buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
$cursor = strlen($this->prompt) + 1 + $this->posCursor - $this->posScroll;
|
$cursor = strlen($this->prompt) + 2 + $this->posCursor - $this->posScroll;
|
||||||
|
|
||||||
$endStyle = "\e[0m";
|
$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)
|
protected function styleToAnsi($style)
|
||||||
@ -161,22 +161,33 @@ class LineRead
|
|||||||
if ($this->posHistory == 0) {
|
if ($this->posHistory == 0) {
|
||||||
$this->stashedBuffer = $this->buffer;
|
$this->stashedBuffer = $this->buffer;
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == strlen($this->buffer)) {
|
||||||
|
$this->posCursor = -1;
|
||||||
|
}
|
||||||
if ($this->posHistory < count($this->history)) {
|
if ($this->posHistory < count($this->history)) {
|
||||||
$this->posHistory++;
|
$this->posHistory++;
|
||||||
$this->buffer = $this->history[$this->posHistory-1];
|
$this->buffer = $this->history[$this->posHistory-1];
|
||||||
$this->redraw();
|
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == -1) {
|
||||||
|
$this->posCursor = strlen($this->buffer);
|
||||||
|
}
|
||||||
|
$this->redraw();
|
||||||
break;
|
break;
|
||||||
case "\e[B": // down
|
case "\e[B": // down
|
||||||
|
if ($this->posCursor == strlen($this->buffer)) {
|
||||||
|
$this->posCursor = -1;
|
||||||
|
}
|
||||||
if ($this->posHistory > 1) {
|
if ($this->posHistory > 1) {
|
||||||
$this->posHistory--;
|
$this->posHistory--;
|
||||||
$this->buffer = $this->history[$this->posHistory-1];
|
$this->buffer = $this->history[$this->posHistory-1];
|
||||||
$this->redraw();
|
|
||||||
} elseif ($this->posHistory > 0) {
|
} elseif ($this->posHistory > 0) {
|
||||||
$this->posHistory--;
|
$this->posHistory--;
|
||||||
$this->buffer = $this->stashedBuffer;
|
$this->buffer = $this->stashedBuffer;
|
||||||
$this->redraw();
|
|
||||||
}
|
}
|
||||||
|
if ($this->posCursor == -1) {
|
||||||
|
$this->posCursor = strlen($this->buffer);
|
||||||
|
}
|
||||||
|
$this->redraw();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(STDERR, "\n%s\n", substr($code,1));
|
fprintf(STDERR, "\n%s\n", substr($code,1));
|
||||||
|
@ -19,6 +19,8 @@ class Shell
|
|||||||
|
|
||||||
protected $timers = [];
|
protected $timers = [];
|
||||||
|
|
||||||
|
protected $prompt = ">";
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->configure();
|
$this->configure();
|
||||||
@ -135,6 +137,7 @@ class Shell
|
|||||||
private $userdata;
|
private $userdata;
|
||||||
public function __construct($interval, callable $handler, array $userdata) {
|
public function __construct($interval, callable $handler, array $userdata) {
|
||||||
$this->interval = $interval / 1000;
|
$this->interval = $interval / 1000;
|
||||||
|
$this->next = microtime(true) + $this->interval;
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
$this->userdata = $userdata;
|
$this->userdata = $userdata;
|
||||||
}
|
}
|
||||||
@ -155,18 +158,21 @@ class Shell
|
|||||||
*
|
*
|
||||||
* @param Timer $timer
|
* @param Timer $timer
|
||||||
*/
|
*/
|
||||||
public function removeTimer(Timer $timer)
|
public function removeTimer($timer)
|
||||||
{
|
{
|
||||||
|
$this->timers = array_filter($this->timers, function ($v) use ($timer) {
|
||||||
|
return ($v !== $timer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPrompt($text)
|
public function setPrompt($text)
|
||||||
{
|
{
|
||||||
if (!$this->lineReader) {
|
$this->prompt = $text;
|
||||||
return;
|
|
||||||
}
|
if ($this->lineReader) {
|
||||||
$this->lineReader->setPromptText($text);
|
$this->lineReader->setPromptText($text);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a command and return a closure.
|
* Find a command and return a closure.
|
||||||
@ -176,12 +182,18 @@ class Shell
|
|||||||
private function findCommand($command)
|
private function findCommand($command)
|
||||||
{
|
{
|
||||||
// Go over current context and walk through stack until finding command
|
// Go over current context and walk through stack until finding command
|
||||||
foreach(array_merge([ $this->context ] , $this->contextStack) as $context) {
|
if ($this->context->hasCommand($command)) {
|
||||||
if ($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);
|
$handler = $context->getCommand($command);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No handler...
|
||||||
if (empty($handler)) {
|
if (empty($handler)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -214,6 +226,12 @@ class Shell
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call 'execute' on the current context
|
||||||
|
if ($this->context->execute($command, ...$args)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Throw error if the command could not be found
|
// Throw error if the command could not be found
|
||||||
throw new Exception\BadCommandException("Command {$command} not found");
|
throw new Exception\BadCommandException("Command {$command} not found");
|
||||||
}
|
}
|
||||||
@ -223,11 +241,11 @@ class Shell
|
|||||||
switch ($command) {
|
switch ($command) {
|
||||||
case '.':
|
case '.':
|
||||||
$type = basename(strtr(get_class($this->context), "\\", "/"));
|
$type = basename(strtr(get_class($this->context), "\\", "/"));
|
||||||
printf("%s<%s>: %s\n", $type, $this->context->getName(), json_encode($this->context->getData()));
|
printf("%s<%s>: %s\n", $type, $this->context->getName(), $this->context->getContextInfo());
|
||||||
$level = 0;
|
$level = 0;
|
||||||
foreach ($this->contextStack as $context) {
|
foreach ($this->contextStack as $context) {
|
||||||
$type = basename(strtr(get_class($context), "\\", "/"));
|
$type = basename(strtr(get_class($context), "\\", "/"));
|
||||||
printf(" %s- %s<%s>\n", str_repeat(" ",$level++), $type, $context->getName());
|
printf(" %s└─%s<%s>: %s\n", str_repeat(" ",$level++), $type, $context->getName(), $context->getContextInfo());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '..':
|
case '..':
|
||||||
@ -236,11 +254,32 @@ class Shell
|
|||||||
break;
|
break;
|
||||||
case 'help':
|
case 'help':
|
||||||
$help = $this->context->getCommandHelp();
|
$help = $this->context->getCommandHelp();
|
||||||
printf("Commands in current context:\n\n");
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ksort($ghelp);
|
||||||
|
printf("Commands in current context:\n");
|
||||||
foreach ($help as $command=>$info) {
|
foreach ($help as $command=>$info) {
|
||||||
printf(" %-20s %s\n", $command, $info);
|
printf(" %-20s %s\n", $command, $info);
|
||||||
}
|
}
|
||||||
printf("\nGlobal commands:\n\n");
|
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", "exit", "Leave the shell");
|
||||||
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
|
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
|
||||||
break;
|
break;
|
||||||
@ -285,9 +324,10 @@ class Shell
|
|||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$this->lineReader = new LineRead();
|
$this->lineReader = new LineRead();
|
||||||
|
|
||||||
$this->lineReader->setPromptText("shell>");
|
$this->lineReader->setPromptText($this->prompt);
|
||||||
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
|
||||||
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
|
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
|
||||||
|
|
||||||
@ -338,6 +378,10 @@ class Shell
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
fprintf(STDERR, "\e[31;1mFatal: Unhandled exception\e[0m\n\n%s\n", $e);
|
||||||
|
}
|
||||||
|
|
||||||
$this->lineReader = null;
|
$this->lineReader = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user