GeezerVison is showing github.com — readline: allow tabs in input · nodejs/node@90c72d4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/readline.js +14-13Lines changed: 14 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,9 +48,7 @@ function Interface(input, output, completer, terminal) { | 48 | 48 | } | 49 | 49 | historySize = historySize || kHistorySize; | 50 | 50 |
| 51 | | - completer = completer || function() { return []; }; | 52 | | - | 53 | | - if (typeof completer !== 'function') { | | 51 | + if (completer && typeof completer !== 'function') { | 54 | 52 | throw new TypeError('Argument \'completer\' must be a function'); | 55 | 53 | } | 56 | 54 |
| @@ -73,9 +71,11 @@ function Interface(input, output, completer, terminal) { | 73 | 71 | this.historySize = historySize; | 74 | 72 |
| 75 | 73 | // Check arity, 2 - for async, 1 for sync | 76 | | - this.completer = completer.length === 2 ? completer : function(v, callback) { | 77 | | - callback(null, completer(v)); | 78 | | - }; | | 74 | + if (typeof completer === 'function') { | | 75 | + this.completer = completer.length === 2 ? completer : function(v, cb) { | | 76 | + cb(null, completer(v)); | | 77 | + }; | | 78 | + } | 79 | 79 |
| 80 | 80 | this.setPrompt('> '); | 81 | 81 |
| @@ -345,9 +345,6 @@ Interface.prototype._normalWrite = function(b) { | 345 | 345 | }; | 346 | 346 |
| 347 | 347 | Interface.prototype._insertString = function(c) { | 348 | | - //BUG: Problem when adding tabs with following content. | 349 | | - // Perhaps the bug is in _refreshLine(). Not sure. | 350 | | - // A hack would be to insert spaces instead of literal '\t'. | 351 | 348 | if (this.cursor < this.line.length) { | 352 | 349 | var beg = this.line.slice(0, this.cursor); | 353 | 350 | var end = this.line.slice(this.cursor, this.line.length); | @@ -840,10 +837,6 @@ Interface.prototype._ttyWrite = function(s, key) { | 840 | 837 | this._deleteRight(); | 841 | 838 | break; | 842 | 839 |
| 843 | | - case 'tab': // tab completion | 844 | | - this._tabComplete(); | 845 | | - break; | 846 | | - | 847 | 840 | case 'left': | 848 | 841 | this._moveCursor(-1); | 849 | 842 | break; | @@ -868,6 +861,14 @@ Interface.prototype._ttyWrite = function(s, key) { | 868 | 861 | this._historyNext(); | 869 | 862 | break; | 870 | 863 |
| | 864 | + case 'tab': | | 865 | + // If tab completion enabled, do that... | | 866 | + if (typeof this.completer === 'function') { | | 867 | + this._tabComplete(); | | 868 | + break; | | 869 | + } | | 870 | + // falls through | | 871 | + | 871 | 872 | default: | 872 | 873 | if (s instanceof Buffer) | 873 | 874 | s = s.toString('utf-8'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-readline-interface.js +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -175,6 +175,59 @@ function isWarned(emitter) { | 175 | 175 | assert.equal(callCount, expectedLines.length); | 176 | 176 | rli.close(); | 177 | 177 |
| | 178 | + // \t when there is no completer function should behave like an ordinary | | 179 | + // character | | 180 | + fi = new FakeInput(); | | 181 | + rli = new readline.Interface({ input: fi, output: fi, terminal: true }); | | 182 | + called = false; | | 183 | + rli.on('line', function(line) { | | 184 | + assert.equal(line, '\t'); | | 185 | + assert.strictEqual(called, false); | | 186 | + called = true; | | 187 | + }); | | 188 | + fi.emit('data', '\t'); | | 189 | + fi.emit('data', '\n'); | | 190 | + assert.ok(called); | | 191 | + rli.close(); | | 192 | + | | 193 | + // \t does not become part of the input when there is a completer function | | 194 | + fi = new FakeInput(); | | 195 | + var completer = function(line) { | | 196 | + return [[], line]; | | 197 | + }; | | 198 | + rli = new readline.Interface({ | | 199 | + input: fi, | | 200 | + output: fi, | | 201 | + terminal: true, | | 202 | + completer: completer | | 203 | + }); | | 204 | + called = false; | | 205 | + rli.on('line', function(line) { | | 206 | + assert.equal(line, 'foo'); | | 207 | + assert.strictEqual(called, false); | | 208 | + called = true; | | 209 | + }); | | 210 | + fi.emit('data', '\tfo\to\t'); | | 211 | + fi.emit('data', '\n'); | | 212 | + assert.ok(called); | | 213 | + rli.close(); | | 214 | + | | 215 | + // constructor throws if completer is not a function or undefined | | 216 | + fi = new FakeInput(); | | 217 | + assert.throws(function() { | | 218 | + readline.createInterface({ | | 219 | + input: fi, | | 220 | + completer: 'string is not valid' | | 221 | + }); | | 222 | + }, function(err) { | | 223 | + if (err instanceof TypeError) { | | 224 | + if (/Argument \'completer\' must be a function/.test(err)) { | | 225 | + return true; | | 226 | + } | | 227 | + } | | 228 | + return false; | | 229 | + }); | | 230 | + | 178 | 231 | // sending a multi-byte utf8 char over multiple writes | 179 | 232 | var buf = Buffer('☮', 'utf8'); | 180 | 233 | fi = new FakeInput(); | |
|