Sha256: b1c3229d6a114b42d487cde3922559ad89bc1473f09f289e3bb6698ef0773836

Contents?: true

Size: 1.74 KB

Versions: 46

Compression:

Stored size: 1.74 KB

Contents

'use strict';
/**
 * `confirm` type prompt
 */

var _ = require('lodash');
var chalk = require('chalk');
var Base = require('./base');
var observe = require('../utils/events');

class ConfirmPrompt extends Base {
  constructor(questions, rl, answers) {
    super(questions, rl, answers);

    var rawDefault = true;

    _.extend(this.opt, {
      filter: function(input) {
        var value = rawDefault;
        if (input != null && input !== '') {
          value = /^y(es)?/i.test(input);
        }
        return value;
      }
    });

    if (_.isBoolean(this.opt.default)) {
      rawDefault = this.opt.default;
    }

    this.opt.default = rawDefault ? 'Y/n' : 'y/N';

    return this;
  }

  /**
   * Start the Inquiry session
   * @param  {Function} cb   Callback when prompt is done
   * @return {this}
   */

  _run(cb) {
    this.done = cb;

    // Once user confirm (enter key)
    var events = observe(this.rl);
    events.keypress.takeUntil(events.line).forEach(this.onKeypress.bind(this));

    events.line.take(1).forEach(this.onEnd.bind(this));

    // Init
    this.render();

    return this;
  }

  /**
   * Render the prompt to screen
   * @return {ConfirmPrompt} self
   */

  render(answer) {
    var message = this.getQuestion();

    if (typeof answer === 'boolean') {
      message += chalk.cyan(answer ? 'Yes' : 'No');
    } else {
      message += this.rl.line;
    }

    this.screen.render(message);

    return this;
  }

  /**
   * When user press `enter` key
   */

  onEnd(input) {
    this.status = 'answered';

    var output = this.opt.filter(input);
    this.render(output);

    this.screen.done();
    this.done(output);
  }

  /**
   * When user press a key
   */

  onKeypress() {
    this.render();
  }
}

module.exports = ConfirmPrompt;

Version data entries

46 entries across 46 versions & 3 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.21.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.20.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.19.1 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.19.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.18.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.17.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.16.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.15.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.14.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.13.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.12.2 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.12.1 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.12.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.11.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.10.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.9.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.8.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.7.0 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js
govuk_publishing_components-17.6.1 node_modules/standard/node_modules/inquirer/lib/prompts/confirm.js