mirror of
https://github.com/tvytlx/ai-agent-deep-dive.git
synced 2026-04-04 16:14:50 +08:00
Add extracted source directory and README navigation
This commit is contained in:
150
extracted-source/node_modules/@inquirer/select/dist/esm/index.mjs
generated
vendored
Normal file
150
extracted-source/node_modules/@inquirer/select/dist/esm/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
import { createPrompt, useState, useKeypress, usePrefix, usePagination, useRef, useMemo, useEffect, isBackspaceKey, isEnterKey, isUpKey, isDownKey, isNumberKey, Separator, ValidationError, makeTheme, } from '@inquirer/core';
|
||||
import colors from 'yoctocolors-cjs';
|
||||
import figures from '@inquirer/figures';
|
||||
import ansiEscapes from 'ansi-escapes';
|
||||
const selectTheme = {
|
||||
icon: { cursor: figures.pointer },
|
||||
style: {
|
||||
disabled: (text) => colors.dim(`- ${text}`),
|
||||
description: (text) => colors.cyan(text),
|
||||
},
|
||||
helpMode: 'auto',
|
||||
};
|
||||
function isSelectable(item) {
|
||||
return !Separator.isSeparator(item) && !item.disabled;
|
||||
}
|
||||
function normalizeChoices(choices) {
|
||||
return choices.map((choice) => {
|
||||
if (Separator.isSeparator(choice))
|
||||
return choice;
|
||||
if (typeof choice === 'string') {
|
||||
return {
|
||||
value: choice,
|
||||
name: choice,
|
||||
short: choice,
|
||||
disabled: false,
|
||||
};
|
||||
}
|
||||
const name = choice.name ?? String(choice.value);
|
||||
return {
|
||||
value: choice.value,
|
||||
name,
|
||||
description: choice.description,
|
||||
short: choice.short ?? name,
|
||||
disabled: choice.disabled ?? false,
|
||||
};
|
||||
});
|
||||
}
|
||||
export default createPrompt((config, done) => {
|
||||
const { loop = true, pageSize = 7 } = config;
|
||||
const firstRender = useRef(true);
|
||||
const theme = makeTheme(selectTheme, config.theme);
|
||||
const [status, setStatus] = useState('idle');
|
||||
const prefix = usePrefix({ status, theme });
|
||||
const searchTimeoutRef = useRef();
|
||||
const items = useMemo(() => normalizeChoices(config.choices), [config.choices]);
|
||||
const bounds = useMemo(() => {
|
||||
const first = items.findIndex(isSelectable);
|
||||
const last = items.findLastIndex(isSelectable);
|
||||
if (first < 0) {
|
||||
throw new ValidationError('[select prompt] No selectable choices. All choices are disabled.');
|
||||
}
|
||||
return { first, last };
|
||||
}, [items]);
|
||||
const defaultItemIndex = useMemo(() => {
|
||||
if (!('default' in config))
|
||||
return -1;
|
||||
return items.findIndex((item) => isSelectable(item) && item.value === config.default);
|
||||
}, [config.default, items]);
|
||||
const [active, setActive] = useState(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
|
||||
// Safe to assume the cursor position always point to a Choice.
|
||||
const selectedChoice = items[active];
|
||||
useKeypress((key, rl) => {
|
||||
clearTimeout(searchTimeoutRef.current);
|
||||
if (isEnterKey(key)) {
|
||||
setStatus('done');
|
||||
done(selectedChoice.value);
|
||||
}
|
||||
else if (isUpKey(key) || isDownKey(key)) {
|
||||
rl.clearLine(0);
|
||||
if (loop ||
|
||||
(isUpKey(key) && active !== bounds.first) ||
|
||||
(isDownKey(key) && active !== bounds.last)) {
|
||||
const offset = isUpKey(key) ? -1 : 1;
|
||||
let next = active;
|
||||
do {
|
||||
next = (next + offset + items.length) % items.length;
|
||||
} while (!isSelectable(items[next]));
|
||||
setActive(next);
|
||||
}
|
||||
}
|
||||
else if (isNumberKey(key)) {
|
||||
rl.clearLine(0);
|
||||
const position = Number(key.name) - 1;
|
||||
const item = items[position];
|
||||
if (item != null && isSelectable(item)) {
|
||||
setActive(position);
|
||||
}
|
||||
}
|
||||
else if (isBackspaceKey(key)) {
|
||||
rl.clearLine(0);
|
||||
}
|
||||
else {
|
||||
// Default to search
|
||||
const searchTerm = rl.line.toLowerCase();
|
||||
const matchIndex = items.findIndex((item) => {
|
||||
if (Separator.isSeparator(item) || !isSelectable(item))
|
||||
return false;
|
||||
return item.name.toLowerCase().startsWith(searchTerm);
|
||||
});
|
||||
if (matchIndex >= 0) {
|
||||
setActive(matchIndex);
|
||||
}
|
||||
searchTimeoutRef.current = setTimeout(() => {
|
||||
rl.clearLine(0);
|
||||
}, 700);
|
||||
}
|
||||
});
|
||||
useEffect(() => () => {
|
||||
clearTimeout(searchTimeoutRef.current);
|
||||
}, []);
|
||||
const message = theme.style.message(config.message, status);
|
||||
let helpTipTop = '';
|
||||
let helpTipBottom = '';
|
||||
if (theme.helpMode === 'always' ||
|
||||
(theme.helpMode === 'auto' && firstRender.current)) {
|
||||
firstRender.current = false;
|
||||
if (items.length > pageSize) {
|
||||
helpTipBottom = `\n${theme.style.help('(Use arrow keys to reveal more choices)')}`;
|
||||
}
|
||||
else {
|
||||
helpTipTop = theme.style.help('(Use arrow keys)');
|
||||
}
|
||||
}
|
||||
const page = usePagination({
|
||||
items,
|
||||
active,
|
||||
renderItem({ item, isActive }) {
|
||||
if (Separator.isSeparator(item)) {
|
||||
return ` ${item.separator}`;
|
||||
}
|
||||
if (item.disabled) {
|
||||
const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
|
||||
return theme.style.disabled(`${item.name} ${disabledLabel}`);
|
||||
}
|
||||
const color = isActive ? theme.style.highlight : (x) => x;
|
||||
const cursor = isActive ? theme.icon.cursor : ` `;
|
||||
return color(`${cursor} ${item.name}`);
|
||||
},
|
||||
pageSize,
|
||||
loop,
|
||||
});
|
||||
if (status === 'done') {
|
||||
return `${prefix} ${message} ${theme.style.answer(selectedChoice.short)}`;
|
||||
}
|
||||
const choiceDescription = selectedChoice.description
|
||||
? `\n${theme.style.description(selectedChoice.description)}`
|
||||
: ``;
|
||||
return `${[prefix, message, helpTipTop].filter(Boolean).join(' ')}\n${page}${helpTipBottom}${choiceDescription}${ansiEscapes.cursorHide}`;
|
||||
});
|
||||
export { Separator } from '@inquirer/core';
|
||||
157
extracted-source/node_modules/@inquirer/select/node_modules/ansi-escapes/index.js
generated
vendored
Normal file
157
extracted-source/node_modules/@inquirer/select/node_modules/ansi-escapes/index.js
generated
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
'use strict';
|
||||
const ansiEscapes = module.exports;
|
||||
// TODO: remove this in the next major version
|
||||
module.exports.default = ansiEscapes;
|
||||
|
||||
const ESC = '\u001B[';
|
||||
const OSC = '\u001B]';
|
||||
const BEL = '\u0007';
|
||||
const SEP = ';';
|
||||
const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
|
||||
|
||||
ansiEscapes.cursorTo = (x, y) => {
|
||||
if (typeof x !== 'number') {
|
||||
throw new TypeError('The `x` argument is required');
|
||||
}
|
||||
|
||||
if (typeof y !== 'number') {
|
||||
return ESC + (x + 1) + 'G';
|
||||
}
|
||||
|
||||
return ESC + (y + 1) + ';' + (x + 1) + 'H';
|
||||
};
|
||||
|
||||
ansiEscapes.cursorMove = (x, y) => {
|
||||
if (typeof x !== 'number') {
|
||||
throw new TypeError('The `x` argument is required');
|
||||
}
|
||||
|
||||
let ret = '';
|
||||
|
||||
if (x < 0) {
|
||||
ret += ESC + (-x) + 'D';
|
||||
} else if (x > 0) {
|
||||
ret += ESC + x + 'C';
|
||||
}
|
||||
|
||||
if (y < 0) {
|
||||
ret += ESC + (-y) + 'A';
|
||||
} else if (y > 0) {
|
||||
ret += ESC + y + 'B';
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
|
||||
ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
|
||||
ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
|
||||
ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
|
||||
|
||||
ansiEscapes.cursorLeft = ESC + 'G';
|
||||
ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
|
||||
ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
|
||||
ansiEscapes.cursorGetPosition = ESC + '6n';
|
||||
ansiEscapes.cursorNextLine = ESC + 'E';
|
||||
ansiEscapes.cursorPrevLine = ESC + 'F';
|
||||
ansiEscapes.cursorHide = ESC + '?25l';
|
||||
ansiEscapes.cursorShow = ESC + '?25h';
|
||||
|
||||
ansiEscapes.eraseLines = count => {
|
||||
let clear = '';
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
|
||||
}
|
||||
|
||||
if (count) {
|
||||
clear += ansiEscapes.cursorLeft;
|
||||
}
|
||||
|
||||
return clear;
|
||||
};
|
||||
|
||||
ansiEscapes.eraseEndLine = ESC + 'K';
|
||||
ansiEscapes.eraseStartLine = ESC + '1K';
|
||||
ansiEscapes.eraseLine = ESC + '2K';
|
||||
ansiEscapes.eraseDown = ESC + 'J';
|
||||
ansiEscapes.eraseUp = ESC + '1J';
|
||||
ansiEscapes.eraseScreen = ESC + '2J';
|
||||
ansiEscapes.scrollUp = ESC + 'S';
|
||||
ansiEscapes.scrollDown = ESC + 'T';
|
||||
|
||||
ansiEscapes.clearScreen = '\u001Bc';
|
||||
|
||||
ansiEscapes.clearTerminal = process.platform === 'win32' ?
|
||||
`${ansiEscapes.eraseScreen}${ESC}0f` :
|
||||
// 1. Erases the screen (Only done in case `2` is not supported)
|
||||
// 2. Erases the whole screen including scrollback buffer
|
||||
// 3. Moves cursor to the top-left position
|
||||
// More info: https://www.real-world-systems.com/docs/ANSIcode.html
|
||||
`${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
|
||||
|
||||
ansiEscapes.beep = BEL;
|
||||
|
||||
ansiEscapes.link = (text, url) => {
|
||||
return [
|
||||
OSC,
|
||||
'8',
|
||||
SEP,
|
||||
SEP,
|
||||
url,
|
||||
BEL,
|
||||
text,
|
||||
OSC,
|
||||
'8',
|
||||
SEP,
|
||||
SEP,
|
||||
BEL
|
||||
].join('');
|
||||
};
|
||||
|
||||
ansiEscapes.image = (buffer, options = {}) => {
|
||||
let ret = `${OSC}1337;File=inline=1`;
|
||||
|
||||
if (options.width) {
|
||||
ret += `;width=${options.width}`;
|
||||
}
|
||||
|
||||
if (options.height) {
|
||||
ret += `;height=${options.height}`;
|
||||
}
|
||||
|
||||
if (options.preserveAspectRatio === false) {
|
||||
ret += ';preserveAspectRatio=0';
|
||||
}
|
||||
|
||||
return ret + ':' + buffer.toString('base64') + BEL;
|
||||
};
|
||||
|
||||
ansiEscapes.iTerm = {
|
||||
setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
|
||||
|
||||
annotation: (message, options = {}) => {
|
||||
let ret = `${OSC}1337;`;
|
||||
|
||||
const hasX = typeof options.x !== 'undefined';
|
||||
const hasY = typeof options.y !== 'undefined';
|
||||
if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
|
||||
throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
|
||||
}
|
||||
|
||||
message = message.replace(/\|/g, '');
|
||||
|
||||
ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
|
||||
|
||||
if (options.length > 0) {
|
||||
ret +=
|
||||
(hasX ?
|
||||
[message, options.length, options.x, options.y] :
|
||||
[options.length, message]).join('|');
|
||||
} else {
|
||||
ret += message;
|
||||
}
|
||||
|
||||
return ret + BEL;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user