const input = Deno.stdin;const output = Deno.stdout;const encoder = new TextEncoder();const decoder = new TextDecoder();const LF = "\n".charCodeAt(0); const CR = "\r".charCodeAt(0); const BS = "\b".charCodeAt(0); const DEL = 0x7f; const CLR = encoder.encode("\r\u001b[K");
const setRawOptions = Deno.build.os === "windows" ? undefined : { cbreak: true };
export type PromptSecretOptions = { mask?: string; clear?: boolean;};
export function promptSecret( message = "Secret", { mask = "*", clear }: PromptSecretOptions = {},): string | null { if (!input.isTerminal()) { return null; }
message += " "; const callback = !mask ? undefined : (n: number) => { output.writeSync(CLR); output.writeSync(encoder.encode(`${message}${mask.repeat(n)}`)); }; output.writeSync(encoder.encode(message));
Deno.stdin.setRaw(true, setRawOptions); try { return readLineFromStdinSync(callback); } finally { if (clear) { output.writeSync(CLR); } else { output.writeSync(encoder.encode("\n")); } Deno.stdin.setRaw(false); }}
function readLineFromStdinSync(callback?: (n: number) => void): string { const c = new Uint8Array(1); const buf = [];
while (true) { const n = input.readSync(c); if (n === null || n === 0) { break; } if (c[0] === CR || c[0] === LF) { break; } if (c[0] === BS || c[0] === DEL) { buf.pop(); } else { buf.push(c[0]!); } if (callback) callback(buf.length); } return decoder.decode(new Uint8Array(buf));}