.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / types / boolean.js
1 'use strict';
2
3 const Prompt = require('../prompt');
4 const { isPrimitive, hasColor } = require('../utils');
5
6 class BooleanPrompt extends Prompt {
7   constructor(options) {
8     super(options);
9     this.cursorHide();
10   }
11
12   async initialize() {
13     let initial = await this.resolve(this.initial, this.state);
14     this.input = await this.cast(initial);
15     await super.initialize();
16   }
17
18   dispatch(ch) {
19     if (!this.isValue(ch)) return this.alert();
20     this.input = ch;
21     return this.submit();
22   }
23
24   format(value) {
25     let { styles, state } = this;
26     return !state.submitted ? styles.primary(value) : styles.success(value);
27   }
28
29   cast(input) {
30     return this.isTrue(input);
31   }
32
33   isTrue(input) {
34     return /^[ty1]/i.test(input);
35   }
36
37   isFalse(input) {
38     return /^[fn0]/i.test(input);
39   }
40
41   isValue(value) {
42     return isPrimitive(value) && (this.isTrue(value) || this.isFalse(value));
43   }
44
45   async hint() {
46     if (this.state.status === 'pending') {
47       let hint = await this.element('hint');
48       if (!hasColor(hint)) {
49         return this.styles.muted(hint);
50       }
51       return hint;
52     }
53   }
54
55   async render() {
56     let { input, size } = this.state;
57
58     let prefix = await this.prefix();
59     let sep = await this.separator();
60     let msg = await this.message();
61     let hint = this.styles.muted(this.default);
62
63     let promptLine = [prefix, msg, hint, sep].filter(Boolean).join(' ');
64     this.state.prompt = promptLine;
65
66     let header = await this.header();
67     let value = this.value = this.cast(input);
68     let output = await this.format(value);
69     let help = (await this.error()) || (await this.hint());
70     let footer = await this.footer();
71
72     if (help && !promptLine.includes(help)) output += ' ' + help;
73     promptLine += ' ' + output;
74
75     this.clear(size);
76     this.write([header, promptLine, footer].filter(Boolean).join('\n'));
77     this.restore();
78   }
79
80   set value(value) {
81     super.value = value;
82   }
83   get value() {
84     return this.cast(super.value);
85   }
86 }
87
88 module.exports = BooleanPrompt;