Skip to content
This repository was archived by the owner on Jul 4, 2026. It is now read-only.
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ const ask = (query) => {
return new Promise((resolve) => getRl().question(query, resolve));
};

// biome-ignore lint/suspicious/noControlCharactersInRegex: ESC char is required to match ANSI escape codes
const ansiRegex = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
const stripAnsi = (str) => {
return str.replace(ansiRegex, "");
};
Comment on lines +22 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating a new RegExp instance on every call to stripAnsi is inefficient, especially since this function is called during terminal rendering and keypress events. Additionally, the current regular expression only matches ANSI sequences ending in m (color codes), whereas a more robust pattern (like the one used in your tests) would strip all ANSI escape sequences.

We can optimize this by using a static regular expression literal with the \x1b escape character.

Suggested change
const stripAnsi = (str) => {
const ansiEscape = String.fromCharCode(27);
return str.replace(new RegExp(`${ansiEscape}\\[[0-9;]*m`, "g"), "");
};
const ansiRegex = /\x1b\[[0-9;?]*[ -/]*[@-~]/g;
const stripAnsi = (str) => {
return str.replace(ansiRegex, "");
};


const getRenderedRowCount = (text, columns = process.stdout.columns ?? 80) => {
const safeColumns = Math.max(columns || 80, 1);
return Math.max(1, Math.ceil(text.length / safeColumns));
const visibleLength = stripAnsi(text).length;
return Math.max(1, Math.ceil(visibleLength / safeColumns));
};

const resolveConfirmInput = (input, defaultValue) => {
Expand Down Expand Up @@ -228,7 +235,8 @@ const processQuestion = async (question) => {

readline.clearLine(stdout, 0);
readline.cursorTo(stdout, 0);
process.stdout.write(`${prefix} ${indexLabel}${label}${explanation}\n`);
const line = `${prefix} ${indexLabel}${label}${explanation}`;
process.stdout.write(`${line}\n`);
});
};

Expand Down
Loading