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:
57
console.php
57
console.php
@@ -317,14 +317,37 @@ private $usleep = 0;
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Multiple answers : display them
|
// 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)
|
if ($isAssoc)
|
||||||
{
|
{
|
||||||
// In associative array, the key is the possible answer to
|
// In associative array, the key is the possible answer to
|
||||||
// autocompletion, and the value is the helper message
|
// autocompletion, and the value is the helper message
|
||||||
|
|
||||||
// Get the largest key length to make a beautiful alignment
|
// Get the largest key length to make a beautiful alignment
|
||||||
|
// Get the smaller key length to found a affined answer
|
||||||
$maxlen = 0;
|
$maxlen = 0;
|
||||||
foreach ($completeArr as $key => $val)
|
foreach ($completeArr as $key => $val)
|
||||||
|
{
|
||||||
$maxlen = max ($maxlen, mb_strlen ($key));
|
$maxlen = max ($maxlen, mb_strlen ($key));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display the keys and helpers in an aligned form
|
||||||
$maxlen = $maxlen + 5;
|
$maxlen = $maxlen + 5;
|
||||||
echo "\n";
|
echo "\n";
|
||||||
foreach ($completeArr as $key => $val)
|
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);
|
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;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user