| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\View\Components\TwoColumnDetail;
|
|
|
6 |
use Symfony\Component\Console\Formatter\OutputFormatter;
|
|
|
7 |
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
|
|
|
8 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
9 |
use Symfony\Component\Console\Question\ChoiceQuestion;
|
|
|
10 |
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
|
|
11 |
use Symfony\Component\Console\Question\Question;
|
|
|
12 |
|
|
|
13 |
class QuestionHelper extends SymfonyQuestionHelper
|
|
|
14 |
{
|
|
|
15 |
/**
|
|
|
16 |
* {@inheritdoc}
|
|
|
17 |
*/
|
|
|
18 |
protected function writePrompt(OutputInterface $output, Question $question)
|
|
|
19 |
{
|
|
|
20 |
$text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
|
|
|
21 |
|
|
|
22 |
$text = $this->ensureEndsWithPunctuation($text);
|
|
|
23 |
|
|
|
24 |
$text = " <fg=default;options=bold>$text</></>";
|
|
|
25 |
|
|
|
26 |
$default = $question->getDefault();
|
|
|
27 |
|
|
|
28 |
if ($question->isMultiline()) {
|
|
|
29 |
$text .= sprintf(' (press %s to continue)', 'Windows' == PHP_OS_FAMILY
|
|
|
30 |
? '<comment>Ctrl+Z</comment> then <comment>Enter</comment>'
|
|
|
31 |
: '<comment>Ctrl+D</comment>');
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
switch (true) {
|
|
|
35 |
case null === $default:
|
|
|
36 |
$text = sprintf('<info>%s</info>', $text);
|
|
|
37 |
|
|
|
38 |
break;
|
|
|
39 |
|
|
|
40 |
case $question instanceof ConfirmationQuestion:
|
|
|
41 |
$text = sprintf('<info>%s (yes/no)</info> [<comment>%s</comment>]', $text, $default ? 'yes' : 'no');
|
|
|
42 |
|
|
|
43 |
break;
|
|
|
44 |
|
|
|
45 |
case $question instanceof ChoiceQuestion:
|
|
|
46 |
$choices = $question->getChoices();
|
|
|
47 |
$text = sprintf('<info>%s</info> [<comment>%s</comment>]', $text, OutputFormatter::escape($choices[$default] ?? $default));
|
|
|
48 |
|
|
|
49 |
break;
|
| 399 |
lars |
50 |
|
|
|
51 |
default:
|
|
|
52 |
$text = sprintf('<info>%s</info> [<comment>%s</comment>]', $text, OutputFormatter::escape($default));
|
|
|
53 |
|
|
|
54 |
break;
|
| 148 |
lars |
55 |
}
|
|
|
56 |
|
|
|
57 |
$output->writeln($text);
|
|
|
58 |
|
|
|
59 |
if ($question instanceof ChoiceQuestion) {
|
|
|
60 |
foreach ($question->getChoices() as $key => $value) {
|
|
|
61 |
with(new TwoColumnDetail($output))->render($value, $key);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$output->write('<options=bold>❯ </>');
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Ensures the given string ends with punctuation.
|
|
|
70 |
*
|
|
|
71 |
* @param string $string
|
|
|
72 |
* @return string
|
|
|
73 |
*/
|
|
|
74 |
protected function ensureEndsWithPunctuation($string)
|
|
|
75 |
{
|
|
|
76 |
if (! str($string)->endsWith(['?', ':', '!', '.'])) {
|
|
|
77 |
return "$string:";
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return $string;
|
|
|
81 |
}
|
|
|
82 |
}
|