diff options
Diffstat (limited to 'node_modules/prompts/lib/util')
-rw-r--r-- | node_modules/prompts/lib/util/action.js | 39 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/clear.js | 22 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/entriesToDisplay.js | 21 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/figures.js | 33 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/index.js | 12 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/lines.js | 15 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/strip.js | 11 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/style.js | 40 | ||||
-rw-r--r-- | node_modules/prompts/lib/util/wrap.js | 27 |
9 files changed, 220 insertions, 0 deletions
diff --git a/node_modules/prompts/lib/util/action.js b/node_modules/prompts/lib/util/action.js new file mode 100644 index 0000000..fefbd94 --- /dev/null +++ b/node_modules/prompts/lib/util/action.js @@ -0,0 +1,39 @@ +'use strict'; + +module.exports = (key, isSelect) => { + if (key.meta && key.name !== 'escape') return; + + if (key.ctrl) { + if (key.name === 'a') return 'first'; + if (key.name === 'c') return 'abort'; + if (key.name === 'd') return 'abort'; + if (key.name === 'e') return 'last'; + if (key.name === 'g') return 'reset'; + } + + if (isSelect) { + if (key.name === 'j') return 'down'; + if (key.name === 'k') return 'up'; + } + + if (key.name === 'return') return 'submit'; + if (key.name === 'enter') return 'submit'; // ctrl + J + if (key.name === 'backspace') return 'delete'; + if (key.name === 'delete') return 'deleteForward'; + if (key.name === 'abort') return 'abort'; + if (key.name === 'escape') return 'exit'; + if (key.name === 'tab') return 'next'; + if (key.name === 'pagedown') return 'nextPage'; + if (key.name === 'pageup') return 'prevPage'; + // TODO create home() in prompt types (e.g. TextPrompt) + if (key.name === 'home') return 'home'; + // TODO create end() in prompt types (e.g. TextPrompt) + if (key.name === 'end') return 'end'; + + if (key.name === 'up') return 'up'; + if (key.name === 'down') return 'down'; + if (key.name === 'right') return 'right'; + if (key.name === 'left') return 'left'; + + return false; +}; diff --git a/node_modules/prompts/lib/util/clear.js b/node_modules/prompts/lib/util/clear.js new file mode 100644 index 0000000..e4772d5 --- /dev/null +++ b/node_modules/prompts/lib/util/clear.js @@ -0,0 +1,22 @@ +'use strict'; + +const strip = require('./strip'); +const { erase, cursor } = require('sisteransi'); + +const width = str => [...strip(str)].length; + +/** + * @param {string} prompt + * @param {number} perLine + */ +module.exports = function(prompt, perLine) { + if (!perLine) return erase.line + cursor.to(0); + + let rows = 0; + const lines = prompt.split(/\r?\n/); + for (let line of lines) { + rows += 1 + Math.floor(Math.max(width(line) - 1, 0) / perLine); + } + + return erase.lines(rows); +}; diff --git a/node_modules/prompts/lib/util/entriesToDisplay.js b/node_modules/prompts/lib/util/entriesToDisplay.js new file mode 100644 index 0000000..5f6efbb --- /dev/null +++ b/node_modules/prompts/lib/util/entriesToDisplay.js @@ -0,0 +1,21 @@ +'use strict'; + +/** + * Determine what entries should be displayed on the screen, based on the + * currently selected index and the maximum visible. Used in list-based + * prompts like `select` and `multiselect`. + * + * @param {number} cursor the currently selected entry + * @param {number} total the total entries available to display + * @param {number} [maxVisible] the number of entries that can be displayed + */ +module.exports = (cursor, total, maxVisible) => { + maxVisible = maxVisible || total; + + let startIndex = Math.min(total- maxVisible, cursor - Math.floor(maxVisible / 2)); + if (startIndex < 0) startIndex = 0; + + let endIndex = Math.min(startIndex + maxVisible, total); + + return { startIndex, endIndex }; +}; diff --git a/node_modules/prompts/lib/util/figures.js b/node_modules/prompts/lib/util/figures.js new file mode 100644 index 0000000..cd31b88 --- /dev/null +++ b/node_modules/prompts/lib/util/figures.js @@ -0,0 +1,33 @@ +'use strict'; + + const main = { + arrowUp: '↑', + arrowDown: '↓', + arrowLeft: '←', + arrowRight: '→', + radioOn: '◉', + radioOff: '◯', + tick: '✔', + cross: '✖', + ellipsis: '…', + pointerSmall: '›', + line: '─', + pointer: '❯' +}; +const win = { + arrowUp: main.arrowUp, + arrowDown: main.arrowDown, + arrowLeft: main.arrowLeft, + arrowRight: main.arrowRight, + radioOn: '(*)', + radioOff: '( )', + tick: '√', + cross: '×', + ellipsis: '...', + pointerSmall: '»', + line: '─', + pointer: '>' +}; +const figures = process.platform === 'win32' ? win : main; + + module.exports = figures; diff --git a/node_modules/prompts/lib/util/index.js b/node_modules/prompts/lib/util/index.js new file mode 100644 index 0000000..f815986 --- /dev/null +++ b/node_modules/prompts/lib/util/index.js @@ -0,0 +1,12 @@ +'use strict'; + +module.exports = { + action: require('./action'), + clear: require('./clear'), + style: require('./style'), + strip: require('./strip'), + figures: require('./figures'), + lines: require('./lines'), + wrap: require('./wrap'), + entriesToDisplay: require('./entriesToDisplay') +}; diff --git a/node_modules/prompts/lib/util/lines.js b/node_modules/prompts/lib/util/lines.js new file mode 100644 index 0000000..de30419 --- /dev/null +++ b/node_modules/prompts/lib/util/lines.js @@ -0,0 +1,15 @@ +'use strict'; + +const strip = require('./strip'); + +/** + * @param {string} msg + * @param {number} perLine + */ +module.exports = function (msg, perLine) { + let lines = String(strip(msg) || '').split(/\r?\n/); + + if (!perLine) return lines.length; + return lines.map(l => Math.ceil(l.length / perLine)) + .reduce((a, b) => a + b); +}; diff --git a/node_modules/prompts/lib/util/strip.js b/node_modules/prompts/lib/util/strip.js new file mode 100644 index 0000000..8ebf4cb --- /dev/null +++ b/node_modules/prompts/lib/util/strip.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = str => { + const pattern = [ + '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))' + ].join('|'); + + const RGX = new RegExp(pattern, 'g'); + return typeof str === 'string' ? str.replace(RGX, '') : str; +}; diff --git a/node_modules/prompts/lib/util/style.js b/node_modules/prompts/lib/util/style.js new file mode 100644 index 0000000..1851cc7 --- /dev/null +++ b/node_modules/prompts/lib/util/style.js @@ -0,0 +1,40 @@ +'use strict'; + +const c = require('kleur'); +const figures = require('./figures'); + +// rendering user input. +const styles = Object.freeze({ + password: { scale: 1, render: input => '*'.repeat(input.length) }, + emoji: { scale: 2, render: input => '😃'.repeat(input.length) }, + invisible: { scale: 0, render: input => '' }, + default: { scale: 1, render: input => `${input}` } +}); +const render = type => styles[type] || styles.default; + +// icon to signalize a prompt. +const symbols = Object.freeze({ + aborted: c.red(figures.cross), + done: c.green(figures.tick), + exited: c.yellow(figures.cross), + default: c.cyan('?') +}); + +const symbol = (done, aborted, exited) => + aborted ? symbols.aborted : exited ? symbols.exited : done ? symbols.done : symbols.default; + +// between the question and the user's input. +const delimiter = completing => + c.gray(completing ? figures.ellipsis : figures.pointerSmall); + +const item = (expandable, expanded) => + c.gray(expandable ? (expanded ? figures.pointerSmall : '+') : figures.line); + +module.exports = { + styles, + render, + symbols, + symbol, + delimiter, + item +}; diff --git a/node_modules/prompts/lib/util/wrap.js b/node_modules/prompts/lib/util/wrap.js new file mode 100644 index 0000000..43b5399 --- /dev/null +++ b/node_modules/prompts/lib/util/wrap.js @@ -0,0 +1,27 @@ +'use strict'; + +/** + * @param {string} msg The message to wrap + * @param {object} opts + * @param {number|string} [opts.margin] Left margin + * @param {number} opts.width Maximum characters per line including the margin + */ +module.exports = (msg, opts = {}) => { + const tab = Number.isSafeInteger(parseInt(opts.margin)) + ? new Array(parseInt(opts.margin)).fill(' ').join('') + : (opts.margin || ''); + + const width = opts.width; + + return (msg || '').split(/\r?\n/g) + .map(line => line + .split(/\s+/g) + .reduce((arr, w) => { + if (w.length + tab.length >= width || arr[arr.length - 1].length + w.length + 1 < width) + arr[arr.length - 1] += ` ${w}`; + else arr.push(`${tab}${w}`); + return arr; + }, [ tab ]) + .join('\n')) + .join('\n'); +}; |