Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[Console][WIP][POC] Add support for arrow keys in QuestionHelper#36700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -109,6 +109,8 @@ private function doAsk(OutputInterface $output, Question $question) | ||
$inputStream = $this->inputStream ?: STDIN; | ||
$autocomplete = $question->getAutocompleterCallback(); | ||
$cursor = new Cursor($output); | ||
if (null === $autocomplete || !Terminal::hasSttyAvailable()) { | ||
$ret = false; | ||
if ($question->isHidden()) { | ||
@@ -123,10 +125,80 @@ private function doAsk(OutputInterface $output, Question $question) | ||
} | ||
if (false === $ret) { | ||
if (!Terminal::hasSttyAvailable()) { | ||
$ret = fgets($inputStream, 4096); | ||
if (false === $ret) { | ||
throw new MissingInputException('Aborted.'); | ||
} | ||
} else { | ||
$sttyMode = shell_exec('stty -g'); | ||
shell_exec('stty -icanon -echo'); | ||
$k = null; | ||
$string = ''; | ||
[$x] = $cursor->getCurrentPosition(); | ||
while (10 !== $k && 0 !== $k) { | ||
[$pos] = $cursor->getCurrentPosition(); | ||
$pos -= $x; | ||
$ret = fgetc($inputStream); | ||
if (false === $ret && '' === $string) { | ||
shell_exec(sprintf('stty %s', $sttyMode)); | ||
throw new MissingInputException('Aborted.'); | ||
} | ||
$k = \ord($ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. To make this code maintainable in the future, we could rename some variables. E.g. | ||
if (27 === $k) { | ||
fgetc($inputStream); | ||
$k = \ord(fgetc($inputStream)); | ||
switch (true) { | ||
case 67 === $k && $pos < self::strlen($string): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Please, let's define some constants to avoid these "magic numbers". E.g.: caseself::ARROW_RIGHT ===$pressedKey:// ...caseself::ARROW_LEFT ===$pressedKey:// ...// ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I would highly appreciate that. I read the code and it took some time to dechipher what these numbers are. | ||
$cursor->moveRight(); | ||
break; | ||
case 68 === $k && $pos > 0: | ||
$cursor->moveLeft(); | ||
break; | ||
case 51 === $k && $pos >= 0: | ||
$string = self::substr($string, 0, $pos).self::substr($string, $pos + 1); | ||
$cursor->clearLineAfter(); | ||
$output->write(self::substr($string, $pos)); | ||
$cursor->moveToColumn($pos + $x); | ||
break; | ||
} | ||
} elseif (127 === $k) { | ||
if ($pos > 0) { | ||
$string = self::substr($string, 0, $pos - 1).self::substr($string, $pos); | ||
$cursor->moveToColumn($x); | ||
$output->write($string); | ||
$cursor->clearLineAfter() | ||
->moveToColumn(($pos + $x) - 1); | ||
} | ||
} elseif ($k >= 32 && $k <= 126) { | ||
if ($pos > 0 && $pos < \strlen($string)) { | ||
$string = self::substr($string, 0, $pos).$ret.self::substr($string, $pos); | ||
$output->write($ret.self::substr($string, $pos + 1)); | ||
$cursor->clearLineAfter() | ||
->moveToColumn($pos + $x + 1); | ||
} else { | ||
$string .= $ret; | ||
$output->write($ret); | ||
} | ||
} else { | ||
$output->write($ret); | ||
} | ||
} | ||
shell_exec(sprintf('stty %s', $sttyMode)); | ||
$ret = $string; | ||
} | ||
if ($question->isTrimmable()) { | ||
$ret = trim($ret); | ||
} | ||