Created
February 24, 2013 18:03
-
-
Save samoshkin/5024849 to your computer and use it in GitHub Desktop.
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
// inspired by this http://stackoverflow.com/a/12506613 | |
// + emit SIGINT, and handle "exit" event | |
var stdin = process.stdin; | |
// without this, we would only get streams once enter is pressed | |
stdin.setRawMode( true ); | |
// resume stdin in the parent process (node app won't quit all by itself | |
// unless an error or process.exit() happens) | |
stdin.resume(); | |
// i don't want binary, do you? | |
stdin.setEncoding( 'utf8' ); | |
// on any data into stdin | |
stdin.on( 'data', function( key ){ | |
// ctrl-c ( end of text ) | |
if ( key === '\u0003' ) { | |
// emulation of SIGINT event | |
// make it uniform between windows and unix | |
process.emit("SIGINT"); | |
} | |
// write the key to stdout all normal like | |
process.stdout.write(key); | |
}); | |
// handle SIGINT, and exit | |
process.on("SIGINT", function(){ | |
console.error("Handle SIGINT event"); | |
process.exit(-1); | |
}); | |
// last chance to hook into before node app exits | |
process.on("exit", function(){ | |
console.error("handle exit..."); | |
process.nextTick(function(){ | |
// no more events will be run from queue | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment