From 6559355d54ee6f17df54722ab33d628a378aa512 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 1 Oct 2019 18:40:42 +0000 Subject: [PATCH] console : rewrite the call analyzis to simplify it. Pass a split part of the string to the method git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5565 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 122 ++++++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/console.php b/console.php index 6d4d030..16b8399 100644 --- a/console.php +++ b/console.php @@ -297,74 +297,84 @@ class console // Manage autocompletion // {{{ { - $completeArr = call_user_func ($this->completionFunction, $string); + // Take the last part of the string without space or double quotes + $pos = strrpos ($string, " "); + if ($pos === false) + { + // No space : put all in end + $start = ""; + $end = $string; + } + elseif ($pos === mb_strlen ($string)) + { + // Last char is a space : put all in start + $start = $string; + $end = ""; + } + else + { + // Last char is not a space, end is the last word and start is the + // begin to before last word + $start = mb_substr ($string, 0, $pos); + $end = mb_substr ($string, $pos + 1); + } + $completeArr = call_user_func ($this->completionFunction, + $this->tokenize ($start)); $isAssoc = is_array ($completeArr) && array_diff_key ($completeArr, array_keys (array_keys ($completeArr))); - if (is_array ($completeArr) && count ($completeArr) === 1) + // Remove from completeArr the proposed values which doesn't match with + // $end (invalid proposals) + foreach ($completeArr as $key => $val) { - if (substr ($string, -1) !== " ") - { - // Continuous word is unique : replace the last word by the proposal - $pos = mb_strrpos ($string, " "); - if ($pos === false) - $string = ""; - else - { - $string = mb_substr ($string, 0, $pos +1); - } - } - else - { - // Next word is unique : add this to the string - } if ($isAssoc) - $string .= key ($completeArr); - else - $string .= reset ($completeArr); + $val = $key; + if (mb_substr ($val, 0, mb_strlen ($end)) !== $end) + unset ($completeArr[$key]); } - elseif (is_array ($completeArr) && count ($completeArr)) + if (count ($completeArr) === 1) { - // Multiple answers : display them - // Manage if all the answers start by the same chars : add them to the - // $string + // One entry : add a space to put on the next + if ($start !== "") + $start .= " "; if ($isAssoc) - $possibilities = array_keys ($completeArr); + $string = $start.key ($completeArr)." "; else - $possibilities = $completeArr; - $addChars = $this->shortestIdenticalValues ($possibilities); - if ($addChars !== "") + $string = $start.reset ($completeArr)." "; + } + elseif (count ($completeArr)) + { + // Multiple entries : display them to allow the user to choose + echo "\n"; + // In associative array, the key is the possible answer to + // autocompletion, and the value is the helper message + // Get the largest key length to make a beautiful alignment + // Get the smaller key length to found a affined answer + $maxlen = 0; + foreach ($completeArr as $key => $val) { - $pos = mb_strrpos ($string, " "); - if ($pos === false) - $string = $addChars; - else - { - $string = mb_substr ($string, 0, $pos + 1).$addChars; - } + $maxlen = max ($maxlen, mb_strlen ($key)); } - if ($isAssoc) + $maxlen = $maxlen + 5; + foreach ($completeArr as $key => $val) { - // In associative array, the key is the possible answer to - // autocompletion, and the value is the helper message - - // Get the largest key length to make a beautiful alignment - // Get the smaller key length to found a affined answer - $maxlen = 0; - foreach ($completeArr as $key => $val) - { - $maxlen = max ($maxlen, mb_strlen ($key)); - } - - // Display the keys and helpers in an aligned form - $maxlen = $maxlen + 5; - echo "\n"; - foreach ($completeArr as $key => $val) + if ($isAssoc) printf ("%-${maxlen}s %s\n", $key, $val); + elseif (trim ($val) === "") + // TODO : Define the string to display for ending string + echo "
\n"; + else + echo "$val\n"; } - else - { - echo "\n".implode ("\n", $completeArr)."\n"; - } + $addChars = $this->shortestIdenticalValues ($completeArr); + if ($addChars === "") + $addChars = $end; + if ($start !== "") + $start .= " "; + $string = $start.$addChars; + } + else + { + $string = $start.$end; } if (is_array ($completeArr) && count ($completeArr)) { @@ -935,7 +945,7 @@ class console * @param string $line The line to tokenize * @return array The tokens */ - static public function tokenize ($line) + private function tokenize ($line) // {{{ { $tokens = array ();