Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Handle overflow in promptSecret #6318

Merged
merged 11 commits into from
Jan 10, 2025
Prev Previous commit
Next Next commit
handle multiline prompt that deletes characters
cosmastech committed Jan 6, 2025

Verified

This commit was signed with the committer’s verified signature.
luehm Alex Luehm
commit b690b4f314523599f9b7db799235766f95b77f92
21 changes: 17 additions & 4 deletions cli/prompt_secret.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const CR = "\r".charCodeAt(0); // ^M - Enter on macOS and Windows (CRLF)
const BS = "\b".charCodeAt(0); // ^H - Backspace on Linux and Windows
const DEL = 0x7f; // ^? - Backspace on macOS
const CLR = encoder.encode("\r\u001b[K"); // Clear the current line
const MOVE_LINE_UP = encoder.encode("\r\u001b[1F"); // Move to previous line

// The `cbreak` option is not supported on Windows
const setRawOptions = Deno.build.os === "windows"
@@ -53,24 +54,36 @@ export function promptSecret(
}

const { columns } = Deno.consoleSize();

let previousLength = 0;
// Make the output consistent with the built-in prompt()
message += " ";
const callback = !mask ? undefined : (n: number) => {
let line = `${message}${mask.repeat(n)}`;

const currentLength = line.length;
const charsPastLineLength = line.length % columns;

if (line.length > columns) {
line = line.slice(
-1 * (charsPastLineLength === 0 ? columns : charsPastLineLength),
);
}
// Always jump the cursor back to the beginning of the line unless it's the first character.
if (charsPastLineLength !== 1) {

// If the user has deleted a character
if (currentLength < previousLength) {
// Then clear the current line
output.writeSync(CLR);
if (charsPastLineLength === 0) {
// And if there's no characters on the current line, return to previous line
output.writeSync(MOVE_LINE_UP);
}
} else {
// Always jump the cursor back to the beginning of the line unless it's the first character.
if (charsPastLineLength !== 1) {
output.writeSync(CLR);
}
}
output.writeSync(encoder.encode(line));
previousLength = currentLength;
};

output.writeSync(encoder.encode(message));