Socket
Socket
Sign inDemoInstall

@inquirer/select

Package Overview
Dependencies
Maintainers
3
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@inquirer/select - npm Package Compare versions

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"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc