Command.prototype.password = function(str, mask, fn){ var self = this , buf = ''; // default mask if ('function' == typeof mask) { fn = mask; mask = ''; } process.stdin.resume(); tty.setRawMode(true); process.stdout.write(str); // keypress process.stdin.on('keypress', function(c, key){ if (key && 'enter' == key.name) { console.log(); process.stdin.removeAllListeners('keypress'); tty.setRawMode(false); if (!buf.trim().length) return self.password(str, mask, fn); fn(buf); return; } if (key && key.ctrl && 'c' == key.name) { console.log('%s', buf); process.exit(); } process.stdout.write(mask); buf += c; }).resume(); };
var onKeyPress = function(chunk, key) { if (isActive) { if (key) { if (key.ctrl && key.name === 'c') { process.exit(); } else if (key.name === 'tab') { var output = doPathCompletion(); input += output; process.stdout.write(output); } else if (key.name === 'enter') { isActive = false; process.stdout.write('\n'); tty.setRawMode(false); process.stdin.pause(); if (callback) { callback(path.resolve(input)); } } else if (key.name === 'backspace') { if (input === '') return; input = input.substr(0, input.length-1); process.stdout.write('\b \b'); } else if (chunk) { input += chunk; process.stdout.write(chunk); } } else if (chunk) { input += chunk; process.stdout.write(chunk); } } };
exports.keycontrol = function keycontrol(){ keypress(process.stdin); function turnUpDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('up');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnLeftDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('left');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnDownDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('down');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } function turnRightDegrees(degrees){ stopTime = Math.floor(degrees * 22.3) setTimeout(function(){ThunderConnector.command('right');},0); setTimeout(function(){ThunderConnector.command('stop');},stopTime); } // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) { console.log('got "keypress"', key); if (key.name == "w") { turnUpDegrees(2.3); // if (key.name == "shift" && key.name == "w") // { // turnUpDegrees(1); // } } if (key.name == "a") { turnLeftDegrees(2.3); } if (key.name == "s") { turnDownDegrees(2.3); } if (key.name == "d") { turnRightDegrees(2.3); } if (key.name == "f") { // console.log("Fire"); setTimeout(function(){ThunderConnector.command('fire');},0); setTimeout(function(){ThunderConnector.command('stop');},10000); } if (key && key.ctrl && key.name == 'c') { process.stdin.pause(); } }); if (typeof process.stdin.setRawMode == 'function') { process.stdin.setRawMode(true); } else { tty.setRawMode(true); } process.stdin.resume(); }
function UltraRLI(input, output, completer){ var self = this; this.output = output; this.input = input; input.resume(); completer = completer || function(){ return [] }; this.completer = completer.length === 2 ? completer : function(v, callback){ callback(null, completer(v)); }; this.line = ''; this.enabled = output.isTTY && !parseInt(process.env['NODE_NO_READLINE'], 10); this.cursor = 0; this.history = []; this.historyIndex = -1; this._promptLength = 0; this._prompt = ''; this.resize.last = []; if (!this.enabled) { input.on('data', function(s, key){ self._ttyWrite(s, key) }); this.resize(); } else { input.on('keypress', function(s, key){ self._ttyWrite(s, key) }); tty.setRawMode(true); this.resize(); if (process.listeners('SIGWINCH').length === 0) { process.on('SIGWINCH', this.resize.bind(this)); } } }
var get_password = function(prompt) { console.log(prompt); process.stdin.resume(); process.stdin.setEncoding('utf8'); tty.setRawMode(true); password = '' process.stdin.on('data', function (char) { char = char + "" switch (char) { case "\n": case "\r": case "\u0004": // They've finished typing their password tty.setRawMode(false) console.log("\nyou entered: "+password) stdin.pause() break case "\u0003": // Ctrl C console.log('Cancelled') process.exit() break default: // More passsword characters //process.stdout.write('*') password += char break } }); }
promptHidden: function (message, callback) { var d = qutil.convert(callback), value = ''; function onKeyPress(c, key) { if (key && key.ctrl && c === 'c') { process.exit(); } else if (c === '\r' || c === '\n' || c === '\u0004') { //End of input, finish up. process.stdin.removeListener('keypress', onKeyPress); tty.setRawMode(false); process.stdin.pause(); d.resolve(value); } else if (c === '\x7f' || c === '\x08') { //A backspace/delete character, remove a char //from the value. value = value.slice(0, -1); } else { value += c; } } tty.setRawMode(true); process.stdin.on('keypress', onKeyPress); process.stdin.resume(); process.stdout.write(message + ' ', 'utf8'); return d.promise; },
function exit(){ charm.display('reset'); charm.erase('screen'); tty.setRawMode(false); charm.destroy(); process.exit(); }
stdin.on('data', function data (line) { line = line + ''; for(var i = 0; i < line.length; i++) { var c = line[i]; switch (c) { case '\n': case '\r': case '\r\n': case '\u0004': try { tty.setRawMode(false) } catch (ex) { } stdin.removeListener('data', data); stdin.removeListener('error', callback); value = value.trim(); stdout.write('\n'); stdout.flush && stdout.flush(); prompt.pause(); return callback(null, value); case '\x7f': case'\x08': value = value.slice(0,-1); break; case '\u0003': case '\0': stdout.write('\n'); process.exit(1); break; default: value = value + c; break; } } });
var run = function(opts) { var example, server; try { example = new Example(opts || {}); server = example.getServer(); tty.setRawMode(true); process.stdin.resume(); server.listen(example.port, function() { console.log("Server is running on http://localhost:" + example.port); console.log("Hit CTRL+C to shutdown the http server"); }); server.on('close', function() { console.log('Shutting down the server.'); }); // exit event is assigned here becuase it requires the closure to server and port process.on('exit', function(){ if(server) server.close(); }); process.on('SIGTERM', function(){ process.exit(1); }); } catch(err) { console.log(err.message); } return example; };
function setRawMode(mode) { if (process.stdin.setRawMode) { process.stdin.setRawMode(mode); } else { tty.setRawMode(mode); } };
function silentRead (def, cb) { var stdin = process.openStdin(); var val = ""; tty.setRawMode(true); stdin.resume(); stdin.on("error", cb); stdin.on("data", function D (c) { c = "" + c; switch (c) { case "\n": case "\r": case "\r\n": case "\u0004": tty.setRawMode(false); stdin.removeListener("data", D); stdin.removeListener("error", cb); val = val.trim() || def; process.stdout.write("\n"); stdin.pause(); return cb(null, val); case "\u0003": case "\0": return cb("cancelled"); break; default: val += buffer + c; buffer = ""; break; } }); };
function onData(line) { var i, c; line = line.toString(); for(i = 0; i < line.length; i += 1) { c = line[i]; if (c === '\r' || c === '\n' || c === '\u0004') { //End of input, finish up. process.stdin.removeListener('error', onError); process.stdin.removeListener('data', onData); tty.setRawMode(false); process.stdin.pause(); d.resolve(value.trim()); return; } else if (c === '\x7f' || c === '\x08') { //A backspace/delete character, remove a char //from the value. value = value.slice(0, -1); } else if (c === '\u0003' || c === '\u0000') { //End of discussion! process.exit(1); } else { value = value + c; } } }
var getPassword = function (currentStr, callback) { var self = this , buf = ''; process.stdin.resume(); tty.setRawMode(true); fs.writeSync(istty1 ? 1 : 2, currentStr); // keypress process.stdin.on('keypress', function (c, key) { if (key && 'enter' === key.name) { console.log(); process.stdin.removeAllListeners('keypress'); tty.setRawMode(false); callback(buf); return; } if (key && key.ctrl && 'c' === key.name) { console.log('%s', buf); process.exit(); } process.stdout.write(mask); buf += c; }).resume(); }
function finish () { if (stream.in === process.stdin) { tty.setRawMode(false); } if (stream.in.pause) stream.in.pause(); stream.in.removeListener('data', ondata); }
function raw (mode) { if (process.stdin.setRawMode) { process.stdin.setRawMode(mode) } else { tty.setRawMode(mode) } }
Interface.prototype.resume = function() { this.input.resume(); if (this.enabled) { tty.setRawMode(true); } this.paused = false; this.emit('resume'); };
function Interface(input, output, completer) { if (!(this instanceof Interface)) { return new Interface(input, output, completer); } EventEmitter.call(this); var self = this; this.output = output; this.input = input; input.resume(); this.completer = completer; this.setPrompt('> '); this.enabled = output.isTTY; if (parseInt(process.env['NODE_NO_READLINE'], 10)) { this.enabled = false; } if (!this.enabled) { input.on('data', function(data) { self._normalWrite(data); }); } else { // input usually refers to stdin input.on('keypress', function(s, key) { self._ttyWrite(s, key); }); // Current line this.line = ''; // Check process.env.TERM ? tty.setRawMode(true); this.enabled = true; // Cursor position on the line. this.cursor = 0; this.history = []; this.historyIndex = -1; var winSize = tty.getWindowSize(output.fd); exports.columns = winSize[1]; if (process.listeners('SIGWINCH').length === 0) { process.on('SIGWINCH', function() { var winSize = tty.getWindowSize(output.fd); exports.columns = winSize[1]; }); } } }
exports.instantOn = function(cb){ instantCallback = cb; if(!instantProcesser) return false; //没指定处理函数 //暂停readline,打开滚动,监听按键 rl.pause(); tty.setRawMode(true); process.stdin.on('keypress', onKeypress); console.log('Instant ON'); };
close: function close(d) { if (this._closing) return; this._closing = true; if (this.enabled) { tty.setRawMode(false); } this.emit('close'); this._closed = true; },
Interface.prototype.pause = function() { if (this.paused) return; if (this.enabled) { tty.setRawMode(false); } this.input.pause(); this.paused = true; this.emit('pause'); };
exports.open = function(ops) { if(ops) { if(ops.prefix) exports.inputPrefix(ops.prefix); } if(exports.opened) return; exports.opened = true; stdin = process.openStdin(); stdin.setEncoding('utf8'); tty.setRawMode(true); stdin.on('keypress', function(c, key) { //integer? if(!key) key = { name: c.toString() } var chain = stringRep(key); var emitSuccess = chain.length > 1 ? exports.emit(chain) : false; //not a handled item to boot? Probably enter, delete, left, right, up, etc. Append it. if(!emitSuccess && c) { if(!_buffer.length) { exports.insertText(exports.inputPrefix(), _cursorPosition); } exports.insertText(c, _cursorPosition); //exports.insertText(c, Math.max(_cursorPosition, exports.inputPrefix().length)); } //new line? reset if(key && key.name == 'enter') { //exports.replaceLine(exports.buffer().magenta); process.stdout.write('\n'); _buffer = ''; _cursorPosition = 0; //exports.insertText(exports.inputPrefix(), _cursorPosition); //exports.cursor(0); } //for custom handlers: password, confirm, etc. exports.emit('keypress', { char: c, key: key }); //character code? if(key.name.length == 1) { exports.emit('charpress', c); } }); }
Interface.prototype.close = function(d) { if (this._closing) return; this._closing = true; if (this.enabled) { tty.setRawMode(false); } this.emit('close'); this._closed = true; };
function setRawMode(mode) { if (process.stdin.setRawMode) { process.stdin.setRawMode(mode); } else { //only do this if we're in a child process if(process.stdout.isTTY){ tty.setRawMode(mode); } } };
function raw (stdin, mode) { if (stdin.setRawMode && stdin.isTTY) { stdin.setRawMode(mode) return } // old style try { tty.setRawMode(mode) } catch (e) {} }
exports.instantOff = function(){ if(instantCallback) instantCallback(); process.stdin.removeListener('keypress', onKeypress); tty.setRawMode(false); //先把模式设置回来,再开启readline rl.resume(); rl.line = ''; //把instant模式下的所有输入清空 console.log('Instant OFF'); prompt(); };
var exports = module.exports = function () { var input = null; function setInput (s) { if (input) throw new Error('multiple inputs specified') else input = s } var output = null; function setOutput (s) { if (output) throw new Error('multiple outputs specified') else output = s } for (var i = 0; i < arguments.length; i++) { var arg = arguments[i]; if (!arg) continue; if (arg.readable) setInput(arg) else if (arg.stdin || arg.input) setInput(arg.stdin || arg.input) if (arg.writable) setOutput(arg) else if (arg.stdout || arg.output) setOutput(arg.stdout || arg.output) } if (input && typeof input.fd === 'number' && tty.isatty(input.fd)) { if (process.stdin.setRawMode) { process.stdin.setRawMode(true); } else tty.setRawMode(true); } var charm = new Charm; if (input) { input.pipe(charm); } if (output) { charm.pipe(output); } charm.once('^C', process.exit); charm.once('end', function () { if (input) { if (typeof input.fd === 'number' && tty.isatty(input.fd)) { if (process.stdin.setRawMode) { process.stdin.setRawMode(false); } else tty.setRawMode(false); } input.destroy(); } }); return charm; };
charm.once('end', function () { if (input) { if (typeof input.fd === 'number' && tty.isatty(input.fd)) { if (process.stdin.setRawMode) { process.stdin.setRawMode(false); } else tty.setRawMode(false); } input.destroy(); } });
passwd = function (charData, key) { if (key !== undefined) { // key parameter is undefined when the acquired character is a number if (key.ctrl && key.name === 'c') { tty.setRawMode(false); // modified to comply with node.js 0.6 process.exit(); } switch (key.name) { case "enter": stdout.write("\n"); tty.setRawMode(false); // modified to comply with node.js 0.6 stdin.pause(); if (invalid_char === true) { error.code = AuthError.prototype.UNKNOWN_ERROR; // we don't use an error message like "invalid character" to avoid information leakage error.message = "Wrong username or password"; callback(error, pswd); } else { callback(null, pswd); } pswd = ""; break; case "backspace": pswd = pswd.substring(0, pswd.length - 1); break; // invalid characters case "space": case "tab": invalid_char = true; break; default: pswd = pswd + charData; } } else { if (charData !== undefined) { // when the acquired character is a number, only charData parameter is defined pswd = pswd + charData; } } };
promptHidden: function (message, callback) { var d = qutil.convert(callback), value = ''; function onError(e) { d.reject(e); } function onData(line) { var i, c; line = line.toString(); for(i = 0; i < line.length; i += 1) { c = line[i]; if (c === '\r' || c === '\n' || c === '\u0004') { //End of input, finish up. process.stdin.removeListener('error', onError); process.stdin.removeListener('data', onData); tty.setRawMode(false); process.stdin.pause(); d.resolve(value.trim()); return; } else if (c === '\x7f' || c === '\x08') { //A backspace/delete character, remove a char //from the value. value = value.slice(0, -1); } else if (c === '\u0003' || c === '\u0000') { //End of discussion! process.exit(1); } else { value = value + c; } } } tty.setRawMode(true); process.stdin.resume(); process.stdin.on('error', onError); process.stdin.on('data', onData); process.stdout.write(message + ' ', 'utf8'); return d.promise; },
var Charm = exports.Charm = function (input, output) { var self = this; self.input = input; self.output = output; self.pending = []; if (!output) { self.emit('error', new Error('output stream required')); } if (input && typeof input.fd === 'number' && tty.isatty(input.fd)) { if (process.stdin.setRawMode) { process.stdin.setRawMode(true); } else tty.setRawMode(true); input.resume(); } if (input) { input.on('data', function (buf) { if (self.pending.length) { var codes = extractCodes(buf); var matched = false; for (var i = 0; i < codes.length; i++) { for (var j = 0; j < self.pending.length; j++) { var cb = self.pending[j]; if (cb(codes[i])) { matched = true; self.pending.splice(j, 1); break; } } } if (matched) return; } self.emit('data', buf) if (buf.length === 1) { if (buf[0] === 3) self.emit('^C'); if (buf[0] === 4) self.emit('^D'); } }); } }