Spaces:
Runtime error
Runtime error
File size: 2,229 Bytes
4a51346 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
'use strict';
const BooleanPrompt = require('../types/boolean');
class TogglePrompt extends BooleanPrompt {
async initialize() {
await super.initialize();
this.value = this.initial = !!this.options.initial;
this.disabled = this.options.disabled || 'no';
this.enabled = this.options.enabled || 'yes';
await this.render();
}
reset() {
this.value = this.initial;
this.render();
}
delete() {
this.alert();
}
toggle() {
this.value = !this.value;
this.render();
}
enable() {
if (this.value === true) return this.alert();
this.value = true;
this.render();
}
disable() {
if (this.value === false) return this.alert();
this.value = false;
this.render();
}
up() {
this.toggle();
}
down() {
this.toggle();
}
right() {
this.toggle();
}
left() {
this.toggle();
}
next() {
this.toggle();
}
prev() {
this.toggle();
}
dispatch(ch = '', key) {
switch (ch.toLowerCase()) {
case ' ':
return this.toggle();
case '1':
case 'y':
case 't':
return this.enable();
case '0':
case 'n':
case 'f':
return this.disable();
default: {
return this.alert();
}
}
}
format() {
let active = str => this.styles.primary.underline(str);
let value = [
this.value ? this.disabled : active(this.disabled),
this.value ? active(this.enabled) : this.enabled
];
return value.join(this.styles.muted(' / '));
}
async render() {
let { size } = this.state;
let header = await this.header();
let prefix = await this.prefix();
let separator = await this.separator();
let message = await this.message();
let output = await this.format();
let help = (await this.error()) || (await this.hint());
let footer = await this.footer();
let prompt = [prefix, message, separator, output].join(' ');
this.state.prompt = prompt;
if (help && !prompt.includes(help)) prompt += ' ' + help;
this.clear(size);
this.write([header, prompt, footer].filter(Boolean).join('\n'));
this.write(this.margin[2]);
this.restore();
}
}
module.exports = TogglePrompt;
|