.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / types / string.js
1 'use strict';
2
3 const Prompt = require('../prompt');
4 const placeholder = require('../placeholder');
5 const { isPrimitive } = require('../utils');
6
7 class StringPrompt extends Prompt {
8   constructor(options) {
9     super(options);
10     this.initial = isPrimitive(this.initial) ? String(this.initial) : '';
11     if (this.initial) this.cursorHide();
12     this.state.prevCursor = 0;
13     this.state.clipboard = [];
14   }
15
16   async keypress(input, key = {}) {
17     let prev = this.state.prevKeypress;
18     this.state.prevKeypress = key;
19     if (this.options.multiline === true && key.name === 'return') {
20       if (!prev || prev.name !== 'return') {
21         return this.append('\n', key);
22       }
23     }
24     return super.keypress(input, key);
25   }
26
27   moveCursor(n) {
28     this.cursor += n;
29   }
30
31   reset() {
32     this.input = this.value = '';
33     this.cursor = 0;
34     return this.render();
35   }
36
37   dispatch(ch, key) {
38     if (!ch || key.ctrl || key.code) return this.alert();
39     this.append(ch);
40   }
41
42   append(ch) {
43     let { cursor, input } = this.state;
44     this.input = `${input}`.slice(0, cursor) + ch + `${input}`.slice(cursor);
45     this.moveCursor(String(ch).length);
46     this.render();
47   }
48
49   insert(str) {
50     this.append(str);
51   }
52
53   delete() {
54     let { cursor, input } = this.state;
55     if (cursor <= 0) return this.alert();
56     this.input = `${input}`.slice(0, cursor - 1) + `${input}`.slice(cursor);
57     this.moveCursor(-1);
58     this.render();
59   }
60
61   deleteForward() {
62     let { cursor, input } = this.state;
63     if (input[cursor] === void 0) return this.alert();
64     this.input = `${input}`.slice(0, cursor) + `${input}`.slice(cursor + 1);
65     this.render();
66   }
67
68   cutForward() {
69     let pos = this.cursor;
70     if (this.input.length <= pos) return this.alert();
71     this.state.clipboard.push(this.input.slice(pos));
72     this.input = this.input.slice(0, pos);
73     this.render();
74   }
75
76   cutLeft() {
77     let pos = this.cursor;
78     if (pos === 0) return this.alert();
79     let before = this.input.slice(0, pos);
80     let after = this.input.slice(pos);
81     let words = before.split(' ');
82     this.state.clipboard.push(words.pop());
83     this.input = words.join(' ');
84     this.cursor = this.input.length;
85     this.input += after;
86     this.render();
87   }
88
89   paste() {
90     if (!this.state.clipboard.length) return this.alert();
91     this.insert(this.state.clipboard.pop());
92     this.render();
93   }
94
95   toggleCursor() {
96     if (this.state.prevCursor) {
97       this.cursor = this.state.prevCursor;
98       this.state.prevCursor = 0;
99     } else {
100       this.state.prevCursor = this.cursor;
101       this.cursor = 0;
102     }
103     this.render();
104   }
105
106   first() {
107     this.cursor = 0;
108     this.render();
109   }
110
111   last() {
112     this.cursor = this.input.length - 1;
113     this.render();
114   }
115
116   next() {
117     let init = this.initial != null ? String(this.initial) : '';
118     if (!init || !init.startsWith(this.input)) return this.alert();
119     this.input = this.initial;
120     this.cursor = this.initial.length;
121     this.render();
122   }
123
124   prev() {
125     if (!this.input) return this.alert();
126     this.reset();
127   }
128
129   backward() {
130     return this.left();
131   }
132
133   forward() {
134     return this.right();
135   }
136
137   right() {
138     if (this.cursor >= this.input.length) return this.alert();
139     this.moveCursor(1);
140     return this.render();
141   }
142
143   left() {
144     if (this.cursor <= 0) return this.alert();
145     this.moveCursor(-1);
146     return this.render();
147   }
148
149   isValue(value) {
150     return !!value;
151   }
152
153   async format(input = this.value) {
154     let initial = await this.resolve(this.initial, this.state);
155     if (!this.state.submitted) {
156       return placeholder(this, { input, initial, pos: this.cursor });
157     }
158     return this.styles.submitted(input || initial);
159   }
160
161   async render() {
162     let size = this.state.size;
163
164     let prefix = await this.prefix();
165     let separator = await this.separator();
166     let message = await this.message();
167
168     let prompt = [prefix, message, separator].filter(Boolean).join(' ');
169     this.state.prompt = prompt;
170
171     let header = await this.header();
172     let output = await this.format();
173     let help = (await this.error()) || (await this.hint());
174     let footer = await this.footer();
175
176     if (help && !output.includes(help)) output += ' ' + help;
177     prompt += ' ' + output;
178
179     this.clear(size);
180     this.write([header, prompt, footer].filter(Boolean).join('\n'));
181     this.restore();
182   }
183 }
184
185 module.exports = StringPrompt;