From b8c900720c3d15c4f6723698ae94da2bdfc32bbf Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 14 Jun 2018 09:21:31 +0000 Subject: [PATCH] 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 --- console.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/console.php b/console.php index f56163c..3b1db99 100644 --- a/console.php +++ b/console.php @@ -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; + } + // }}} }