private function autocomplete($inputStream)
{
$autocomplete = $this->question->getAutocompleterValues();
$ret = '';
$i = 0;
$ofs = -1;
$matches = $autocomplete;
$numMatches = count($matches);
$sttyMode = shell_exec('stty -g');
shell_exec('stty -icanon -echo');
while (!feof($inputStream)) {
$c = fread($inputStream, 1);
if ("" === $c) {
if (0 === $numMatches && 0 !== $i) {
--$i;
$this->output->write("[1D");
}
if ($i === 0) {
$ofs = -1;
$matches = $autocomplete;
$numMatches = count($matches);
} else {
$numMatches = 0;
}
$ret = substr($ret, 0, $i);
} elseif ("" === $c) {
$c .= fread($inputStream, 2);
if (isset($c[2]) && ('A' === $c[2] || 'B' === $c[2])) {
if ('A' === $c[2] && -1 === $ofs) {
$ofs = 0;
}
if (0 === $numMatches) {
continue;
}
$ofs += 'A' === $c[2] ? -1 : 1;
$ofs = ($numMatches + $ofs) % $numMatches;
}
} elseif (ord($c) < 32) {
if ("\t" === $c || "\n" === $c) {
if ($numMatches > 0 && -1 !== $ofs) {
$ret = $matches[$ofs];
$this->output->write(substr($ret, $i));
$i = strlen($ret);
}
if ("\n" === $c) {
$this->output->write($c);
break;
}
$numMatches = 0;
}
continue;
} else {
$this->output->write($c);
$ret .= $c;
++$i;
$numMatches = 0;
$ofs = 0;
foreach ($autocomplete as $value) {
if (0 === strpos($value, $ret) && $i !== strlen($value)) {
$matches[$numMatches++] = $value;
}
}
}
$this->output->write("[K");
if ($numMatches > 0 && -1 !== $ofs) {
$this->output->write("7");
$this->output->highlight(substr($matches[$ofs], $i));
$this->output->write("8");
}
}
shell_exec(sprintf('stty %s', $sttyMode));
return $ret;
}