Skip to content

Commit

Permalink
readline: allow tabs in input
Browse files Browse the repository at this point in the history
If tab completion is not being used, allow user to enter tab
characters.
  • Loading branch information
Trott committed Jun 1, 2015
1 parent 1baba05 commit 3d27eee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ function Interface(input, output, completer, terminal) {
}
historySize = historySize || kHistorySize;

completer = completer || function() { return []; };

if (typeof completer !== 'function') {
if (completer && typeof completer !== 'function') {
throw new TypeError('Argument \'completer\' must be a function');
}

Expand All @@ -72,9 +70,12 @@ function Interface(input, output, completer, terminal) {
this.historySize = historySize;

// Check arity, 2 - for async, 1 for sync
this.completer = completer.length === 2 ? completer : function(v, callback) {
callback(null, completer(v));
};
if (typeof completer === 'function') {
this.completer = completer.length === 2 ?
completer : function(v, callback) {
callback(null, completer(v));
};
}

this.setPrompt('> ');

Expand Down Expand Up @@ -344,9 +345,6 @@ Interface.prototype._normalWrite = function(b) {
};

Interface.prototype._insertString = function(c) {
//BUG: Problem when adding tabs with following content.
// Perhaps the bug is in _refreshLine(). Not sure.
// A hack would be to insert spaces instead of literal '\t'.
if (this.cursor < this.line.length) {
var beg = this.line.slice(0, this.cursor);
var end = this.line.slice(this.cursor, this.line.length);
Expand Down Expand Up @@ -839,10 +837,6 @@ Interface.prototype._ttyWrite = function(s, key) {
this._deleteRight();
break;

case 'tab': // tab completion
this._tabComplete();
break;

case 'left':
this._moveCursor(-1);
break;
Expand All @@ -867,6 +861,14 @@ Interface.prototype._ttyWrite = function(s, key) {
this._historyNext();
break;

case 'tab':
// If tab completion enabled, do that...
if (typeof this.completer === 'function') {
this._tabComplete();
break;
}
// falls through

default:
if (s instanceof Buffer)
s = s.toString('utf-8');
Expand Down
53 changes: 53 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,59 @@ function isWarned(emitter) {
assert.equal(callCount, expectedLines.length);
rli.close();

// \t when there is no completer function should behave like an ordinary
// character
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
called = false;
rli.on('line', function(line) {
assert.equal(line, '\t');
assert.strictEqual(called, false);
called = true;
});
fi.emit('data', '\t');
fi.emit('data', '\n');
assert.ok(called);
rli.close();

// \t does not become part of the input when there is a completer function
fi = new FakeInput();
var completer = function(line) {
return [[], line];
};
rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
completer: completer
});
called = false;
rli.on('line', function(line) {
assert.equal(line, 'foo');
assert.strictEqual(called, false);
called = true;
});
fi.emit('data', '\tfo\to\t');
fi.emit('data', '\n');
assert.ok(called);
rli.close();

// constructor throws if completer is not a function or undefined
fi = new FakeInput();
assert.throws(function() {
readline.createInterface({
input: fi,
completer: 'string is not valid'
});
}, function(err) {
if (err instanceof TypeError) {
if (/Argument \'completer\' must be a function/.test(err)) {
return true;
}
}
return false;
});

// sending a multi-byte utf8 char over multiple writes
var buf = Buffer('☮', 'utf8');
fi = new FakeInput();
Expand Down

0 comments on commit 3d27eee

Please sign in to comment.