Elffuss BraidHub

kikocis/continue public

main · 1.4 KiBRawPermalink
1import * as readline from "readline";
2
3/**
4 * Creates a promise-based question prompt using readline
5 * Properly handles backspace and Ctrl+C
6 */
7export function question(prompt: string): Promise<string> {
8 return new Promise((resolve) => {
9 const rl = readline.createInterface({
10 input: process.stdin,
11 output: process.stdout,
12 terminal: true,
13 });
14
15 // Handle Ctrl+C properly
16 rl.on("SIGINT", () => {
17 console.log("\n");
18 rl.close();
19 process.exit(0);
20 });
21
22 rl.question(prompt, (answer) => {
23 rl.close();
24 resolve(answer);
25 });
26 });
27}
28
29/**
30 * Creates a prompt with limited choices
31 * @param prompt The prompt to display
32 * @param choices Array of valid choices
33 * @param defaultChoice Default choice if user presses enter
34 * @param limitMessage Message to show when invalid choice is entered
35 */
36export async function questionWithChoices(
37 prompt: string,
38 choices: string[],
39 defaultChoice?: string,
40 limitMessage?: string,
41): Promise<string> {
42 while (true) {
43 const answer = await question(prompt);
44
45 // Handle default choice
46 if (answer === "" && defaultChoice !== undefined) {
47 return defaultChoice;
48 }
49
50 // Check if answer is valid
51 if (choices.includes(answer)) {
52 return answer;
53 }
54
55 // Show limit message if provided
56 if (limitMessage) {
57 console.log(limitMessage);
58 }
59 }
60}