From 27af65af5e7eae7e5d88dfdc2fef652068527a2f Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 29 Apr 2016 15:49:10 +0200 Subject: [PATCH] Changed prompt management --- examples/simple.php | 12 +++++++----- lib/Shell.php | 8 +++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/simple.php b/examples/simple.php index 78b34ab..acd1d07 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -28,11 +28,14 @@ class MyShell extends Shell echo "world\n\rthis\n\ris\n\ra\ntest\n\r"; }); $this->addCommand(new MyCommand()); + $this->updatePrompt(); } - - protected function getPrompt() + + protected function updatePrompt() { - return "test[{$this->seq}]: "; + $this->setPrompt("test[{$this->seq}]: "); + $fg = ($this->seq % 7) + 1; + $this->setPromptStyle("3{$fg}"); } protected function onUpdate() @@ -43,13 +46,12 @@ class MyShell extends Shell $lt = $t + 5; echo date("Y-m-d h:i:s")."\n"; } - $fg = ($this->seq % 7) + 1; - $this->setPromptStyle("3{$fg}"); } protected function onCommand($buffer) { $this->seq++; + $this->updatePrompt(); parent::onCommand($buffer); } diff --git a/lib/Shell.php b/lib/Shell.php index 89d35de..3d6ea81 100644 --- a/lib/Shell.php +++ b/lib/Shell.php @@ -6,6 +6,7 @@ use NoccyLabs\Shell\LineRead; abstract class Shell { + protected $prompt; protected $promptStyle; @@ -76,6 +77,11 @@ abstract class Shell { } + public function setPrompt($prompt) + { + $this->prompt = $prompt; + } + public function setPromptStyle($style) { $this->promptStyle = $style; @@ -95,7 +101,7 @@ abstract class Shell $this->running = true; do { - $lineRead->setPrompt($this->getPrompt(), $this->promptStyle); + $lineRead->setPrompt($this->prompt, $this->promptStyle); $buffer = $lineRead->update(); if ($buffer == "\x03") { $this->stop();