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