From ba27d840ea60e0871239695eed89dbaef269a344 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Tue, 27 Feb 2024 22:07:38 +0100 Subject: [PATCH] Styling, abbreviated commands --- src/CommandHandler.php | 15 +++++++++++++++ src/Shell.php | 8 +++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/CommandHandler.php b/src/CommandHandler.php index c0cc527..789065b 100644 --- a/src/CommandHandler.php +++ b/src/CommandHandler.php @@ -13,6 +13,8 @@ class CommandHandler implements EventEmitterInterface private array $commands = []; + private bool $allowAbbreviatedCommands = true; + public function add(string $command, callable $handler, array $signature=[]): self { $this->commands[$command] = [ 'handler' => $handler, 'signature' => $signature ]; @@ -28,6 +30,19 @@ class CommandHandler implements EventEmitterInterface { $command = array_shift($line); + if ($this->allowAbbreviatedCommands) { + $candidates = array_filter( + array_keys($this->commands), + fn($c)=>str_starts_with($c,$command) + ); + if (count($candidates)>2) { + $this->emit("candidates", [ $candidates, $shell ]); + return; + } + if (count($candidates)==1) { + $command = array_shift($candidates); + } + } if (!array_key_exists($command, $this->commands)) { $this->emit("command", [ $command, $line, $shell ]); return; diff --git a/src/Shell.php b/src/Shell.php index 7b2f171..a93a22b 100644 --- a/src/Shell.php +++ b/src/Shell.php @@ -35,7 +35,9 @@ class Shell implements WritableStreamInterface, EventEmitterInterface private int $promptWidth = 0; - private string $promptStyle = "35;1"; + private string $promptStyle = "36;1"; + + private string $inputStyle = "36"; public function __construct(?ReadableStreamInterface $input=null, ?WritableStreamInterface $output=null) { @@ -191,8 +193,8 @@ class Shell implements WritableStreamInterface, EventEmitterInterface $obuf = "\r" . "\e[{$this->promptStyle}m" . $this->prompt . "\e[0m"; $ostr = mb_substr($this->buffer, $this->scrollOffset) . " "; $ostr = mb_substr($ostr, 0, $this->termWidth - $this->promptWidth); - $obuf .= $ostr . "\e[K\e[" . $pos . "G"; + $obuf .= "\e[{$this->inputStyle}m" . $ostr . "\e[K\e[0m\e[" . $pos . "G"; $this->ostream->write($obuf); } -} \ No newline at end of file +}