console : manage the autocompletion : add the identical possibilities to the string

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4247 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-06-14 09:21:31 +00:00
parent 7cce0f6d1d
commit b8c900720c

View File

@@ -317,14 +317,37 @@ private $usleep = 0;
else
{
// Multiple answers : display them
// Manage if all the answers start by the same chars : add them to the
// $string
if ($isAssoc)
$possibilities = array_keys ($completeArr);
else
$possibilities = $completeArr;
$addChars = $this->shortestIdenticalValues ($possibilities);
if ($addChars !== "")
{
$pos = mb_strrpos ($string, " ");
if ($pos === false)
$string = $addChars;
else
{
$string = mb_substr ($string, 0, $pos + 1).$addChars;
}
}
if ($isAssoc)
{
// 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)
@@ -810,4 +833,38 @@ private $usleep = 0;
file_put_contents ("/tmp/debug", date ("H:i:s")." $data\n", FILE_APPEND);
}
// }}}
/** Look in the array which first chars of each possibilites are identical.
* @param array $completeArr The values to examine
* @return string the identical chars
*/
private function shortestIdenticalValues ($completeArr)
// {{{
{
$minlen = 99999;
foreach ($completeArr as $val)
{
$minlen = min ($minlen, mb_strlen ($val));
}
$identicalString = "";
$sameCharLength = 1 ;
while ($sameCharLength <= $minlen)
{
$tmp = "";
foreach ($completeArr as $val)
{
$part = mb_substr ($val, 0, $sameCharLength);
if ($tmp == "")
$tmp = $part;
if ($tmp !== $part)
{
break 2;
}
}
$identicalString = $tmp;
$sameCharLength++;
}
return $identicalString;
}
// }}}
}