mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 00:40:43 +00:00
Merged via squash.
Prepared head SHA: d754ddcf29
Co-authored-by: ai-hpc <183861985+ai-hpc@users.noreply.github.com>
Co-authored-by: hxy91819 <8814856+hxy91819@users.noreply.github.com>
Reviewed-by: @hxy91819
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { stdin as input, stdout as output } from "node:process";
|
|
import readline from "node:readline/promises";
|
|
import { isVerbose, isYes } from "../globals.js";
|
|
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
|
|
|
|
export class PromptInputClosedError extends Error {
|
|
constructor() {
|
|
super("Prompt input closed before an answer was received.");
|
|
this.name = "PromptInputClosedError";
|
|
}
|
|
}
|
|
|
|
type ReadlineInterface = ReturnType<typeof readline.createInterface>;
|
|
|
|
function questionUntilClose(rl: ReadlineInterface, question: string): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
let settled = false;
|
|
const finish = (complete: () => void) => {
|
|
if (settled) {
|
|
return;
|
|
}
|
|
settled = true;
|
|
rl.off("close", onClose);
|
|
complete();
|
|
};
|
|
const onClose = () => finish(() => reject(new PromptInputClosedError()));
|
|
|
|
rl.once("close", onClose);
|
|
void rl.question(question).then(
|
|
(answer) => finish(() => resolve(answer)),
|
|
(error: unknown) => finish(() => reject(error)),
|
|
);
|
|
});
|
|
}
|
|
|
|
export async function promptYesNo(question: string, defaultYes = false): Promise<boolean> {
|
|
// Simple Y/N prompt honoring global --yes and verbosity flags.
|
|
if (isVerbose() && isYes()) {
|
|
return true;
|
|
} // redundant guard when both flags set
|
|
if (isYes()) {
|
|
return true;
|
|
}
|
|
const rl = readline.createInterface({ input, output });
|
|
const suffix = defaultYes ? " [Y/n] " : " [y/N] ";
|
|
const answer = normalizeLowercaseStringOrEmpty(
|
|
await questionUntilClose(rl, `${question}${suffix}`).finally(() => {
|
|
rl.close();
|
|
}),
|
|
);
|
|
if (!answer) {
|
|
return defaultYes;
|
|
}
|
|
return answer.startsWith("y");
|
|
}
|