20 Commits
0.1 ... 0.2.3.3

Author SHA1 Message Date
b6727bed80 Fixed timer implementation 2017-01-22 23:22:13 +01:00
0e5a25567c Added exception handling to run() 2016-11-21 01:26:06 +01:00
bdec60717f Improved the cursor in LineRead when using history 2016-11-19 15:46:13 +01:00
43a6475192 Added an execute() method to context to catch unhandled commands 2016-11-19 14:18:53 +01:00
7bfd8453e7 Improved the context stack 2016-11-15 03:29:00 +01:00
482d8a54e5 Prompt can now be set before linereader is created 2016-11-12 16:37:51 +01:00
b2a23f992d Fixed an issue with the help command 2016-11-04 02:23:10 +01:00
455574b6a5 Fixed issue with global commands in parent contexts 2016-11-02 22:49:19 +01:00
46806e3d62 Bugfixes in lineread, implemented command history 2016-11-02 22:11:35 +01:00
427cac578a Implemented timers 2016-11-02 14:22:53 +01:00
ea09a15963 Ooops, bugfix. 2016-11-02 13:57:12 +01:00
fe27eeb4a3 Bugfixes and improvements to shell and context 2016-11-02 13:53:56 +01:00
a2c1148c52 Now shows context class with . 2016-11-01 16:27:23 +01:00
81dea747b2 Doccomment can now override context command name 2016-11-01 16:19:38 +01:00
ae17abb6c1 Improved context commands 2016-11-01 16:10:35 +01:00
01ee043bac The . builtin command now shows the stack too 2016-11-01 15:45:54 +01:00
bb74b56fc4 Armored getContextPath() 2016-11-01 15:32:53 +01:00
3cbf504aed Fixed bug in addCommands 2016-11-01 15:28:03 +01:00
612e8d06c0 Implemented update event 2016-11-01 15:15:47 +01:00
de7f12b7d5 Implemented contexts, optimizations 2016-11-01 15:12:11 +01:00
16 changed files with 867 additions and 133 deletions

View File

@ -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
View 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
View 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();

27
examples/contexts.php Normal file
View 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();

19
examples/errors.php Normal file
View 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();

View File

@ -4,16 +4,49 @@ require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
use NoccyLabs\Shell\Context;
class MyCommand extends Command
{
public function getName()
{
return "mycommand";
}
public function execute()
{
$this->writeln("Executing command");
echo "Executing command";
}
}
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()
{
}
}
@ -22,30 +55,29 @@ 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 = new MyContext();
$this->pushContext($context);
$context->addCommand("hello", function () {
echo "world\nthis\nis\na\ntest\n";
});
$this->addCommand(new MyCommand());
$context->addCommand("mycommand", new MyCommand());
$this->updatePrompt();
$this->addListener("update", function() {
static $lt;
$t = floor(microtime(true));
if ($t > $lt) {
$lt = $t + 5;
echo date("Y-m-d h:i:s")."\n";
}
});
}
protected function updatePrompt()
{
$this->setPrompt("test[{$this->seq}]: ");
$fg = ($this->seq % 7) + 1;
$this->setPromptStyle("3{$fg}");
}
protected function onUpdate()
{
static $lt;
$t = floor(microtime(true));
if ($t > $lt) {
$lt = $t + 5;
echo date("Y-m-d h:i:s")."\n";
}
}
protected function onCommand($buffer)

60
examples/timers.php Normal file
View File

@ -0,0 +1,60 @@
<?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();
$t1 = $this->addTimer(5000, function () {
echo "5 seconds\n";
});
$app = $this;
$t2 = $this->addTimer(15000, function () use ($t1, $app) {
echo "Removing timers...\n";
$app->removeTimer($t1);
});
}
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";

View File

@ -1,14 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
class Hinter implement HinterInterface
{
public function getHints()
{
}
public function addHinter(HinterInterface $hinter)
{
}
}

View File

@ -1,7 +0,0 @@
<?php
namespace NoccyLabs\Shell\AutoComplete;
interface HinterInterface
{
}

View File

@ -23,7 +23,8 @@ abstract class Command
$this->shell->writeln($str);
}
abstract public function getName();
public function getName()
{}
public function getDescription()
{}
@ -31,8 +32,8 @@ abstract class Command
public function getHelp()
{}
public function run(array $args)
public function __invoke(...$args)
{
call_user_func_array([$this,"execute"], $args);
call_user_func([$this,"execute"], ...$args);
}
}

199
lib/Context.php Normal file
View File

@ -0,0 +1,199 @@
<?php
namespace NoccyLabs\Shell;
class Context
{
protected $name;
protected $commands = [];
protected $commandInfo = [];
protected $data = [];
protected $parent;
protected $shell;
public function __construct($name=null, array $data=[])
{
$this->name = $name;
$this->data = $data;
$this->configure();
}
public function getContextInfo()
{
return null;
}
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
$this->findCommands();
}
protected function findCommands()
{
$refl = new \ReflectionClass(get_called_class());
foreach ($refl->getMethods() as $method) {
$docblock = $method->getDocComment();
$lines = array_map(function ($line) {
return trim($line, "*/ \t");
}, explode("\n", $docblock));
$info = [];
foreach ($lines as $line) {
if (preg_match("/^@(command|help|args|global)\\s*(.*)$/", $line, $match)) {
list($void,$key,$value) = $match;
$info[$key] = $value;
}
if (count($info)>0) {
$cmdName = array_key_exists("command",$info)?$info["command"]:$method->getName();
$this->addCommand($cmdName, [$this, $method->getName()], $info);
}
}
}
}
public function addCommand($command, callable $handler, array $info=[])
{
$this->commands[$command] = $handler;
$this->commandInfo[$command] = $info;
ksort($this->commands);
}
public function setCommandHelp($command, $help)
{
if (!array_key_exists($command, $this->commandInfo)) {
return;
}
$this->commandInfo[$command]['help'] = $help;
}
public function addCommands(array $commands)
{
foreach ($commands as $command=>$handler) {
// Make it easier to connect commands direct to local functions
if (is_string($handler) && is_callable([$this,$handler])) {
$handler = [ $this,$handler ];
}
// Add the command to the command list
$this->addCommand($command, $handler);
}
}
public function hasCommand($command)
{
return array_key_exists($command, $this->commands);
}
public function getCommand($command)
{
return $this->commands[$command];
}
public function getCommandHelp()
{
$ret = [];
foreach ($this->commands as $command=>$handler) {
$info = $this->commandInfo[$command];
$args = array_key_exists("args",$info)?$info['args']:"";
$help = array_key_exists("help",$info)?$info['help']:"";
$ret[trim("{$command} {$args}")] = $help;
}
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()
{
return $this->name;
}
public function __get($key)
{
if (!array_key_exists($key,$this->data)) {
return false;
}
return $this->data[$key];
}
public function __set($key,$value)
{
$this->data[$key] = $value;
}
public function __isset($key)
{
return array_key_exists($key);
}
public function __unset($key)
{
unset($this->data[$key]);
}
public function getData()
{
return $this->data;
}
}

View File

@ -0,0 +1,6 @@
<?php
namespace NoccyLabs\Shell\Exception;
class BadCommandException extends ShellException
{}

View File

@ -0,0 +1,8 @@
<?php
namespace NoccyLabs\Shell\Exception;
use Exception;
class ShellException extends Exception
{}

View File

@ -15,6 +15,8 @@ class LineRead
protected $buffer = null;
protected $history = [];
protected $stashedBuffer = null;
protected $posHistory = 0;
@ -34,7 +36,7 @@ class LineRead
{
stream_set_blocking(STDIN, false);
$this->sttyOld = trim(exec('stty -g'));
exec('stty raw -echo'); // isig');
exec('stty raw -echo opost onlret'); // isig');
}
public function __destruct()
@ -61,13 +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;
$promptStyle = $this->styleToAnsi($this->promptStyle);
$commandStyle = $this->styleToAnsi($this->commandStyle);
$endStyle = "\e[0m";
fprintf(STDOUT, "\r\e[2K{$promptStyle}%s{$commandStyle}%s\e[%dG{$endStyle}", $prompt, $buffer, $cursor);
fprintf(STDOUT, "\r\e[2K%s %s\e[%dG{$endStyle}", ($this->promptStyle)($prompt), ($this->commandStyle)($buffer), $cursor);
}
protected function styleToAnsi($style)
@ -87,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);
@ -115,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");
@ -148,7 +158,36 @@ class LineRead
$this->redraw();
break;
case "\e[A": // up
if ($this->posHistory == 0) {
$this->stashedBuffer = $this->buffer;
}
if ($this->posCursor == strlen($this->buffer)) {
$this->posCursor = -1;
}
if ($this->posHistory < count($this->history)) {
$this->posHistory++;
$this->buffer = $this->history[$this->posHistory-1];
}
if ($this->posCursor == -1) {
$this->posCursor = strlen($this->buffer);
}
$this->redraw();
break;
case "\e[B": // down
if ($this->posCursor == strlen($this->buffer)) {
$this->posCursor = -1;
}
if ($this->posHistory > 1) {
$this->posHistory--;
$this->buffer = $this->history[$this->posHistory-1];
} elseif ($this->posHistory > 0) {
$this->posHistory--;
$this->buffer = $this->stashedBuffer;
}
if ($this->posCursor == -1) {
$this->posCursor = strlen($this->buffer);
}
$this->redraw();
break;
default:
fprintf(STDERR, "\n%s\n", substr($code,1));
@ -160,12 +199,14 @@ class LineRead
$this->commandStyle = $style;
}
public function setPrompt($prompt, $style=null)
public function setPromptText($prompt)
{
$this->prompt = $prompt;
if ($style) {
$this->promptStyle = $style;
}
}
public function setPromptStyle($style)
{
$this->promptStyle = $style;
}
}

View File

@ -4,58 +4,311 @@ namespace NoccyLabs\Shell;
use NoccyLabs\Shell\LineRead;
abstract class Shell
class Shell
{
protected $prompt;
const EV_PROMPT = "prompt";
const EV_COMMAND = "command";
protected $promptStyle;
protected $lineReader = null;
protected $context = null;
protected $contextStack = [];
protected $listeners = [];
protected $commandStyle;
protected $timers = [];
public function __construct(array $config=[])
protected $prompt = ">";
public function __construct()
{
$this->configure($config);
$this->configure();
}
abstract protected function configure(array $config);
public function addCommand($command, callable $handler=null)
protected function configure()
{
if (!$handler) {
if (!($command instanceof Command)) {
throw new \RuntimeException("Handler is not callable nor a Command");
}
$command->setShell($this);
$this->commands[$command->getName()] = $command;
}
/**
* Push a new primary context, saving the previous contexts on a stack.
*
* @param Context $context
*/
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 ]);
}
/**
* Pop the current context.
*
* @return Context
*/
public function popContext()
{
$previous = $this->context;
if (count($this->contextStack)>0) {
$this->context = array_shift($this->contextStack);
} else {
$this->commands[$command] = $handler;
$this->context = null;
}
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
return $previous;
}
public function getContextPath($separator=":")
{
// Return null if we don't have a current context
if (!$this->context)
return null;
// Assemble the contexts to walk
$stack = [ $this->context->getName() ];
foreach ($this->contextStack as $context) {
$stack[] = $context->getName();
}
// Reverse the order to make it more logical
$stack = array_reverse($stack);
return join($separator,$stack);
}
public function createContext()
{
$context = new Context();
$this->pushContext($context);
return $context;
}
/**
* Add a handler to be called when an event fires.
*
* @param string $event
* @param callable $handler
*/
public function addListener($event, callable $handler)
{
if (!array_key_exists($event,$this->listeners)) {
$this->listeners[$event] = [];
}
$this->listeners[$event][] = $handler;
}
/**
* Invoke event handlers for event.
*
* @param string $event
* @param callable $handler
*/
protected function dispatchEvent($event, array $data=[])
{
if (!array_key_exists($event,$this->listeners)) {
return;
}
foreach ($this->listeners[$event] as $handler) {
call_user_func($handler, (object)$data, $this);
}
}
public function execute($command)
/**
* Add a callback to be called at a regular interval during the update phase.
* Adding timers with a low interval (less than 200 i.e. 0.2s) may have an
* impact on performance.
*
* @param int $interval Interval in ms
* @param callable $handler The handler
* @param array $userdata Data to be passed to the handler
* @return Timer
*/
public function addTimer($interval, callable $handler, array $userdata=[])
{
if (is_array($command)) {
foreach ($command as $cmd) {
$this->execute($cmd);
$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->next = microtime(true) + $this->interval;
$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;
}
/**
* Remove a created timer.
*
* @param Timer $timer
*/
public function removeTimer($timer)
{
$this->timers = array_filter($this->timers, function ($v) use ($timer) {
return ($v !== $timer);
});
}
public function setPrompt($text)
{
$this->prompt = $text;
if ($this->lineReader) {
$this->lineReader->setPromptText($text);
}
}
/**
* Find a command and return a closure.
*
* @return callable The command
*/
private function findCommand($command)
{
// Go over current context and walk through stack until finding command
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;
}
// Return closure
return function (...$args) use ($handler) {
return call_user_func($handler, ...$args);
};
}
/**
* Execute a command with arguments.
*
* @param string $command The command name to execute
* @param string $args Arguments
* @return mixed
* @throws Exception\BadCommandExcception
*/
public function executeCommand($command, ...$args)
{
if ($this->executeBuiltin($command, ...$args)) {
return;
}
// Call the handler if the command was found
if (($target = $this->findCommand($command))) {
$ret = $target(...$args);
if ($ret instanceof Context) {
$this->pushContext($ret);
}
return;
}
$buffer = str_getcsv($command, " ", "\"", "\\");
if (count($buffer)>0) {
$this->onCommand($buffer);
// Call 'execute' on the current context
if ($this->context->execute($command, ...$args)) {
return;
}
}
protected function onCommand($buffer)
{
$this->executeBuffer($buffer);
// Throw error if the command could not be found
throw new Exception\BadCommandException("Command {$command} not found");
}
protected function executeBuffer(array $buffer)
public function executeBuiltin($command, ...$args)
{
$commandName = array_shift($buffer);
switch ($command) {
case '.':
$type = basename(strtr(get_class($this->context), "\\", "/"));
printf("%s<%s>: %s\n", $type, $this->context->getName(), $this->context->getContextInfo());
$level = 0;
foreach ($this->contextStack as $context) {
$type = basename(strtr(get_class($context), "\\", "/"));
printf(" %s└─%s<%s>: %s\n", str_repeat(" ",$level++), $type, $context->getName(), $context->getContextInfo());
}
break;
case '..':
if (count($this->contextStack)>0)
$this->popContext();
break;
case 'help':
$help = $this->context->getCommandHelp();
$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) {
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();
break;
default:
return false;
}
return true;
}
/**
* Parse a string and execute the resulting command.
*
* @param string $command The string to parse
* @return mixed
*/
public function executeBuffer(string $command)
{
$args = str_getcsv($command, " ", "\"", "\\");
$command = array_shift($args);
try {
$this->executeCommand($command, ...$args);
} catch (Exception\ShellException $e) {
echo "\e[31;91;1m{$e->getMessage()}\e[0m\n";
}
/*
if (array_key_exists($commandName, $this->commands)) {
$command = $this->commands[$commandName];
if ($command instanceof Command) {
@ -66,61 +319,70 @@ abstract class Shell
return;
}
$this->writeln("Bad command: ".$commandName);
}
public function writeln($output)
{
echo "\r\e[K\e[0m".$output."\n";
}
protected function onUpdate()
{
}
public function setPrompt($prompt)
{
$this->prompt = $prompt;
}
public function setPromptStyle($style)
{
$this->promptStyle = $style;
}
public function setCommandStyle($style)
{
$this->commandStyle = $style;
*/
}
public function run()
{
$lineRead = new LineRead();
$lineRead->setCommandStyle($this->commandStyle);
try {
$this->lineReader = new LineRead();
$this->running = true;
$this->lineReader->setPromptText($this->prompt);
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
do {
$lineRead->setPrompt($this->prompt, $this->promptStyle);
$buffer = $lineRead->update();
if ($buffer == "\x03") {
$this->stop();
continue;
$this->running = true;
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
while ($this->running) {
// Update the input stuff, sleep if nothing to do.
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);
}
$output = ob_get_contents();
ob_end_clean();
if (trim($output)) {
$this->lineReader->erase();
echo rtrim($output)."\n";
$this->lineReader->redraw();
}
if (!$this->context) {
$this->stop();
}
if ($buffer) {
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
}
}
ob_start();
$this->onUpdate();
if ($buffer !== null) {
$this->execute($buffer);
}
$buf = ob_get_contents();
ob_end_clean();
if ($buf) {
$lineRead->erase();
echo str_replace("\n", "\r\n", rtrim($buf)."\n");
$lineRead->redraw();
}
usleep(10000);
} while ($this->running);
} catch (\Exception $e) {
fprintf(STDERR, "\e[31;1mFatal: Unhandled exception\e[0m\n\n%s\n", $e);
}
$this->lineReader = null;
}

56
lib/Style.php Normal file
View File

@ -0,0 +1,56 @@
<?php
namespace NoccyLabs\Shell;
class Style
{
const NONE = null;
const BLACK = 0;
const RED = 1;
const GREEN = 2;
const YELLOW = 3;
const BLUE = 4;
const CYAN = 5;
const MAGENTA = 6;
const WHITE = 7;
const GRAY = 8;
const BR_RED = 9;
const BR_GREEN = 10;
const BR_YELLOW = 11;
const BR_BLUE = 12;
const BR_CYAN = 13;
const BR_MAGENTA = 14;
const BR_WHITE = 15;
const A_INTENSE = 1;
const A_UNDERLINE = 4;
const A_INVERSE = 7;
protected $fg = self::NONE;
protected $bg = self::NONE;
public function __construct($fg=self::NONE, $bg=self::NONE)
{
$this->fg = $fg;
$this->bg = $bg;
}
public function __invoke($string)
{
$pre = null; $post = null;
if ($this->fg !== self::NONE) {
$pre.= "\e[".(($this->fg > 7)?(90+($this->fg-8)):(30+$this->fg))."m";
$post.= "\e[39m";
}
if ($this->bg !== self::NONE) {
$pre.= "\e[".(($this->bg > 7)?(100+($this->bg-8)):(40+$this->bg))."m";
$post.= "\e[49m";
}
return $pre . $string . $post;
}
}