.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / placeholder.js
1 'use strict';
2
3 const utils = require('./utils');
4
5 /**
6  * Render a placeholder value with cursor and styling based on the
7  * position of the cursor.
8  *
9  * @param {Object} `prompt` Prompt instance.
10  * @param {String} `input` Input string.
11  * @param {String} `initial` The initial user-provided value.
12  * @param {Number} `pos` Current cursor position.
13  * @param {Boolean} `showCursor` Render a simulated cursor using the inverse primary style.
14  * @return {String} Returns the styled placeholder string.
15  * @api public
16  */
17
18 module.exports = (prompt, options = {}) => {
19   prompt.cursorHide();
20
21   let { input = '', initial = '', pos, showCursor = true, color } = options;
22   let style = color || prompt.styles.placeholder;
23   let inverse = utils.inverse(prompt.styles.primary);
24   let blinker = str => inverse(prompt.styles.black(str));
25   let output = input;
26   let char = ' ';
27   let reverse = blinker(char);
28
29   if (prompt.blink && prompt.blink.off === true) {
30     blinker = str => str;
31     reverse = '';
32   }
33
34   if (showCursor && pos === 0 && initial === '' && input === '') {
35     return blinker(char);
36   }
37
38   if (showCursor && pos === 0 && (input === initial || input === '')) {
39     return blinker(initial[0]) + style(initial.slice(1));
40   }
41
42   initial = utils.isPrimitive(initial) ? `${initial}` : '';
43   input = utils.isPrimitive(input) ? `${input}` : '';
44
45   let placeholder = initial && initial.startsWith(input) && initial !== input;
46   let cursor = placeholder ? blinker(initial[input.length]) : reverse;
47
48   if (pos !== input.length && showCursor === true) {
49     output = input.slice(0, pos) + blinker(input[pos]) + input.slice(pos + 1);
50     cursor = '';
51   }
52
53   if (showCursor === false) {
54     cursor = '';
55   }
56
57   if (placeholder) {
58     let raw = prompt.styles.unstyle(output + cursor);
59     return output + cursor + style(initial.slice(raw.length));
60   }
61
62   return output + cursor;
63 };