Skip to content

Instantly share code, notes, and snippets.

@davidrapin
Created July 21, 2015 23:10
Show Gist options
  • Save davidrapin/a0a4593d44f12a3bbb66 to your computer and use it in GitHub Desktop.
Save davidrapin/a0a4593d44f12a3bbb66 to your computer and use it in GitHub Desktop.
check rawmode switch
'use strict';
var state = false;
function readbin(_state) {
if (_state === state) {
console.log('not changing state (' + state + ')');
return;
}
console.log('changing state (' + _state + ')');
var stop = function() {
rawMode(false);
process.stdin.removeAllListeners('data');
}
state = _state;
if (_state) {
rawMode(true);
var listener = function(buffer) {
console.log(JSON.stringify(buffer));
if (eq(buffer, [3])) { // ctrl+c
stop()
process.exit(0);
} else if (eq(buffer, [113])) { // q
stop();
}
}
process.stdin.on('data', listener);
} else {
stop();
}
}
function rawMode(_raw) {
process.stdin.setRawMode(_raw);
}
function eq(buffer, octals) {
if (buffer.length !== octals.length) { return false; }
for (var i=0, l=buffer.length; i<l; ++i) {
if (buffer[i] !== octals[i]) { return false; }
}
return true;
}
readbin(true);
setInterval(function() {
console.log('\n [FLIP!] \n')
readbin(!state);
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment