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);