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
This commit is contained in:
2019-10-01 18:40:42 +00:00
parent abdbd245a2
commit 6559355d54

View File

@@ -297,74 +297,84 @@ class console
// Manage autocompletion // 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) && $isAssoc = is_array ($completeArr) &&
array_diff_key ($completeArr, array_keys (array_keys ($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) if ($isAssoc)
$string .= key ($completeArr); $val = $key;
else if (mb_substr ($val, 0, mb_strlen ($end)) !== $end)
$string .= reset ($completeArr); unset ($completeArr[$key]);
} }
elseif (is_array ($completeArr) && count ($completeArr)) if (count ($completeArr) === 1)
{ {
// Multiple answers : display them // One entry : add a space to put on the next
// Manage if all the answers start by the same chars : add them to the if ($start !== "")
// $string $start .= " ";
if ($isAssoc) if ($isAssoc)
$possibilities = array_keys ($completeArr); $string = $start.key ($completeArr)." ";
else else
$possibilities = $completeArr; $string = $start.reset ($completeArr)." ";
$addChars = $this->shortestIdenticalValues ($possibilities); }
if ($addChars !== "") 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, " "); $maxlen = max ($maxlen, mb_strlen ($key));
if ($pos === false)
$string = $addChars;
else
{
$string = mb_substr ($string, 0, $pos + 1).$addChars;
}
} }
if ($isAssoc) $maxlen = $maxlen + 5;
foreach ($completeArr as $key => $val)
{ {
// In associative array, the key is the possible answer to if ($isAssoc)
// 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)
printf ("%-${maxlen}s %s\n", $key, $val); printf ("%-${maxlen}s %s\n", $key, $val);
elseif (trim ($val) === "")
// TODO : Define the string to display for ending string
echo "<br>\n";
else
echo "$val\n";
} }
else $addChars = $this->shortestIdenticalValues ($completeArr);
{ if ($addChars === "")
echo "\n".implode ("\n", $completeArr)."\n"; $addChars = $end;
} if ($start !== "")
$start .= " ";
$string = $start.$addChars;
}
else
{
$string = $start.$end;
} }
if (is_array ($completeArr) && count ($completeArr)) if (is_array ($completeArr) && count ($completeArr))
{ {
@@ -935,7 +945,7 @@ class console
* @param string $line The line to tokenize * @param string $line The line to tokenize
* @return array The tokens * @return array The tokens
*/ */
static public function tokenize ($line) private function tokenize ($line)
// {{{ // {{{
{ {
$tokens = array (); $tokens = array ();