From 0bd11eeda6757b6a41273babd958026a0f187495 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 27 Sep 2019 13:07:55 +0000 Subject: [PATCH] console : manage the history without error (bug introduced with the time of each command stored) console : add a tokenize method to get the user data from line git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5547 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/console.php b/console.php index f62095f..a3bced2 100644 --- a/console.php +++ b/console.php @@ -498,7 +498,8 @@ class console if ($historyPos > 0) { $historyPos--; - $string = $this->history[$historyPos]; + $slice = array_slice ($this->history, $historyPos, 1); + $string = reset ($slice); $cursorPos = mb_strlen ($prompt.$string) + 1; $this->rewriteLine ($prompt.$string); $this->moveCursor ($cursorPos); @@ -513,7 +514,8 @@ class console if ($historyPos < count ($this->history) - 1) { $historyPos++; - $string = $this->history[$historyPos]; + $slice = array_slice ($this->history, $historyPos, 1); + $string = reset ($slice); $cursorPos = mb_strlen ($prompt.$string) + 1; } elseif (isset ($historyTmp)) @@ -925,6 +927,30 @@ class console } // }}} + /** Tokenize the provided line and aggragate if there is single or double + * quotes. + * Trim the spaces + * @param string $line The line to tokenize + * @return array The tokens + */ + public function tokenize ($line) + // {{{ + { + $tokens = array (); + $token = strtok ($line,' '); + while ($token) + { + // find double quoted tokens + if ($token{0}=='"') { $token .= ' '.strtok('"').'"'; } + // find single quoted tokens + if ($token{0}=="'") { $token .= ' '.strtok("'")."'"; } + $tokens[] = $token; + $token = strtok(' '); + } + return $tokens; + } + // }}} + /** This function return an array with each char, but supports UTF-8 * @param string $string The string to explode * @param integer $split_length The number of chars in each split