forked from caspervonb/deno-prompts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
42 lines (35 loc) · 830 Bytes
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export async function promptSecret(message : string) : Promise<string | null> {
Deno.stdout.write(new TextEncoder().encode(message));
Deno.setRaw(0, true);
let input = "";
while (true) {
const data = new Uint8Array(1);
const nread = await Deno.stdin.read(data);
if (!nread) {
break;
}
const string = new TextDecoder().decode(data.slice(0, nread));
for (const char of string) {
switch (char) {
case "\u0003":
case "\u0004":
Deno.setRaw(Deno.stdin.rid, false);
console.log();
return null;
case "\r":
case "\n":
Deno.setRaw(Deno.stdin.rid, false);
console.log();
return input;
case "\u0008":
input = input.slice(0, input.length - 1);
break;
default:
input += char;
break;
}
}
}
console.log();
return null;
}