showing results for - "node stdin read char by char"
Abagail
13 Aug 2020
1const stdin = process.stdin;
2if (!stdin.isTTY) {
3  console.log('Error: TTY is not available. (Don\'t start me with nodemon.)');
4  process.exit(1);
5}
6stdin.setRawMode(true);
7stdin.setEncoding('utf8');
8stdin.resume();
9
10stdin.on('data', (key) => {
11  if (key === '\u0003') {
12    process.exit();
13  } else if (key === '\b') {
14    process.stdout.write('\b \b');
15  } else if (key === '\r') {
16    process.stdout.write('\r\n');
17  } else {
18    process.stdout.write(key);
19  }
20});