@inquirer/select
Advanced tools
Comparing version 1.2.11 to 1.2.12
@@ -12,19 +12,31 @@ "use strict"; | ||
const ansi_escapes_1 = __importDefault(require("ansi-escapes")); | ||
function isSelectableChoice(choice) { | ||
return choice != null && !core_1.Separator.isSeparator(choice) && !choice.disabled; | ||
function isSelectable(item) { | ||
return !core_1.Separator.isSeparator(item) && !item.disabled; | ||
} | ||
function renderItem({ item, isActive }) { | ||
if (core_1.Separator.isSeparator(item)) { | ||
return ` ${item.separator}`; | ||
} | ||
const line = item.name || item.value; | ||
if (item.disabled) { | ||
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)'; | ||
return chalk_1.default.dim(`- ${line} ${disabledLabel}`); | ||
} | ||
const color = isActive ? chalk_1.default.cyan : (x) => x; | ||
const prefix = isActive ? figures_1.default.pointer : ` `; | ||
return color(`${prefix} ${line}`); | ||
} | ||
exports.default = (0, core_1.createPrompt)((config, done) => { | ||
const { choices } = config; | ||
const { choices: items, pageSize } = config; | ||
const firstRender = (0, core_1.useRef)(true); | ||
const prefix = (0, core_1.usePrefix)(); | ||
const [status, setStatus] = (0, core_1.useState)('pending'); | ||
const [cursorPosition, setCursorPos] = (0, core_1.useState)(() => { | ||
const startIndex = choices.findIndex(isSelectableChoice); | ||
if (startIndex < 0) { | ||
const [active, setActive] = (0, core_1.useState)(() => { | ||
const selected = items.findIndex(isSelectable); | ||
if (selected < 0) | ||
throw new Error('[select prompt] No selectable choices. All choices are disabled.'); | ||
} | ||
return startIndex; | ||
return selected; | ||
}); | ||
// Safe to assume the cursor position always point to a Choice. | ||
const selectedChoice = choices[cursorPosition]; | ||
const selectedChoice = items[active]; | ||
(0, core_1.useKeypress)((key) => { | ||
@@ -36,20 +48,15 @@ if ((0, core_1.isEnterKey)(key)) { | ||
else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
let newCursorPosition = cursorPosition; | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let selectedOption; | ||
while (!isSelectableChoice(selectedOption)) { | ||
newCursorPosition = | ||
(newCursorPosition + offset + choices.length) % choices.length; | ||
selectedOption = choices[newCursorPosition]; | ||
} | ||
setCursorPos(newCursorPosition); | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
} | ||
else if ((0, core_1.isNumberKey)(key)) { | ||
// Adjust index to start at 1 | ||
const newCursorPosition = Number(key.name) - 1; | ||
// Abort if the choice doesn't exists or if disabled | ||
if (!isSelectableChoice(choices[newCursorPosition])) { | ||
const position = Number(key.name) - 1; | ||
const item = items[position]; | ||
if (item == null || !isSelectable(item)) | ||
return; | ||
} | ||
setCursorPos(newCursorPosition); | ||
setActive(position); | ||
} | ||
@@ -59,24 +66,11 @@ }); | ||
if (firstRender.current) { | ||
firstRender.current = false; | ||
message += chalk_1.default.dim(' (Use arrow keys)'); | ||
firstRender.current = false; | ||
} | ||
const allChoices = choices | ||
.map((choice, index) => { | ||
if (core_1.Separator.isSeparator(choice)) { | ||
return ` ${choice.separator}`; | ||
} | ||
const line = choice.name || choice.value; | ||
if (choice.disabled) { | ||
const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)'; | ||
return chalk_1.default.dim(`- ${line} ${disabledLabel}`); | ||
} | ||
if (index === cursorPosition) { | ||
return chalk_1.default.cyan(`${figures_1.default.pointer} ${line}`); | ||
} | ||
return ` ${line}`; | ||
}) | ||
const lines = items | ||
.map((item, index) => renderItem({ item, isActive: index === active })) | ||
.join('\n'); | ||
const windowedChoices = (0, core_1.usePagination)(allChoices, { | ||
active: cursorPosition, | ||
pageSize: config.pageSize, | ||
const page = (0, core_1.usePagination)(lines, { | ||
active, | ||
pageSize, | ||
}); | ||
@@ -89,3 +83,3 @@ if (status === 'done') { | ||
: ``; | ||
return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
return `${prefix} ${message}\n${page}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
}); |
@@ -12,19 +12,31 @@ "use strict"; | ||
const ansi_escapes_1 = __importDefault(require("ansi-escapes")); | ||
function isSelectableChoice(choice) { | ||
return choice != null && !core_1.Separator.isSeparator(choice) && !choice.disabled; | ||
function isSelectable(item) { | ||
return !core_1.Separator.isSeparator(item) && !item.disabled; | ||
} | ||
function renderItem({ item, isActive }) { | ||
if (core_1.Separator.isSeparator(item)) { | ||
return ` ${item.separator}`; | ||
} | ||
const line = item.name || item.value; | ||
if (item.disabled) { | ||
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)'; | ||
return chalk_1.default.dim(`- ${line} ${disabledLabel}`); | ||
} | ||
const color = isActive ? chalk_1.default.cyan : (x) => x; | ||
const prefix = isActive ? figures_1.default.pointer : ` `; | ||
return color(`${prefix} ${line}`); | ||
} | ||
exports.default = (0, core_1.createPrompt)((config, done) => { | ||
const { choices } = config; | ||
const { choices: items, pageSize } = config; | ||
const firstRender = (0, core_1.useRef)(true); | ||
const prefix = (0, core_1.usePrefix)(); | ||
const [status, setStatus] = (0, core_1.useState)('pending'); | ||
const [cursorPosition, setCursorPos] = (0, core_1.useState)(() => { | ||
const startIndex = choices.findIndex(isSelectableChoice); | ||
if (startIndex < 0) { | ||
const [active, setActive] = (0, core_1.useState)(() => { | ||
const selected = items.findIndex(isSelectable); | ||
if (selected < 0) | ||
throw new Error('[select prompt] No selectable choices. All choices are disabled.'); | ||
} | ||
return startIndex; | ||
return selected; | ||
}); | ||
// Safe to assume the cursor position always point to a Choice. | ||
const selectedChoice = choices[cursorPosition]; | ||
const selectedChoice = items[active]; | ||
(0, core_1.useKeypress)((key) => { | ||
@@ -36,20 +48,15 @@ if ((0, core_1.isEnterKey)(key)) { | ||
else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
let newCursorPosition = cursorPosition; | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let selectedOption; | ||
while (!isSelectableChoice(selectedOption)) { | ||
newCursorPosition = | ||
(newCursorPosition + offset + choices.length) % choices.length; | ||
selectedOption = choices[newCursorPosition]; | ||
} | ||
setCursorPos(newCursorPosition); | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
} | ||
else if ((0, core_1.isNumberKey)(key)) { | ||
// Adjust index to start at 1 | ||
const newCursorPosition = Number(key.name) - 1; | ||
// Abort if the choice doesn't exists or if disabled | ||
if (!isSelectableChoice(choices[newCursorPosition])) { | ||
const position = Number(key.name) - 1; | ||
const item = items[position]; | ||
if (item == null || !isSelectable(item)) | ||
return; | ||
} | ||
setCursorPos(newCursorPosition); | ||
setActive(position); | ||
} | ||
@@ -59,24 +66,11 @@ }); | ||
if (firstRender.current) { | ||
firstRender.current = false; | ||
message += chalk_1.default.dim(' (Use arrow keys)'); | ||
firstRender.current = false; | ||
} | ||
const allChoices = choices | ||
.map((choice, index) => { | ||
if (core_1.Separator.isSeparator(choice)) { | ||
return ` ${choice.separator}`; | ||
} | ||
const line = choice.name || choice.value; | ||
if (choice.disabled) { | ||
const disabledLabel = typeof choice.disabled === 'string' ? choice.disabled : '(disabled)'; | ||
return chalk_1.default.dim(`- ${line} ${disabledLabel}`); | ||
} | ||
if (index === cursorPosition) { | ||
return chalk_1.default.cyan(`${figures_1.default.pointer} ${line}`); | ||
} | ||
return ` ${line}`; | ||
}) | ||
const lines = items | ||
.map((item, index) => renderItem({ item, isActive: index === active })) | ||
.join('\n'); | ||
const windowedChoices = (0, core_1.usePagination)(allChoices, { | ||
active: cursorPosition, | ||
pageSize: config.pageSize, | ||
const page = (0, core_1.usePagination)(lines, { | ||
active, | ||
pageSize, | ||
}); | ||
@@ -89,3 +83,3 @@ if (status === 'done') { | ||
: ``; | ||
return `${prefix} ${message}\n${windowedChoices}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
return `${prefix} ${message}\n${page}${choiceDescription}${ansi_escapes_1.default.cursorHide}`; | ||
}); |
{ | ||
"name": "@inquirer/select", | ||
"version": "1.2.11", | ||
"version": "1.2.12", | ||
"description": "Inquirer select/list prompt", | ||
@@ -57,4 +57,4 @@ "main": "./dist/cjs/index.js", | ||
"dependencies": { | ||
"@inquirer/core": "^5.0.0", | ||
"@inquirer/type": "^1.1.4", | ||
"@inquirer/core": "^5.0.1", | ||
"@inquirer/type": "^1.1.5", | ||
"ansi-escapes": "^4.3.2", | ||
@@ -65,3 +65,3 @@ "chalk": "^4.1.2", | ||
"devDependencies": { | ||
"@inquirer/testing": "^2.1.6" | ||
"@inquirer/testing": "^2.1.7" | ||
}, | ||
@@ -91,3 +91,3 @@ "scripts": { | ||
}, | ||
"gitHead": "c2d1c5fdfd1029f78351fb04e06f1cfb29d55bb6" | ||
"gitHead": "85784061d702778bc9dd48ca08f09ee9976b06ee" | ||
} |
Sorry, the diff of this file is not supported yet
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
17166
252
Updated@inquirer/core@^5.0.1
Updated@inquirer/type@^1.1.5