| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const color = require('kleur');
|
|---|
| 4 |
|
|---|
| 5 | const Prompt = require('./prompt');
|
|---|
| 6 |
|
|---|
| 7 | const _require = require('../util'),
|
|---|
| 8 | style = _require.style,
|
|---|
| 9 | clear = _require.clear;
|
|---|
| 10 |
|
|---|
| 11 | const _require2 = require('sisteransi'),
|
|---|
| 12 | erase = _require2.erase,
|
|---|
| 13 | cursor = _require2.cursor;
|
|---|
| 14 | /**
|
|---|
| 15 | * ConfirmPrompt Base Element
|
|---|
| 16 | * @param {Object} opts Options
|
|---|
| 17 | * @param {String} opts.message Message
|
|---|
| 18 | * @param {Boolean} [opts.initial] Default value (true/false)
|
|---|
| 19 | * @param {Stream} [opts.stdin] The Readable stream to listen to
|
|---|
| 20 | * @param {Stream} [opts.stdout] The Writable stream to write readline data to
|
|---|
| 21 | * @param {String} [opts.yes] The "Yes" label
|
|---|
| 22 | * @param {String} [opts.yesOption] The "Yes" option when choosing between yes/no
|
|---|
| 23 | * @param {String} [opts.no] The "No" label
|
|---|
| 24 | * @param {String} [opts.noOption] The "No" option when choosing between yes/no
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | class ConfirmPrompt extends Prompt {
|
|---|
| 29 | constructor(opts = {}) {
|
|---|
| 30 | super(opts);
|
|---|
| 31 | this.msg = opts.message;
|
|---|
| 32 | this.value = opts.initial;
|
|---|
| 33 | this.initialValue = !!opts.initial;
|
|---|
| 34 | this.yesMsg = opts.yes || 'yes';
|
|---|
| 35 | this.yesOption = opts.yesOption || '(Y/n)';
|
|---|
| 36 | this.noMsg = opts.no || 'no';
|
|---|
| 37 | this.noOption = opts.noOption || '(y/N)';
|
|---|
| 38 | this.render();
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | reset() {
|
|---|
| 42 | this.value = this.initialValue;
|
|---|
| 43 | this.fire();
|
|---|
| 44 | this.render();
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | exit() {
|
|---|
| 48 | this.abort();
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | abort() {
|
|---|
| 52 | this.done = this.aborted = true;
|
|---|
| 53 | this.fire();
|
|---|
| 54 | this.render();
|
|---|
| 55 | this.out.write('\n');
|
|---|
| 56 | this.close();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | submit() {
|
|---|
| 60 | this.value = this.value || false;
|
|---|
| 61 | this.done = true;
|
|---|
| 62 | this.aborted = false;
|
|---|
| 63 | this.fire();
|
|---|
| 64 | this.render();
|
|---|
| 65 | this.out.write('\n');
|
|---|
| 66 | this.close();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | _(c, key) {
|
|---|
| 70 | if (c.toLowerCase() === 'y') {
|
|---|
| 71 | this.value = true;
|
|---|
| 72 | return this.submit();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (c.toLowerCase() === 'n') {
|
|---|
| 76 | this.value = false;
|
|---|
| 77 | return this.submit();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | return this.bell();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | render() {
|
|---|
| 84 | if (this.closed) return;
|
|---|
| 85 | if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText, this.out.columns));
|
|---|
| 86 | super.render();
|
|---|
| 87 | this.outputText = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), this.done ? this.value ? this.yesMsg : this.noMsg : color.gray(this.initialValue ? this.yesOption : this.noOption)].join(' ');
|
|---|
| 88 | this.out.write(erase.line + cursor.to(0) + this.outputText);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | module.exports = ConfirmPrompt; |
|---|