Last active
May 22, 2023 17:19
-
-
Save lahmatiy/9b44078e1ecdf81c4718 to your computer and use it in GitHub Desktop.
Snippet for node.js to prevent any user input while script is working, but process ctrl+c to exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// doesn't work w/o this interface creation | |
require('readline').createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
// stop process input | |
process.stdin.pause(); | |
// read from stdin 10 times per second to check if ctrl+c pressed | |
setInterval(function(){ | |
var chunk = process.stdin.read(); | |
if (chunk) | |
for (var i = 0; i < chunk.length; i++) { | |
if (chunk[i] == 3) // ^C | |
process.exit(); | |
} | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, useful.