From e5e56b66c0d9fc5d191a3c43f018f8774edfb4da Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 13 Jun 2018 12:12:54 +0000 Subject: [PATCH] console : allow to set the user proposition when using readline console : manage correctely the backspace git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4239 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/console.php b/console.php index d7f7b63..1ffa55a 100644 --- a/console.php +++ b/console.php @@ -232,21 +232,28 @@ private $usleep = 0; /** Get the line of characters pressed by the user and return the result. * Stop when the user valid by \n. * Manage correctely the backspace, the Ctrl+W to remove word... + * @param string $propo Preset the text for the user + * @param bool|string $stopperChar The chars to stop the analysis and return + * the result * @return The typed string */ - public function readline ($stopperChar = false) + public function readline ($propo = "", $stopperChar = false) // {{{ { // Gets can not delete chars before the call. Keep the prompt (if exists) + if (! is_string ($propo)) + $this->consoleException ("Invalid proposition provided to readline"); $prompt = $this->lineContent; $minLength = mb_strlen ($this->lineContent) + 1; - // The cursor position from first char of line. - $cursorPos = $minLength; // Manage the history and a temporary buffer if the user has already type // something before calling the history $historyTmp = ""; $historyPos = count ($this->history); - $string = ""; + $string = $propo; + echo $string; + $this->lineContent = $prompt.$string; + // The cursor position from last char of line. + $cursorPos = mb_strlen ($this->lineContent) + 1; while (1) { $char = $this->getKey (); @@ -368,11 +375,11 @@ private $usleep = 0; // Remove the previous char (Backspace) // {{{ { - if ($cursorPos -1 <= $minLength) + if ($cursorPos <= $minLength) continue; $strArr = $this->mb_str_split ($string); $cursorPos--; - unset ($strArr[$cursorPos - $minLength - 1]); + unset ($strArr[$cursorPos - $minLength]); $string = implode ($strArr); $this->rewriteLine ($prompt.$string); $this->moveCursor ($cursorPos);