Skip to content

Commit

Permalink
cli: simplify cli autocompletion
Browse files Browse the repository at this point in the history
Remove unnecessary recursion.

PR-URL: #150
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
  • Loading branch information
lundibundi authored and belochub committed Jan 22, 2018
1 parent a01a4bc commit 1e65143
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,27 @@ function completer(line) {
// function or '.help()' or neither
// (no completions or help available)
function iterativeCompletion(inputs, depth, completer) {
function helper(depth, oldDepth, completer, completions) {
let help = '';
let showHelp = false;

if (completions.length === 1 && completer[completions[0]]) {
completer = completer[completions[0]];
if (depth < inputs.length && completer.complete) {
const [newCompletions, newDepth] = completer.complete(inputs, depth);
if (newDepth >= inputs.length) return [newCompletions, ''];
return helper(newDepth, depth, completer, newCompletions);
let help = '';
let completions = [];
let newDepth = depth;
if (completer.complete) {
do {
depth = newDepth;
[completions, newDepth] = completer.complete(inputs, newDepth);
if (completions.length === 1 && completer[completions[0]]) {
completer = completer[completions[0]];
} else {
break;
}
showHelp = inputs[oldDepth] === completions[0];
}
if (inputs[depth] === '') completions = [];
if (showHelp || !completions[0]) {
if (completer.help) help = completer.help();
return [[], help];
}
return [completions, help];
} while (newDepth < inputs.length && completer.complete);
}
if (completer.complete) {
const [newCompletions, newDepth] = completer.complete(inputs, depth);
return helper(newDepth, depth, completer, newCompletions);
if (newDepth <= inputs.length - 1) completions = [];
if (!completions[0] ||
completions.length === 1 && completions[0] === inputs[depth]) {
completions = [];
if (completer.help) help = completer.help();
}
if (completer.help) return [[], completer.help()];
return [[], ''];
return [completions, help];
}

rl.on('line', (line) => {
Expand Down

0 comments on commit 1e65143

Please sign in to comment.