Skip to content

Commit

Permalink
CLI: Select: Wrap & color option-keys & code review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Wolf Wortmann <[email protected]>
  • Loading branch information
element-code committed Feb 2, 2021
1 parent 1fa44d0 commit 41e749b
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,21 @@ public static function prompt(string $field, $options = null, string $validation
else
{
$extraOutput = '[' . $extraOutputDefault . ', ' . implode(', ', $opts) . ']';
$validation .= '|in_list[' . implode(',', $options) . ']';
$validation .= '|in_list[' . implode(', ', $options) . ']';
$validation = trim($validation, '|');
}

$default = $options[0];
}

static::fwrite(STDOUT, $field . (empty($field) ? ' ' : '') . $extraOutput . ': ');
static::fwrite(STDOUT, $field . (trim($field) ? ' ' : '') . $extraOutput . ': ');

// Read the input from keyboard.
$input = trim(static::input()) ?: $default;

if (isset($validation))
{
while (! static::validate($field, $input, $validation))
while (! static::validate(trim($field), $input, $validation))
{
$input = static::prompt($field, $options, $validation);
}
Expand All @@ -279,29 +279,44 @@ public static function prompt(string $field, $options = null, string $validation
/**
* Asks the user to select a specific option out of a set of options based on the option's key
*
* @param string $text Output "field" text
* @param array $options a list of options (array(key => description)), the first option will be the default value
* @param string|array $text Output "field" text or an one or two value array where the first value is the text before listing the options
* and the second value the text before asking to select one option. Provide empty string to omit
* @param array $options A list of options (array(key => description)), the first option will be the default value
* @param string|array|null $validation Validation rules
*
* @return string The selected key
* @return string The selected key of $options
*
* @codeCoverageIgnore
*/
public static function promptSelect(string $text, array $options, $validation = null): string
public static function promptSelect($text, array $options, $validation = null): string
{
if (!$options) {
throw new InvalidArgumentException('No options to select from provided');
if (is_string($text))
{
$text = [$text];
}
else if (!is_array($text))
{
throw new InvalidArgumentException('$text can only be of type string|array');
}

CLI::write($text);
if (!$options)
{
throw new InvalidArgumentException('No options to select from were provided');
}

$keyMaxLength = max(array_map('mb_strlen', array_keys($options)));
foreach ($options as $key => $description){
$name = ' ' . str_pad($key, $keyMaxLength + 2, ' ');
CLI::write($name . CLI::wrap($description, 125, strlen($name)));
if($line = array_shift($text))
{
CLI::write($line);
}

$keyMaxLength = max(array_map('mb_strwidth', array_keys($options)));
foreach ($options as $key => $description)
{
$name = str_pad(' [' . $key . ']', $keyMaxLength + 6, ' ');
CLI::write(CLI::color($name, 'green') . CLI::wrap($description, 125, $keyMaxLength + 6));
}

return static::prompt(PHP_EOL, array_keys($options), $validation);
return static::prompt(PHP_EOL . array_shift($text), array_keys($options), $validation);
}

//--------------------------------------------------------------------
Expand Down

0 comments on commit 41e749b

Please sign in to comment.