You are here

function ConfigPartialExportCommands::_choice in Config Partial Export 8

Parameters

$options:

string $prompt:

string $label:

array $widths:

Return value

bool

1 call to ConfigPartialExportCommands::_choice()
ConfigPartialExportCommands::configPartialExport in src/Commands/ConfigPartialExportCommands.php
Command description here.

File

src/Commands/ConfigPartialExportCommands.php, line 276

Class

ConfigPartialExportCommands
Defines Drush commands for the Search API.

Namespace

Drupal\config_partial_export\Commands

Code

function _choice($options, $prompt = 'Enter a number.', $label = '!value', $widths = []) {
  $this
    ->output()
    ->writeln(dt($prompt));

  // Preflight so that all rows will be padded out to the same number of columns
  $array_pad = 0;
  foreach ($options as $key => $option) {
    if (is_array($option) && count($option) > $array_pad) {
      $array_pad = count($option);
    }
  }
  $rows[] = array_pad([
    '[0]',
    ':',
    'Cancel',
  ], $array_pad + 2, '');
  $selection_number = 0;
  foreach ($options as $key => $option) {
    if (substr($key, 0, 3) == '-- ' && substr($key, -3) == ' --') {
      $rows[] = array_pad([
        '',
        '',
        $option,
      ], $array_pad + 2, '');
    }
    else {
      $selection_number++;
      $row = [
        "[{$selection_number}]",
        ':',
      ];
      if (is_array($option)) {
        $row = array_merge($row, $option);
      }
      else {
        $row[] = dt($label, [
          '!number' => $selection_number,
          '!key' => $key,
          '!value' => $option,
        ]);
      }
      $rows[] = $row;
      $selection_list[$selection_number] = $key;
    }
  }
  drush_print_table($rows, FALSE, $widths);
  drush_print_pipe(array_keys($options));

  // If the user specified --choice, then make an
  // automatic selection.  Cancel if the choice is
  // not an available option.
  if (($choice = $this
    ->getConfig()
    ->get('choice', FALSE)) !== FALSE) {

    // First check to see if $choice is one of the symbolic options
    if (array_key_exists($choice, $options)) {
      return $choice;
    }
    elseif (array_key_exists($choice, $selection_list)) {
      return $selection_list[$choice];
    }
    return FALSE;
  }

  // If the user specified --no, then cancel; also avoid
  // getting hung up waiting for user input in --pipe and
  // backend modes.  If none of these apply, then wait,
  // for user input and return the selected result.
  if (!drush_get_context('DRUSH_NEGATIVE') && !drush_get_context('DRUSH_AFFIRMATIVE') && !drush_get_context('DRUSH_PIPE')) {
    while ($line = trim(fgets(STDIN))) {
      if (array_key_exists($line, $selection_list)) {
        return $selection_list[$line];
      }
    }
  }

  // We will allow --yes to confirm input if there is only
  // one choice; otherwise, --yes will cancel to avoid ambiguity
  if (drush_get_context('DRUSH_AFFIRMATIVE') && count($options) == 1) {
    return $selection_list[1];
  }
  return FALSE;
}