Skip to content

Commit

Permalink
Add writeFilter when in the readline
Browse files Browse the repository at this point in the history
Switch \n with \r\n for all strings printed out.
Necessary for writev patch.
  • Loading branch information
ry committed Nov 12, 2010
1 parent c7b24ef commit d4af8a6
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = "";

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit d4af8a6

Please sign in to comment.