@inquirer/select
Advanced tools
Comparing version 0.0.7-alpha.0 to 0.0.9-alpha.0
105
index.js
@@ -1,3 +0,4 @@ | ||
const { createPrompt } = require('@inquirer/core'); | ||
const { isUpKey, isDownKey, isNumberKey } = require('@inquirer/core/lib/key'); | ||
const { createPrompt, useState, useKeypress, useRef } = require('@inquirer/core/hooks'); | ||
const { usePrefix } = require('@inquirer/core/lib/prefix'); | ||
const { isEnterKey, isUpKey, isDownKey, isNumberKey } = require('@inquirer/core/lib/key'); | ||
const Paginator = require('@inquirer/core/lib/Paginator'); | ||
@@ -8,60 +9,62 @@ const chalk = require('chalk'); | ||
module.exports = createPrompt( | ||
readline => ({ | ||
onKeypress: (value, key, { cursorPosition = 0, choices }, setState) => { | ||
if (isUpKey(key) || isDownKey(key)) { | ||
let newCursorPosition = cursorPosition; | ||
const offset = isUpKey(key) ? -1 : 1; | ||
let selectedOption; | ||
module.exports = createPrompt((config, done) => { | ||
const [status, setStatus] = useState('pending'); | ||
const [cursorPosition, setCursorPos] = useState(0); | ||
const { choices, pageSize = 7 } = config; | ||
const paginator = useRef(new Paginator()).current; | ||
const prefix = usePrefix(); | ||
while (!selectedOption || selectedOption.disabled) { | ||
newCursorPosition = | ||
(newCursorPosition + offset + choices.length) % choices.length; | ||
selectedOption = choices[newCursorPosition]; | ||
} | ||
useKeypress(key => { | ||
if (isEnterKey(key)) { | ||
setStatus('done'); | ||
done(choices[cursorPosition].value); | ||
} else if (isUpKey(key) || isDownKey(key)) { | ||
let newCursorPosition = cursorPosition; | ||
const offset = isUpKey(key) ? -1 : 1; | ||
let selectedOption; | ||
setState({ cursorPosition: newCursorPosition }); | ||
} else if (isNumberKey(key)) { | ||
// Adjust index to start at 1 | ||
const newCursorPosition = Number(key.name) - 1; | ||
while (!selectedOption || selectedOption.disabled) { | ||
newCursorPosition = | ||
(newCursorPosition + offset + choices.length) % choices.length; | ||
selectedOption = choices[newCursorPosition]; | ||
} | ||
// Abort if the choice doesn't exists or if disabled | ||
if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) { | ||
return; | ||
} | ||
setCursorPos(newCursorPosition); | ||
} else if (isNumberKey(key)) { | ||
// Adjust index to start at 1 | ||
const newCursorPosition = Number(key.name) - 1; | ||
setState({ cursorPosition: newCursorPosition }); | ||
// Abort if the choice doesn't exists or if disabled | ||
if (!choices[newCursorPosition] || choices[newCursorPosition].disabled) { | ||
return; | ||
} | ||
}, | ||
mapStateToValue: ({ cursorPosition = 0, choices }) => { | ||
return choices[cursorPosition].value; | ||
}, | ||
paginator: new Paginator(readline) | ||
}), | ||
(state, { paginator }) => { | ||
const { prefix, choices, cursorPosition = 0, pageSize = 7 } = state; | ||
const message = chalk.bold(state.message); | ||
if (state.status === 'done') { | ||
const choice = choices[cursorPosition]; | ||
return `${prefix} ${message} ${chalk.cyan(choice.name || choice.value)}`; | ||
setCursorPos(newCursorPosition); | ||
} | ||
}); | ||
const allChoices = choices | ||
.map(({ name, value, disabled }, index) => { | ||
const line = name || value; | ||
if (disabled) { | ||
return chalk.dim(`- ${line} (disabled)`); | ||
} | ||
const message = chalk.bold(config.message); | ||
if (index === cursorPosition) { | ||
return chalk.cyan(`${figures.pointer} ${line}`); | ||
} | ||
if (status === 'done') { | ||
const choice = choices[cursorPosition]; | ||
return `${prefix} ${message} ${chalk.cyan(choice.name || choice.value)}`; | ||
} | ||
return ` ${line}`; | ||
}) | ||
.join('\n'); | ||
const windowedChoices = paginator.paginate(allChoices, cursorPosition, pageSize); | ||
return `${prefix} ${message}\n${windowedChoices}${cursorHide}`; | ||
} | ||
); | ||
const allChoices = choices | ||
.map(({ name, value, disabled }, index) => { | ||
const line = name || value; | ||
if (disabled) { | ||
return chalk.dim(`- ${line} (disabled)`); | ||
} | ||
if (index === cursorPosition) { | ||
return chalk.cyan(`${figures.pointer} ${line}`); | ||
} | ||
return ` ${line}`; | ||
}) | ||
.join('\n'); | ||
const windowedChoices = paginator.paginate(allChoices, cursorPosition, pageSize); | ||
return `${prefix} ${message}\n${windowedChoices}${cursorHide}`; | ||
}); |
{ | ||
"name": "@inquirer/select", | ||
"version": "0.0.7-alpha.0", | ||
"version": "0.0.9-alpha.0", | ||
"description": "Inquirer select/list prompt", | ||
@@ -16,4 +16,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"@inquirer/core": "^0.0.7-alpha.0", | ||
"chalk": "^2.4.1", | ||
"@inquirer/core": "^0.0.9-alpha.0", | ||
"chalk": "^3.0.0", | ||
"figures": "^3.0.0" | ||
@@ -24,3 +24,3 @@ }, | ||
}, | ||
"gitHead": "54285c75bc7986e0e2c7bcd2bc8ae4ddd6f05929" | ||
"gitHead": "65a4d598658e1b7dfd45ec88620962c989c94c5d" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4894
+ Added@inquirer/core@0.0.9-alpha.0(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removed@inquirer/core@0.0.7-alpha.0(transitive)
- Removedansi-regex@4.1.1(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedstrip-ansi@5.2.0(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedchalk@^3.0.0