Example #1
0
module.exports = createPrompt(
  readline => ({
    onKeypress: (value, key, { cursorPosition = 0, choices }, setState) => {
      let newCursorPosition = cursorPosition;
      if (isUpKey(key) || isDownKey(key)) {
        const offset = isUpKey(key) ? -1 : 1;
        let selectedOption;

        while (!selectedOption || selectedOption.disabled) {
          newCursorPosition =
            (newCursorPosition + offset + choices.length) % choices.length;
          selectedOption = choices[newCursorPosition];
        }

        setState({ cursorPosition: newCursorPosition });
      } else if (isSpaceKey(key)) {
        setState({
          showHelpTip: false,
          choices: choices.map((choice, i) => {
            if (i === cursorPosition) {
              return Object.assign({}, choice, { checked: !choice.checked });
            }

            return choice;
          })
        });
      } else if (key.name === 'a') {
        const selectAll = Boolean(choices.find(choice => !choice.checked));
        setState({
          choices: choices.map(choice =>
            Object.assign({}, choice, { checked: selectAll })
          )
        });
      } else if (key.name === 'i') {
        setState({
          choices: choices.map(choice =>
            Object.assign({}, choice, { checked: !choice.checked })
          )
        });
      } else if (isNumberKey(key)) {
        // Adjust index to start at 1
        const position = Number(key.name) - 1;

        // Abort if the choice doesn't exists or if disabled
        if (!choices[position] || choices[position].disabled) {
          return;
        }

        setState({
          cursorPosition: position,
          choices: choices.map((choice, i) => {
            if (i === position) {
              return Object.assign({}, choice, { checked: !choice.checked });
            }

            return choice;
          })
        });
      }
    },
    mapStateToValue: ({ choices }) => {
      return choices.filter(choice => choice.checked).map(choice => choice.value);
    },
    paginator: new Paginator(readline)
  }),
  (state, { paginator }) => {
    const { prefix, choices, showHelpTip, cursorPosition = 0, pageSize = 7 } = state;
    const message = chalk.bold(state.message);

    if (state.status === 'done') {
      const selection = choices
        .filter(choice => choice.checked)
        .map(({ name, value }) => name || value);
      return `${prefix} ${message} ${chalk.cyan(selection.join(', '))}`;
    }

    let helpTip = '';
    if (showHelpTip !== false) {
      const keys = [
        `${chalk.cyan.bold('<space>')} to select`,
        `${chalk.cyan.bold('<a>')} to toggle all`,
        `${chalk.cyan.bold('<i>')} to invert selection`
      ];
      helpTip = ` (Press ${keys.join(', ')})`;
    }

    const allChoices = choices
      .map(({ name, value, checked, disabled }, index) => {
        const line = name || value;
        if (disabled) {
          return chalk.dim(` - ${line} (disabled)`);
        }

        const checkbox = checked ? chalk.green(figures.circleFilled) : figures.circle;
        if (index === cursorPosition) {
          return chalk.cyan(`${figures.pointer}${checkbox} ${line}`);
        }

        return ` ${checkbox} ${line}`;
      })
      .join('\n');
    const windowedChoices = paginator.paginate(allChoices, cursorPosition, pageSize);
    return `${prefix} ${message}${helpTip}\n${windowedChoices}${cursorHide}`;
  }
);
Example #2
0
module.exports = createPrompt(
  readline => ({
    onKeypress: (value, key, { cursorPosition = 0, choices }, setState) => {
      let newCursorPosition = cursorPosition;
      const offset = isUpKey(key) ? -1 : 1;
      let selectedOption;

      while (!selectedOption || selectedOption.disabled) {
        newCursorPosition = Math.abs(newCursorPosition + offset) % choices.length;
        selectedOption = choices[newCursorPosition];
      }

      setState({
        cursorPosition: newCursorPosition,
        value: selectedOption.value
      });
    },
    paginator: new Paginator(readline)
  }),
  (state, { paginator }) => {
    const { prefix, message, choices, cursorPosition = 0, pageSize = 7 } = state;

    if (state.status === 'done') {
      const choice = choices[cursorPosition];
      return `${prefix} ${message} ${chalk.cyan(choice.name || choice.value)}`;
    }

    const allChoices = choices
      .map(({ name, value, disabled }, index) => {
        const line = name || value;
        if (disabled) {
          return chalk.dim(`- ${line} (disabled)`);
        }
        if (index === cursorPosition) {
          return chalk.cyan(`${figures.pointer} ${line}`);
        }
        return `  ${line}`;
      })
      .join('\n');
    const windowedChoices = paginator.paginate(allChoices, cursorPosition, pageSize);
    return `${prefix} ${message}\n${windowedChoices}${cursorHide}`;
  }
);
Example #3
0
const chalk = require('chalk');

module.exports = createPrompt(
  {
    canRemoveDefault: true,
    onKeypress: (value, key, { canRemoveDefault }, setState) => {
      const newState = { canRemoveDefault: !value };

      // Allow user to remove the default value by pressing backspace
      if (canRemoveDefault && key.name === 'backspace') {
        newState.default = undefined;
      }
      setState(newState);
    }
  },
  state => {
    const { prefix, message, value = '', status } = state;
    let formattedValue = value;
    if (status === 'done') {
      formattedValue = chalk.cyan(value);
    }

    let defaultValue = '';
    if (state.default && status !== 'done' && !value) {
      defaultValue = chalk.dim(` (${state.default})`);
    }

    return `${prefix} ${message}${defaultValue} ${formattedValue}`;
  }
);
Example #4
0
const { createPrompt } = require('@inquirer/core');
const chalk = require('chalk');

module.exports = createPrompt(
  {
    mapStateToValue({ value, default: rawDefault }) {
      if (value) {
        return /^y(es)?/i.test(value);
      }

      return rawDefault !== false;
    }
  },
  (state, { mapStateToValue }) => {
    const { prefix, value = '', status } = state;
    const message = chalk.bold(state.message);
    let formattedValue = value;
    if (status === 'done') {
      const value = mapStateToValue(state);
      formattedValue = chalk.cyan(value ? 'yes' : 'no');
    }

    let defaultValue = '';
    if (status !== 'done') {
      defaultValue = chalk.dim(state.default === false ? ' (y/N)' : ' (Y/n)');
    }

    return `${prefix} ${message}${defaultValue} ${formattedValue}`;
  }
);