Outlook_Addin_LLM/node_modules/@inquirer/expand/dist/cjs/index.js

101 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@inquirer/core");
const chalk_1 = __importDefault(require("chalk"));
const helpChoice = {
key: 'h',
name: 'Help, list all options',
value: undefined,
};
function getChoiceKey(choice, key) {
if (key === 'name') {
if ('name' in choice)
return choice.name;
return choice.value;
}
if ('value' in choice)
return choice.value;
return choice.name;
}
exports.default = (0, core_1.createPrompt)((config, done) => {
const { choices, default: defaultKey = 'h', expanded: defaultExpandState = false, } = config;
const [status, setStatus] = (0, core_1.useState)('pending');
const [value, setValue] = (0, core_1.useState)('');
const [expanded, setExpanded] = (0, core_1.useState)(defaultExpandState);
const [errorMsg, setError] = (0, core_1.useState)(undefined);
const prefix = (0, core_1.usePrefix)();
(0, core_1.useKeypress)((event, rl) => {
if ((0, core_1.isEnterKey)(event)) {
const answer = (value || defaultKey).toLowerCase();
if (answer === 'h' && !expanded) {
setExpanded(true);
}
else {
const selectedChoice = choices.find(({ key }) => key === answer);
if (selectedChoice) {
const finalValue = getChoiceKey(selectedChoice, 'value');
setValue(finalValue);
setStatus('done');
done(finalValue);
}
else if (value === '') {
setError('Please input a value');
}
else {
setError(`"${chalk_1.default.red(value)}" isn't an available option`);
}
}
}
else {
setValue(rl.line);
setError(undefined);
}
});
const message = chalk_1.default.bold(config.message);
if (status === 'done') {
// TODO: `value` should be the display name instead of the raw value.
return `${prefix} ${message} ${chalk_1.default.cyan(value)}`;
}
const allChoices = expanded ? choices : [...choices, helpChoice];
// Collapsed display style
let longChoices = '';
let shortChoices = allChoices
.map((choice) => {
if (choice.key === defaultKey) {
return choice.key.toUpperCase();
}
return choice.key;
})
.join('');
shortChoices = chalk_1.default.dim(` (${shortChoices})`);
// Expanded display style
if (expanded) {
shortChoices = '';
longChoices = allChoices
.map((choice) => {
const line = ` ${choice.key}) ${getChoiceKey(choice, 'name')}`;
if (choice.key === value.toLowerCase()) {
return chalk_1.default.cyan(line);
}
return line;
})
.join('\n');
}
let helpTip = '';
const currentOption = allChoices.find(({ key }) => key === value.toLowerCase());
if (currentOption) {
helpTip = `${chalk_1.default.cyan('>>')} ${getChoiceKey(currentOption, 'name')}`;
}
let error = '';
if (errorMsg) {
error = chalk_1.default.red(`> ${errorMsg}`);
}
return [
`${prefix} ${message}${shortChoices} ${value}`,
[longChoices, helpTip, error].filter(Boolean).join('\n'),
];
});