This commit is contained in:
seb
2026-09-23 00:50:44 +02:00
parent 73e7f7e7f8
commit 5aab24998d
8 changed files with 499 additions and 16 deletions

View File

@@ -21,6 +21,7 @@ import {
createPrompt,
useState,
useKeypress,
useEffect,
usePrefix,
isEnterKey,
isTabKey,
@@ -55,9 +56,10 @@ function replaceBuffer(rl, nextValue) {
export const autocompleteInput = createPrompt((config, done) => {
const theme = makeTheme({}, config.theme);
const initial = typeof config.default === 'string' ? config.default : '';
const [status, setStatus] = useState('idle');
const [value, setValue] = useState('');
const [term, setTerm] = useState(null); // frozen filter term while the menu is open
const [value, setValue] = useState(initial);
const [term, setTerm] = useState(termFrom(initial)); // frozen filter term while the menu is open
const [active, setActive] = useState(0); // highlighted suggestion index
const prefix = usePrefix({ status, theme });
@@ -100,6 +102,13 @@ export const autocompleteInput = createPrompt((config, done) => {
setActive(0);
});
// Prefill the editable readline buffer (and mirror it into `value`) when an
// initial value is supplied — used to restore a message that a Ctrl+C
// cancelled back into the prompt so it can be edited and resent.
useEffect((rl) => {
if (initial) rl.write(initial);
}, []);
const message = theme.style.message(config.message);
const formattedValue = status === 'done' ? theme.style.answer(value) : value;