Commit 24e3ee4b authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

MLAB-677: Update modules

parent 60ea96ad
Showing with 0 additions and 572 deletions
+0 -572
'use strict';
const colors = require('ansi-colors');
const clean = (str = '') => {
return typeof str === 'string' ? str.replace(/^['"]|['"]$/g, '') : '';
};
/**
* This file contains the interpolation and rendering logic for
* the Snippet prompt.
*/
class Item {
constructor(token) {
this.name = token.key;
this.field = token.field || {};
this.value = clean(token.initial || this.field.initial || '');
this.message = token.message || this.name;
this.cursor = 0;
this.input = '';
this.lines = [];
}
}
const tokenize = async(options = {}, defaults = {}, fn = token => token) => {
let unique = new Set();
let fields = options.fields || [];
let input = options.template;
let tabstops = [];
let items = [];
let keys = [];
let line = 1;
if (typeof input === 'function') {
input = await input();
}
let i = -1;
let next = () => input[++i];
let peek = () => input[i + 1];
let push = token => {
token.line = line;
tabstops.push(token);
};
push({ type: 'bos', value: '' });
while (i < input.length - 1) {
let value = next();
if (/^[^\S\n ]$/.test(value)) {
push({ type: 'text', value });
continue;
}
if (value === '\n') {
push({ type: 'newline', value });
line++;
continue;
}
if (value === '\\') {
value += next();
push({ type: 'text', value });
continue;
}
if ((value === '$' || value === '#' || value === '{') && peek() === '{') {
let n = next();
value += n;
let token = { type: 'template', open: value, inner: '', close: '', value };
let ch;
while ((ch = next())) {
if (ch === '}') {
if (peek() === '}') ch += next();
token.value += ch;
token.close = ch;
break;
}
if (ch === ':') {
token.initial = '';
token.key = token.inner;
} else if (token.initial !== void 0) {
token.initial += ch;
}
token.value += ch;
token.inner += ch;
}
token.template = token.open + (token.initial || token.inner) + token.close;
token.key = token.key || token.inner;
if (defaults.hasOwnProperty(token.key)) {
token.initial = defaults[token.key];
}
token = fn(token);
push(token);
keys.push(token.key);
unique.add(token.key);
let item = items.find(item => item.name === token.key);
token.field = fields.find(ch => ch.name === token.key);
if (!item) {
item = new Item(token);
items.push(item);
}
item.lines.push(token.line - 1);
continue;
}
let last = tabstops[tabstops.length - 1];
if (last.type === 'text' && last.line === line) {
last.value += value;
} else {
push({ type: 'text', value });
}
}
push({ type: 'eos', value: '' });
return { input, tabstops, unique, keys, items };
};
module.exports = async prompt => {
let options = prompt.options;
let required = new Set(options.required === true ? [] : (options.required || []));
let defaults = { ...options.values, ...options.initial };
let { tabstops, items, keys } = await tokenize(options, defaults);
let result = createFn('result', prompt, options);
let format = createFn('format', prompt, options);
let isValid = createFn('validate', prompt, options, true);
let isVal = prompt.isValue.bind(prompt);
return async(state = {}, submitted = false) => {
let index = 0;
state.required = required;
state.items = items;
state.keys = keys;
state.output = '';
let validate = async(value, state, item, index) => {
let error = await isValid(value, state, item, index);
if (error === false) {
return 'Invalid field ' + item.name;
}
return error;
};
for (let token of tabstops) {
let value = token.value;
let key = token.key;
if (token.type !== 'template') {
if (value) state.output += value;
continue;
}
if (token.type === 'template') {
let item = items.find(ch => ch.name === key);
if (options.required === true) {
state.required.add(item.name);
}
let val = [item.input, state.values[item.value], item.value, value].find(isVal);
let field = item.field || {};
let message = field.message || token.inner;
if (submitted) {
let error = await validate(state.values[key], state, item, index);
if ((error && typeof error === 'string') || error === false) {
state.invalid.set(key, error);
continue;
}
state.invalid.delete(key);
let res = await result(state.values[key], state, item, index);
state.output += colors.unstyle(res);
continue;
}
item.placeholder = false;
let before = value;
value = await format(value, state, item, index);
if (val !== value) {
state.values[key] = val;
value = prompt.styles.typing(val);
state.missing.delete(message);
} else {
state.values[key] = void 0;
val = `<${message}>`;
value = prompt.styles.primary(val);
item.placeholder = true;
if (state.required.has(key)) {
state.missing.add(message);
}
}
if (state.missing.has(message) && state.validating) {
value = prompt.styles.warning(val);
}
if (state.invalid.has(key) && state.validating) {
value = prompt.styles.danger(val);
}
if (index === state.index) {
if (before !== value) {
value = prompt.styles.underline(value);
} else {
value = prompt.styles.heading(colors.unstyle(value));
}
}
index++;
}
if (value) {
state.output += value;
}
}
let lines = state.output.split('\n').map(l => ' ' + l);
let len = items.length;
let done = 0;
for (let item of items) {
if (state.invalid.has(item.name)) {
item.lines.forEach(i => {
if (lines[i][0] !== ' ') return;
lines[i] = state.styles.danger(state.symbols.bullet) + lines[i].slice(1);
});
}
if (prompt.isValue(state.values[item.name])) {
done++;
}
}
state.completed = ((done / len) * 100).toFixed(0);
state.output = lines.join('\n');
return state.output;
};
};
function createFn(prop, prompt, options, fallback) {
return (value, state, item, index) => {
if (typeof item.field[prop] === 'function') {
return item.field[prop].call(prompt, value, state, item, index);
}
return [fallback, value].find(v => prompt.isValue(v));
};
}
'use strict';
const readline = require('readline');
const combos = require('./combos');
/* eslint-disable no-control-regex */
const metaKeyCodeRe = /^(?:\x1b)([a-zA-Z0-9])$/;
const fnKeyRe = /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/;
const keyName = {
/* xterm/gnome ESC O letter */
'OP': 'f1',
'OQ': 'f2',
'OR': 'f3',
'OS': 'f4',
/* xterm/rxvt ESC [ number ~ */
'[11~': 'f1',
'[12~': 'f2',
'[13~': 'f3',
'[14~': 'f4',
/* from Cygwin and used in libuv */
'[[A': 'f1',
'[[B': 'f2',
'[[C': 'f3',
'[[D': 'f4',
'[[E': 'f5',
/* common */
'[15~': 'f5',
'[17~': 'f6',
'[18~': 'f7',
'[19~': 'f8',
'[20~': 'f9',
'[21~': 'f10',
'[23~': 'f11',
'[24~': 'f12',
/* xterm ESC [ letter */
'[A': 'up',
'[B': 'down',
'[C': 'right',
'[D': 'left',
'[E': 'clear',
'[F': 'end',
'[H': 'home',
/* xterm/gnome ESC O letter */
'OA': 'up',
'OB': 'down',
'OC': 'right',
'OD': 'left',
'OE': 'clear',
'OF': 'end',
'OH': 'home',
/* xterm/rxvt ESC [ number ~ */
'[1~': 'home',
'[2~': 'insert',
'[3~': 'delete',
'[4~': 'end',
'[5~': 'pageup',
'[6~': 'pagedown',
/* putty */
'[[5~': 'pageup',
'[[6~': 'pagedown',
/* rxvt */
'[7~': 'home',
'[8~': 'end',
/* rxvt keys with modifiers */
'[a': 'up',
'[b': 'down',
'[c': 'right',
'[d': 'left',
'[e': 'clear',
'[2$': 'insert',
'[3$': 'delete',
'[5$': 'pageup',
'[6$': 'pagedown',
'[7$': 'home',
'[8$': 'end',
'Oa': 'up',
'Ob': 'down',
'Oc': 'right',
'Od': 'left',
'Oe': 'clear',
'[2^': 'insert',
'[3^': 'delete',
'[5^': 'pageup',
'[6^': 'pagedown',
'[7^': 'home',
'[8^': 'end',
/* misc. */
'[Z': 'tab',
}
function isShiftKey(code) {
return ['[a', '[b', '[c', '[d', '[e', '[2$', '[3$', '[5$', '[6$', '[7$', '[8$', '[Z'].includes(code)
}
function isCtrlKey(code) {
return [ 'Oa', 'Ob', 'Oc', 'Od', 'Oe', '[2^', '[3^', '[5^', '[6^', '[7^', '[8^'].includes(code)
}
const keypress = (s = '', event = {}) => {
let parts;
let key = {
name: event.name,
ctrl: false,
meta: false,
shift: false,
option: false,
sequence: s,
raw: s,
...event
};
if (Buffer.isBuffer(s)) {
if (s[0] > 127 && s[1] === void 0) {
s[0] -= 128;
s = '\x1b' + String(s);
} else {
s = String(s);
}
} else if (s !== void 0 && typeof s !== 'string') {
s = String(s);
} else if (!s) {
s = key.sequence || '';
}
key.sequence = key.sequence || s || key.name;
if (s === '\r') {
// carriage return
key.raw = void 0;
key.name = 'return';
} else if (s === '\n') {
// enter, should have been called linefeed
key.name = 'enter';
} else if (s === '\t') {
// tab
key.name = 'tab';
} else if (s === '\b' || s === '\x7f' || s === '\x1b\x7f' || s === '\x1b\b') {
// backspace or ctrl+h
key.name = 'backspace';
key.meta = s.charAt(0) === '\x1b';
} else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key
key.name = 'escape';
key.meta = s.length === 2;
} else if (s === ' ' || s === '\x1b ') {
key.name = 'space';
key.meta = s.length === 2;
} else if (s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
} else if (s.length === 1 && s >= '0' && s <= '9') {
// number
key.name = 'number';
} else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter
key.name = s;
} else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter
key.name = s.toLowerCase();
key.shift = true;
} else if ((parts = metaKeyCodeRe.exec(s))) {
// meta+character key
key.meta = true;
key.shift = /^[A-Z]$/.test(parts[1]);
} else if ((parts = fnKeyRe.exec(s))) {
let segs = [...s];
if (segs[0] === '\u001b' && segs[1] === '\u001b') {
key.option = true;
}
// ansi escape sequence
// reassemble the key code leaving out leading \x1b's,
// the modifier key bitflag and any meaningless "1;" sequence
let code = [parts[1], parts[2], parts[4], parts[6]].filter(Boolean).join('');
let modifier = (parts[3] || parts[5] || 1) - 1;
// Parse the key modifier
key.ctrl = !!(modifier & 4);
key.meta = !!(modifier & 10);
key.shift = !!(modifier & 1);
key.code = code;
key.name = keyName[code];
key.shift = isShiftKey(code) || key.shift;
key.ctrl = isCtrlKey(code) || key.ctrl;
}
return key;
};
keypress.listen = (options = {}, onKeypress) => {
let { stdin } = options;
if (!stdin || (stdin !== process.stdin && !stdin.isTTY)) {
throw new Error('Invalid stream passed');
}
let rl = readline.createInterface({ terminal: true, input: stdin });
readline.emitKeypressEvents(stdin, rl);
let on = (buf, key) => onKeypress(buf, keypress(buf, key), rl);
let isRaw = stdin.isRaw;
if (stdin.isTTY) stdin.setRawMode(true);
stdin.on('keypress', on);
rl.resume();
let off = () => {
if (stdin.isTTY) stdin.setRawMode(isRaw);
stdin.removeListener('keypress', on);
rl.pause();
rl.close();
};
return off;
};
keypress.action = (buf, key, customActions) => {
let obj = { ...combos, ...customActions };
if (key.ctrl) {
key.action = obj.ctrl[key.name];
return key;
}
if (key.option && obj.option) {
key.action = obj.option[key.name];
return key;
}
if (key.shift) {
key.action = obj.shift[key.name];
return key;
}
key.action = obj.keys[key.name];
return key;
};
module.exports = keypress;
'use strict';
const utils = require('./utils');
/**
* Render a placeholder value with cursor and styling based on the
* position of the cursor.
*
* @param {Object} `prompt` Prompt instance.
* @param {String} `input` Input string.
* @param {String} `initial` The initial user-provided value.
* @param {Number} `pos` Current cursor position.
* @param {Boolean} `showCursor` Render a simulated cursor using the inverse primary style.
* @return {String} Returns the styled placeholder string.
* @api public
*/
module.exports = (prompt, options = {}) => {
prompt.cursorHide();
let { input = '', initial = '', pos, showCursor = true, color } = options;
let style = color || prompt.styles.placeholder;
let inverse = utils.inverse(prompt.styles.primary);
let blinker = str => inverse(prompt.styles.black(str));
let output = input;
let char = ' ';
let reverse = blinker(char);
if (prompt.blink && prompt.blink.off === true) {
blinker = str => str;
reverse = '';
}
if (showCursor && pos === 0 && initial === '' && input === '') {
return blinker(char);
}
if (showCursor && pos === 0 && (input === initial || input === '')) {
return blinker(initial[0]) + style(initial.slice(1));
}
initial = utils.isPrimitive(initial) ? `${initial}` : '';
input = utils.isPrimitive(input) ? `${input}` : '';
let placeholder = initial && initial.startsWith(input) && initial !== input;
let cursor = placeholder ? blinker(initial[input.length]) : reverse;
if (pos !== input.length && showCursor === true) {
output = input.slice(0, pos) + blinker(input[pos]) + input.slice(pos + 1);
cursor = '';
}
if (showCursor === false) {
cursor = '';
}
if (placeholder) {
let raw = prompt.styles.unstyle(output + cursor);
return output + cursor + style(initial.slice(raw.length));
}
return output + cursor;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment