You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
849 B
39 lines
849 B
// Packages
|
|
const chalk = require('chalk')
|
|
|
|
module.exports = promptOptions
|
|
|
|
function promptOptions(opts) {
|
|
return new Promise((resolve, reject) => {
|
|
opts.forEach(([, text], i) => {
|
|
console.log(`${chalk.gray('>')} [${chalk.bold(i + 1)}] ${text}`)
|
|
})
|
|
|
|
const ondata = v => {
|
|
const s = v.toString()
|
|
|
|
const cleanup = () => {
|
|
process.stdin.setRawMode(false)
|
|
process.stdin.removeListener('data', ondata)
|
|
}
|
|
|
|
// Ctrl + C
|
|
if (s === '\u0003') {
|
|
cleanup()
|
|
const err = new Error('Aborted')
|
|
err.code = 'USER_ABORT'
|
|
return reject(err)
|
|
}
|
|
|
|
const n = Number(s)
|
|
if (opts[n - 1]) {
|
|
cleanup()
|
|
resolve(opts[n - 1][0])
|
|
}
|
|
}
|
|
|
|
process.stdin.setRawMode(true)
|
|
process.stdin.resume()
|
|
process.stdin.on('data', ondata)
|
|
})
|
|
}
|
|
|