Browse Source

Add writeFilter when in the readline

Switch \n with \r\n for all strings printed out.
Necessary for writev patch.
v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
d4af8a6b6a
  1. 22
      lib/readline.js

22
lib/readline.js

@ -17,6 +17,20 @@ exports.createInterface = function (output, completer) {
return new Interface(output, completer);
};
function writeFilter (stream) {
if (stream._writeFiltered) return;
stream._writeFiltered = true;
stream._normalWrite = stream.write;
stream.write = function (d) {
var args = Array.prototype.slice.call(arguments);
if (typeof d == 'string') {
args[0] = d.replace(/([^\r])\n|^\n/g, '$1\r\n');
}
// TODO what about buffers?
return stream._normalWrite.apply(stream, args);
}
}
function Interface (output, completer) {
if (!(this instanceof Interface)) return new Interface(output, completer);
EventEmitter.call(this);
@ -35,6 +49,9 @@ function Interface (output, completer) {
if (this.enabled) {
// input refers to stdin
writeFilter(this.output);
writeFilter(process.stdout);
// Current line
this.line = "";
@ -99,8 +116,6 @@ Interface.prototype._addHistory = function () {
Interface.prototype._refreshLine = function () {
if (this._closed) return;
stdio.setRawMode(true);
// Cursor to left edge.
this.output.write('\x1b[0G');
@ -288,8 +303,7 @@ Interface.prototype._ttyWrite = function (b) {
case 13: /* enter */
var line = this._addHistory();
this.output.write('\n\x1b[0G');
stdio.setRawMode(false);
this.output.write('\r\n');
this.emit('line', line);
break;

Loading…
Cancel
Save