Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
43a6475192 | |||
7bfd8453e7 | |||
482d8a54e5 |
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();
|
@ -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;
|
||||||
@ -141,6 +146,23 @@ class Context
|
|||||||
return array_key_exists('global', $info);
|
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)
|
||||||
|
@ -19,6 +19,8 @@ class Shell
|
|||||||
|
|
||||||
protected $timers = [];
|
protected $timers = [];
|
||||||
|
|
||||||
|
protected $prompt = ">";
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->configure();
|
$this->configure();
|
||||||
@ -162,11 +164,12 @@ class Shell
|
|||||||
|
|
||||||
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.
|
||||||
@ -220,6 +223,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");
|
||||||
}
|
}
|
||||||
@ -229,11 +238,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 '..':
|
||||||
@ -314,7 +323,7 @@ class Shell
|
|||||||
{
|
{
|
||||||
$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));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user