123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- const color = require('kleur');
- const Prompt = require('./prompt');
- const { style, clear } = require('../util');
- const { erase, cursor } = require('sisteransi');
- /**
- * ConfirmPrompt Base Element
- * @param {Object} opts Options
- * @param {String} opts.message Message
- * @param {Boolean} [opts.initial] Default value (true/false)
- */
- class ConfirmPrompt extends Prompt {
- constructor(opts={}) {
- super(opts);
- this.msg = opts.message;
- this.value = opts.initial;
- this.initialValue = !!opts.initial;
- this.render(true);
- }
- reset() {
- this.value = this.initialValue;
- this.fire();
- this.render();
- }
- abort() {
- this.done = this.aborted = true;
- this.fire();
- this.render();
- this.out.write('\n');
- this.close();
- }
- submit() {
- this.value = this.value || false;
- this.done = true;
- this.aborted = false;
- this.fire();
- this.render();
- this.out.write('\n');
- this.close();
- }
- _(c, key) {
- if (c.toLowerCase() === 'y') {
- this.value = true;
- return this.submit();
- }
- if (c.toLowerCase() === 'n') {
- this.value = false;
- return this.submit();
- }
- return this.bell();
- }
- render(first) {
- if (first) this.out.write(cursor.hide);
- this.out.write(
- erase.line +
- cursor.to(0) +
- [
- style.symbol(this.done, this.aborted),
- color.bold(this.msg),
- style.delimiter(this.done),
- this.done
- ? this.value ? 'yes' : 'no'
- : color.gray(this.initialValue ? '(Y/n)' : '(y/N)')
- ].join(' ')
- );
- }
- }
- module.exports = ConfirmPrompt;
|