This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+77
-40
@@ -55,7 +55,7 @@ class QuestionHelper extends Helper
|
||||
}
|
||||
|
||||
$inputStream = $input instanceof StreamableInputInterface ? $input->getStream() : null;
|
||||
$inputStream ??= STDIN;
|
||||
$inputStream ??= \STDIN;
|
||||
|
||||
try {
|
||||
if (!$question->getValidator()) {
|
||||
@@ -211,7 +211,7 @@ class QuestionHelper extends Helper
|
||||
foreach ($choices as $key => $value) {
|
||||
$padding = str_repeat(' ', $maxWidth - self::width($key));
|
||||
|
||||
$messages[] = sprintf(" [<$tag>%s$padding</$tag>] %s", $key, $value);
|
||||
$messages[] = \sprintf(" [<$tag>%s$padding</$tag>] %s", $key, $value);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
@@ -234,7 +234,8 @@ class QuestionHelper extends Helper
|
||||
/**
|
||||
* Autocompletes a question.
|
||||
*
|
||||
* @param resource $inputStream
|
||||
* @param resource $inputStream
|
||||
* @param callable(string):string[] $autocomplete
|
||||
*/
|
||||
private function autocomplete(OutputInterface $output, Question $question, $inputStream, callable $autocomplete): string
|
||||
{
|
||||
@@ -247,11 +248,7 @@ class QuestionHelper extends Helper
|
||||
$ofs = -1;
|
||||
$matches = $autocomplete($ret);
|
||||
$numMatches = \count($matches);
|
||||
|
||||
$sttyMode = shell_exec('stty -g');
|
||||
$isStdin = 'php://stdin' === (stream_get_meta_data($inputStream)['uri'] ?? null);
|
||||
$r = [$inputStream];
|
||||
$w = [];
|
||||
$inputHelper = new TerminalInputHelper($inputStream);
|
||||
|
||||
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
|
||||
shell_exec('stty -icanon -echo');
|
||||
@@ -261,16 +258,14 @@ class QuestionHelper extends Helper
|
||||
|
||||
// Read a keypress
|
||||
while (!feof($inputStream)) {
|
||||
while ($isStdin && 0 === @stream_select($r, $w, $w, 0, 100)) {
|
||||
// Give signal handlers a chance to run
|
||||
$r = [$inputStream];
|
||||
}
|
||||
$inputHelper->waitForInput();
|
||||
$c = fread($inputStream, 1);
|
||||
|
||||
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
|
||||
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
|
||||
shell_exec('stty '.$sttyMode);
|
||||
throw new MissingInputException('Aborted.');
|
||||
// Restore the terminal so it behaves normally again
|
||||
$inputHelper->finish();
|
||||
throw new MissingInputException('Aborted while asking: '.$question->getQuestion());
|
||||
} elseif ("\177" === $c) { // Backspace Character
|
||||
if (0 === $numMatches && 0 !== $i) {
|
||||
--$i;
|
||||
@@ -306,19 +301,19 @@ class QuestionHelper extends Helper
|
||||
$ofs += ('A' === $c[2]) ? -1 : 1;
|
||||
$ofs = ($numMatches + $ofs) % $numMatches;
|
||||
}
|
||||
} elseif (\ord($c) < 32) {
|
||||
} elseif ('' === $c || \ord($c) < 32) {
|
||||
if ("\t" === $c || "\n" === $c) {
|
||||
if ($numMatches > 0 && -1 !== $ofs) {
|
||||
$ret = (string) $matches[$ofs];
|
||||
// Echo out remaining chars for current match
|
||||
$remainingCharacters = substr($ret, \strlen(trim($this->mostRecentlyEnteredValue($fullChoice))));
|
||||
$remainingCharacters = substr($ret, \strlen($this->mostRecentlyEnteredValue($fullChoice)));
|
||||
$output->write($remainingCharacters);
|
||||
$fullChoice .= $remainingCharacters;
|
||||
$i = (false === $encoding = mb_detect_encoding($fullChoice, null, true)) ? \strlen($fullChoice) : mb_strlen($fullChoice, $encoding);
|
||||
|
||||
$matches = array_filter(
|
||||
$autocomplete($ret),
|
||||
fn ($match) => '' === $ret || str_starts_with($match, $ret)
|
||||
static fn ($match) => '' === $ret || str_starts_with($match, $ret)
|
||||
);
|
||||
$numMatches = \count($matches);
|
||||
$ofs = -1;
|
||||
@@ -365,14 +360,14 @@ class QuestionHelper extends Helper
|
||||
if ($numMatches > 0 && -1 !== $ofs) {
|
||||
$cursor->savePosition();
|
||||
// Write highlighted text, complete the partially entered response
|
||||
$charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)));
|
||||
$charactersEntered = \strlen($this->mostRecentlyEnteredValue($fullChoice));
|
||||
$output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>');
|
||||
$cursor->restorePosition();
|
||||
}
|
||||
}
|
||||
|
||||
// Reset stty so it behaves normally again
|
||||
shell_exec('stty '.$sttyMode);
|
||||
// Restore the terminal so it behaves normally again
|
||||
$inputHelper->finish();
|
||||
|
||||
return $fullChoice;
|
||||
}
|
||||
@@ -384,12 +379,13 @@ class QuestionHelper extends Helper
|
||||
return $entered;
|
||||
}
|
||||
|
||||
$choices = explode(',', $entered);
|
||||
if ('' !== $lastChoice = trim($choices[\count($choices) - 1])) {
|
||||
return $lastChoice;
|
||||
if (false === $lastCommaPos = strrpos($entered, ',')) {
|
||||
return $entered;
|
||||
}
|
||||
|
||||
return $entered;
|
||||
$lastChoice = trim(substr($entered, $lastCommaPos + 1));
|
||||
|
||||
return '' !== $lastChoice ? $lastChoice : $entered;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -423,27 +419,25 @@ class QuestionHelper extends Helper
|
||||
return $value;
|
||||
}
|
||||
|
||||
$inputHelper = null;
|
||||
|
||||
if (self::$stty && Terminal::hasSttyAvailable()) {
|
||||
$sttyMode = shell_exec('stty -g');
|
||||
$inputHelper = new TerminalInputHelper($inputStream);
|
||||
shell_exec('stty -echo');
|
||||
} elseif ($this->isInteractiveInput($inputStream)) {
|
||||
throw new RuntimeException('Unable to hide the response.');
|
||||
}
|
||||
|
||||
$value = fgets($inputStream, 4096);
|
||||
$value = $this->doReadInput($inputStream, helper: $inputHelper);
|
||||
|
||||
if (4095 === \strlen($value)) {
|
||||
$errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
|
||||
$errOutput->warning('The value was possibly truncated by your shell or terminal emulator');
|
||||
}
|
||||
|
||||
if (self::$stty && Terminal::hasSttyAvailable()) {
|
||||
shell_exec('stty '.$sttyMode);
|
||||
}
|
||||
// Restore the terminal so it behaves normally again
|
||||
$inputHelper?->finish();
|
||||
|
||||
if (false === $value) {
|
||||
throw new MissingInputException('Aborted.');
|
||||
}
|
||||
if ($trimmable) {
|
||||
$value = trim($value);
|
||||
}
|
||||
@@ -471,6 +465,8 @@ class QuestionHelper extends Helper
|
||||
|
||||
try {
|
||||
return $question->getValidator()($interviewer());
|
||||
} catch (MissingInputException $e) {
|
||||
throw $error ?? $e;
|
||||
} catch (RuntimeException $e) {
|
||||
throw $e;
|
||||
} catch (\Exception $error) {
|
||||
@@ -501,9 +497,21 @@ class QuestionHelper extends Helper
|
||||
*/
|
||||
private function readInput($inputStream, Question $question): string|false
|
||||
{
|
||||
if (null !== $question->getTimeout() && $this->isInteractiveInput($inputStream)) {
|
||||
$read = [$inputStream];
|
||||
$write = null;
|
||||
$except = null;
|
||||
$timeoutSeconds = $question->getTimeout();
|
||||
$changedStreams = stream_select($read, $write, $except, $timeoutSeconds);
|
||||
|
||||
if (0 === $changedStreams) {
|
||||
throw new MissingInputException(\sprintf('Timed out after waiting for input for %d second%s.', $timeoutSeconds, 1 === $timeoutSeconds ? '' : 's'));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$question->isMultiline()) {
|
||||
$cp = $this->setIOCodepage();
|
||||
$ret = fgets($inputStream, 4096);
|
||||
$ret = $this->doReadInput($inputStream);
|
||||
|
||||
return $this->resetIOCodepage($cp, $ret);
|
||||
}
|
||||
@@ -513,13 +521,11 @@ class QuestionHelper extends Helper
|
||||
return false;
|
||||
}
|
||||
|
||||
$ret = '';
|
||||
$cp = $this->setIOCodepage();
|
||||
while (false !== ($char = fgetc($multiLineStreamReader))) {
|
||||
if (\PHP_EOL === "{$ret}{$char}") {
|
||||
break;
|
||||
}
|
||||
$ret .= $char;
|
||||
$ret = $this->doReadInput($multiLineStreamReader, "\x4");
|
||||
|
||||
if (stream_get_meta_data($inputStream)['seekable']) {
|
||||
fseek($inputStream, ftell($multiLineStreamReader));
|
||||
}
|
||||
|
||||
return $this->resetIOCodepage($cp, $ret);
|
||||
@@ -576,7 +582,7 @@ class QuestionHelper extends Helper
|
||||
|
||||
// For seekable and writable streams, add all the same data to the
|
||||
// cloned stream and then seek to the same offset.
|
||||
if (true === $seekable && !\in_array($mode, ['r', 'rb', 'rt'])) {
|
||||
if (true === $seekable && !\in_array($mode, ['r', 'rb', 'rt'], true)) {
|
||||
$offset = ftell($inputStream);
|
||||
rewind($inputStream);
|
||||
stream_copy_to_stream($inputStream, $cloneStream);
|
||||
@@ -586,4 +592,35 @@ class QuestionHelper extends Helper
|
||||
|
||||
return $cloneStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $inputStream
|
||||
*/
|
||||
private function doReadInput($inputStream, ?string $exitChar = null, ?TerminalInputHelper $helper = null): string
|
||||
{
|
||||
$ret = '';
|
||||
$helper ??= new TerminalInputHelper($inputStream, false);
|
||||
|
||||
while (!feof($inputStream)) {
|
||||
$helper->waitForInput();
|
||||
$char = fread($inputStream, 1);
|
||||
|
||||
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
|
||||
if (false === $char || ('' === $ret && '' === $char)) {
|
||||
throw new MissingInputException('Aborted.');
|
||||
}
|
||||
|
||||
if (\PHP_EOL === "{$ret}{$char}" || $exitChar === $char) {
|
||||
break;
|
||||
}
|
||||
|
||||
$ret .= $char;
|
||||
|
||||
if (null === $exitChar && "\n" === $char) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user