Skip to content

Commit

Permalink
cli: fix cli autocompletion
Browse files Browse the repository at this point in the history
* Fix iterativeCompletion doc (function naming was wrong).
* Fix repetitive completion ('c<space><tab>' would have
  been completed as 'c<tab>').

PR-URL: #140
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
  • Loading branch information
lundibundi authored and belochub committed May 7, 2017
1 parent d1d8144 commit 5fa7a4c
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,26 @@ function completer(line) {

// inputs - array of user inputs
// depth - level of nested completion (index in inputs array)
// completer - object that has '_complete(inputs, depth)' function or ._help()
// or neither (no completions or help available)
// completer - object that has '.complete(inputs, depth)'
// function or '.help()' or neither
// (no completions or help available)
function iterativeCompletion(inputs, depth, completer) {
function helper(depth, oldDepth, completer, completions) {
let help = '';

if (completions.length !== 1) return [completions, help];
const nextCompleter = completer[completions[0]];
if (!nextCompleter) return [completions, help];

if (nextCompleter.complete && depth < inputs.length) {
const [newCompletions, newDepth] = nextCompleter.complete(inputs, depth);
return helper(newDepth, depth, nextCompleter, newCompletions);
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);
}
showHelp = inputs[oldDepth] === completions[0];
}
if (inputs[oldDepth] === completions[0]) {
if (nextCompleter.help) help = nextCompleter.help();
if (inputs[depth] === '') completions = [];
if (showHelp || !completions[0]) {
if (completer.help) help = completer.help();
return [[], help];
}
return [completions, help];
Expand Down

0 comments on commit 5fa7a4c

Please sign in to comment.