3 const utils = require('./utils');
6 * Render a placeholder value with cursor and styling based on the
7 * position of the cursor.
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.
18 module.exports = (prompt, options = {}) => {
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));
27 let reverse = blinker(char);
29 if (prompt.blink && prompt.blink.off === true) {
34 if (showCursor && pos === 0 && initial === '' && input === '') {
38 if (showCursor && pos === 0 && (input === initial || input === '')) {
39 return blinker(initial[0]) + style(initial.slice(1));
42 initial = utils.isPrimitive(initial) ? `${initial}` : '';
43 input = utils.isPrimitive(input) ? `${input}` : '';
45 let placeholder = initial && initial.startsWith(input) && initial !== input;
46 let cursor = placeholder ? blinker(initial[input.length]) : reverse;
48 if (pos !== input.length && showCursor === true) {
49 output = input.slice(0, pos) + blinker(input[pos]) + input.slice(pos + 1);
53 if (showCursor === false) {
58 let raw = prompt.styles.unstyle(output + cursor);
59 return output + cursor + style(initial.slice(raw.length));
62 return output + cursor;