Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
jblandry committed Mar 29, 2017
1 parent 59116b2 commit 9b81579
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
70 changes: 69 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const os = require('os');
const path = require('path');
const glob = require('glob');
const readPkgUp = require('read-pkg-up');
const omelette = require('omelette');
const chalk = require('chalk');
const indentString = require('indent-string');
const stringWidth = require('string-width');
Expand Down Expand Up @@ -50,6 +51,73 @@ const cmdUsage = (cmd, length, spacer) => {
return `${chalk.yellow(length ? pad(call, length) : call)}${' '.repeat(spacer)}${desc}`;
};

const initAutocomplete = () => {

// Add array of params
const _addParams = (tree, lists) => {
if (lists) {
lists[0].forEach((param1) => {
tree[param1] = {};

if (lists[1]) {
lists[1].forEach((param2) => {
tree[param1][param2] = {};
});
}
});
}
};

// Build autocomplete tree
const autocomplete = {};
Object.keys(STATIC.commands).forEach((task) => {
const treeTask = STATIC.commands[task];
autocomplete[task] = {};

if (!Array.isArray(treeTask)) {
Object.keys(treeTask).forEach((subtask) => {
const treeSubtask = treeTask[subtask];
autocomplete[task][subtask] = {};
_addParams(autocomplete[task][subtask], treeSubtask[2]);
});

} else {
_addParams(autocomplete[task], treeTask[2]);
}
});


// Breks eggs
const complete = omelette(pkg.name);

complete.on('$1', function() {
try {
this.reply(Object.keys(autocomplete));
} catch (e) {} // eslint-disable-line no-empty
});

complete.on('$2', function(lvl1) {
try {
this.reply(Object.keys(autocomplete[lvl1]));
} catch (e) {} // eslint-disable-line no-empty
});

complete.on('$3', function(lvl2, line) {
const [, lvl1] = line.split(' ');
try {
this.reply(Object.keys(autocomplete[lvl1][lvl2]));
} catch (e) {} // eslint-disable-line no-empty
});

complete.on('$4', function(lvl3, line) {
const [, lvl1, lvl2] = line.split(' ');
try {
this.reply(Object.keys(autocomplete[lvl1][lvl2][lvl3]));
} catch (e) {} // eslint-disable-line no-empty
});

complete.init();
};



Expand All @@ -74,7 +142,7 @@ module.exports = class Cli {
//-- Set tasks
static setUsageTasks(commands) {
STATIC.commands = commands;

initAutocomplete();
}


Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@absolunet/cli",
"version": "0.0.4",
"version": "0.1.0",
"description": "CLI utilities",
"definition": "",
"homepage": "https://github.com/absolunet/node-cli",
Expand All @@ -16,13 +16,14 @@
},

"devDependencies": {
"@absolunet/tester": "0.0.2"
"@absolunet/tester": "0.0.4"
},

"dependencies": {
"chalk": "^1.1.3",
"glob": "^7.1.1",
"indent-string": "^3.1.0",
"omelette": "^0.3.2",
"read-pkg-up": "^2.0.0",
"string-width": "^2.0.0",

Expand Down

0 comments on commit 9b81579

Please sign in to comment.