console : Allow to autocomplete with value+help, or value only
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4235 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
38
console.php
38
console.php
@@ -240,30 +240,50 @@ class console
|
|||||||
// {{{
|
// {{{
|
||||||
{
|
{
|
||||||
$completeArr = call_user_func ($this->completionFunction, $string);
|
$completeArr = call_user_func ($this->completionFunction, $string);
|
||||||
|
$isAssoc = is_array ($completeArr) &&
|
||||||
|
array_diff_key ($completeArr, array_keys (array_keys ($completeArr)));
|
||||||
if (count ($completeArr) === 1)
|
if (count ($completeArr) === 1)
|
||||||
{
|
{
|
||||||
if (substr ($string, -1) === " ")
|
if (substr ($string, -1) !== " ")
|
||||||
{
|
|
||||||
// Next word is unique : add this to the string
|
|
||||||
$string .= reset ($completeArr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
// Continuous word is unique : replace the last word by the proposal
|
// Continuous word is unique : replace the last word by the proposal
|
||||||
$pos = mb_strrpos ($string, " ");
|
$pos = mb_strrpos ($string, " ");
|
||||||
if ($pos === false)
|
if ($pos === false)
|
||||||
$string = reset ($completeArr);
|
$string = "";
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$string = mb_substr ($string, 0, $pos +1);
|
$string = mb_substr ($string, 0, $pos +1);
|
||||||
$string .= reset ($completeArr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Next word is unique : add this to the string
|
||||||
|
}
|
||||||
|
if ($isAssoc)
|
||||||
|
$string .= key ($completeArr);
|
||||||
|
else
|
||||||
|
$string .= reset ($completeArr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Multiple answers : display them
|
// Multiple answers : display them
|
||||||
echo "\n".implode ("\n", $completeArr)."\n";
|
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
|
||||||
|
$maxlen = 0;
|
||||||
|
foreach ($completeArr as $key => $val)
|
||||||
|
$maxlen = max ($maxlen, mb_strlen ($key));
|
||||||
|
$maxlen = $maxlen + 5;
|
||||||
|
echo "\n";
|
||||||
|
foreach ($completeArr as $key => $val)
|
||||||
|
printf ("%-${maxlen}s %s\n", $key, $val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
echo "\n".implode ("\n", $completeArr)."\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$cursorPos = mb_strlen ($prompt.$string);
|
$cursorPos = mb_strlen ($prompt.$string);
|
||||||
$this->rewriteLine ($prompt.$string);
|
$this->rewriteLine ($prompt.$string);
|
||||||
|
|||||||
Reference in New Issue
Block a user