-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (40 loc) · 1.13 KB
/
index.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs');
function readStdinUntilNewline() {
const BUFFER_SIZE = 256;
let totalBuf = Buffer.alloc(0);
let fileDescriptor = process.stdin.fd;
let closeFs = false;
try {
fileDescriptor = process.stdin.isTTY ? fs.openSync('/dev/tty', 'rs') : fs.openSync('/dev/stdin', 'rs');
closeFs = true;
} catch (e) {}
while (true) {
const buffer = Buffer.alloc(BUFFER_SIZE);
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, BUFFER_SIZE, null);
if (bytesRead === 0) {
break;
}
totalBuf = Buffer.concat([totalBuf, buffer.subarray(0, bytesRead)]);
if (buffer.subarray(0, bytesRead).includes('\n')) {
break;
}
}
if (closeFs) {
fs.closeSync(fileDescriptor);
}
return totalBuf;
}
let stdinBuffer = '';
function input(prompt) {
if (prompt) {
process.stdout.write(prompt);
}
if (stdinBuffer.length === 0) {
stdinBuffer = readStdinUntilNewline().toString('utf-8');
}
const newline = stdinBuffer.indexOf('\n');
const line = stdinBuffer.slice(0, newline);
stdinBuffer = stdinBuffer.slice(newline + 1);
return line;
}
module.exports = input;