Skip to content

Commit

Permalink
Port base Symfony choice helper logic
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 28, 2023
1 parent 320606e commit e091f4d
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions packages/framework/src/Console/Concerns/BetterChoiceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,54 @@

namespace Hyde\Console\Concerns;

use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;

use function max;
use function sprintf;
use function array_map;
use function str_repeat;
use function array_keys;
use function func_get_args;

/**
* @interal Adds a better choice helper for console commands.
*/
trait BetterChoiceHelper
{
protected function betterChoice(): string
private int $retryCount = 0;

protected function betterChoice(string $question, array $choices, int|string $default = null): string|array

Check warning on line 27 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L27

Added line #L27 was not covered by tests
{
//
if ($this->input->isInteractive() && $this->retryCount === 0) {
$this->newLine();

Check warning on line 30 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L29-L30

Added lines #L29 - L30 were not covered by tests
}

$this->line(sprintf(' <info>%s</info> [<comment>%s</comment>]:', $question, OutputFormatter::escape($choices[$default] ?? $default)));

Check warning on line 33 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L33

Added line #L33 was not covered by tests

$maxWidth = max(array_map([Helper::class, 'width'], array_keys($choices)));
foreach ($choices as $key => $value) {
$padding = str_repeat(' ', $maxWidth - Helper::width((string) $key));
$this->line(sprintf(" [<comment>%s$padding</comment>] %s", $key, $value));

Check warning on line 38 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L35-L38

Added lines #L35 - L38 were not covered by tests
}

$this->output->write(' > ');
$answer = (new SymfonyQuestionHelper())->ask($this->input, new NullOutput, new Question($question, $default));

Check warning on line 42 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L41-L42

Added lines #L41 - L42 were not covered by tests

$selection = $choices[$answer] ?? null;

Check warning on line 44 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L44

Added line #L44 was not covered by tests

if ($selection === null) {
$this->retryCount++;
$this->output->error(sprintf('Invalid option "%s"', $answer));
$this->betterChoice(...func_get_args());

Check warning on line 49 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L46-L49

Added lines #L46 - L49 were not covered by tests
} else {
$this->newLine();
$this->retryCount = 0;

Check warning on line 52 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L51-L52

Added lines #L51 - L52 were not covered by tests
}

return $selection;

Check warning on line 55 in packages/framework/src/Console/Concerns/BetterChoiceHelper.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Console/Concerns/BetterChoiceHelper.php#L55

Added line #L55 was not covered by tests
}
}

0 comments on commit e091f4d

Please sign in to comment.