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
This commit is contained in:
2018-06-13 12:12:54 +00:00
parent cb93127fa2
commit e5e56b66c0

View File

@@ -232,21 +232,28 @@ private $usleep = 0;
/** Get the line of characters pressed by the user and return the result. /** Get the line of characters pressed by the user and return the result.
* Stop when the user valid by \n. * Stop when the user valid by \n.
* Manage correctely the backspace, the Ctrl+W to remove word... * 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 * @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) // 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; $prompt = $this->lineContent;
$minLength = mb_strlen ($this->lineContent) + 1; $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 // Manage the history and a temporary buffer if the user has already type
// something before calling the history // something before calling the history
$historyTmp = ""; $historyTmp = "";
$historyPos = count ($this->history); $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) while (1)
{ {
$char = $this->getKey (); $char = $this->getKey ();
@@ -368,11 +375,11 @@ private $usleep = 0;
// Remove the previous char (Backspace) // Remove the previous char (Backspace)
// {{{ // {{{
{ {
if ($cursorPos -1 <= $minLength) if ($cursorPos <= $minLength)
continue; continue;
$strArr = $this->mb_str_split ($string); $strArr = $this->mb_str_split ($string);
$cursorPos--; $cursorPos--;
unset ($strArr[$cursorPos - $minLength - 1]); unset ($strArr[$cursorPos - $minLength]);
$string = implode ($strArr); $string = implode ($strArr);
$this->rewriteLine ($prompt.$string); $this->rewriteLine ($prompt.$string);
$this->moveCursor ($cursorPos); $this->moveCursor ($cursorPos);