Add writeFilter when in the readline

Switch \n with \r\n for all strings printed out.
Necessary for writev patch.
This commit is contained in:
Ryan Dahl committed 2010-11-16 10:49:26 -08:00
1 parent 7590075ea7
commit 44db37859d
1 file changed
+18 -4
+18 -4
View File
@@ -19,6 +19,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);
@@ -37,6 +51,9 @@ function Interface (output, completer) {
if (this.enabled) {
// input refers to stdin
writeFilter(this.output);
writeFilter(process.stdout);
// Current line
this.line = "";
@@ -101,8 +118,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');
@@ -290,8 +305,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;