Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Created February 24, 2013 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samoshkin/5024849 to your computer and use it in GitHub Desktop.
Save samoshkin/5024849 to your computer and use it in GitHub Desktop.
// 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