repl completion: multi-column display of completions
This commit is contained in:
parent
8d5e05668b
commit
72e4a49cac
@ -41,7 +41,6 @@ function Interface (output, completer) {
|
|||||||
// Check process.env.TERM ?
|
// Check process.env.TERM ?
|
||||||
stdio.setRawMode(true);
|
stdio.setRawMode(true);
|
||||||
this._tty = true;
|
this._tty = true;
|
||||||
this.columns = stdio.getColumns();
|
|
||||||
|
|
||||||
// Cursor position on the line.
|
// Cursor position on the line.
|
||||||
this.cursor = 0;
|
this.cursor = 0;
|
||||||
@ -53,6 +52,11 @@ function Interface (output, completer) {
|
|||||||
|
|
||||||
inherits(Interface, EventEmitter);
|
inherits(Interface, EventEmitter);
|
||||||
|
|
||||||
|
Interface.prototype.__defineGetter__("columns", function () {
|
||||||
|
if (this._tty) {
|
||||||
|
return stdio.getColumns();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Interface.prototype.setPrompt = function (prompt, length) {
|
Interface.prototype.setPrompt = function (prompt, length) {
|
||||||
this._prompt = prompt;
|
this._prompt = prompt;
|
||||||
@ -156,14 +160,48 @@ Interface.prototype._tabComplete = function () {
|
|||||||
self._insertString(completions[0].slice(completeOn.length));
|
self._insertString(completions[0].slice(completeOn.length));
|
||||||
self._refreshLine();
|
self._refreshLine();
|
||||||
} else {
|
} else {
|
||||||
//TODO: Multi-column display. Request to show if more than N completions.
|
//TODO: If there is a common prefix to all matches (e.g. Python `sys.exi<Tab>`) then apply that portion
|
||||||
self.output.write("\n");
|
self.output.write("\r\n");
|
||||||
completions.forEach(function (c) {
|
var width = completions.reduce(function(a, b) {
|
||||||
//TODO: try using '\r\n' instead of the following goop for getting to column 0
|
return a.length > b.length ? a : b;
|
||||||
self.output.write('\x1b[0G');
|
}).length + 2; // 2 space padding
|
||||||
self.output.write(c + "\n");
|
var maxColumns = Math.floor(this.columns / width) || 1;
|
||||||
})
|
|
||||||
self.output.write('\n');
|
function handleGroup(group) {
|
||||||
|
if (group.length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var minRows = Math.ceil(group.length / maxColumns);
|
||||||
|
for (var row = 0; row < minRows; row++) {
|
||||||
|
for (var col = 0; col < maxColumns; col++) {
|
||||||
|
var idx = row * maxColumns + col;
|
||||||
|
if (idx >= group.length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var item = group[idx];
|
||||||
|
self.output.write(item);
|
||||||
|
if (col < maxColumns - 1) {
|
||||||
|
for (var s = 0; s < width - item.length; s++) {
|
||||||
|
self.output.write(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.output.write('\r\n');
|
||||||
|
}
|
||||||
|
self.output.write('\r\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
var group = [], c;
|
||||||
|
for (var i = 0; i < completions.length; i++) {
|
||||||
|
c = completions[i];
|
||||||
|
if (c === "") {
|
||||||
|
handleGroup(group);
|
||||||
|
group = [];
|
||||||
|
} else {
|
||||||
|
group.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
handleGroup(group);
|
||||||
self._refreshLine();
|
self._refreshLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user