diff --git a/index.js b/index.js index fee58ca..46900c7 100644 --- a/index.js +++ b/index.js @@ -13,15 +13,18 @@ const log = console.log; const API_URL = process.env.IKALAS_API_URL; +let ikalas_commands = []; + if (process.env.ENABLE_IKALAS == "yes") { axios.get(`${API_URL}/api/v1/functions`).then((result) => { if (result != null && result.data != null) { - result.data.forEach(function (fn) { - suggestedCommands.push({ + ikalas_commands = result.data.map(function (fn) { + return { name: fn.nameFunction, summary: fn.summaryFunction, - }); + }; }); + suggestedCommands.unshift(...ikalas_commands); } }); } @@ -34,17 +37,9 @@ Array.prototype.unique = function () { readline.emitKeypressEvents(process.stdin); console.log("\033[2J"); -var suggestedCommands = []; var historyCommands = shellHistory(); -historyCommands.forEach(function (historyCommand) { - var found = suggestedCommands.find(function (element) { - return element.name == historyCommand; - }); - if (!found) { - suggestedCommands.push({ name: historyCommand }); - } -}); +let suggestedCommands = historyCommands.map((element) => ({ name: element })); var command = ""; process.stdin.setRawMode(true); @@ -71,14 +66,10 @@ process.stdin.on("keypress", (str, key) => { console.log(">>" + command); console.log(""); - var previewSuggestedCommands = []; - suggestedCommands.forEach(function (suggestedCommand) { - if (suggestedCommand.name.indexOf(command) >= 0) { - previewSuggestedCommands.push(suggestedCommand); - } - }); + const previewSuggestedCommands = suggestedCommands + .filter((element) => element.name.indexOf(command) >= 0) + .slice(0, 10); - previewSuggestedCommands = previewSuggestedCommands.slice(0, 10); previewSuggestedCommands.forEach(function (suggestedCommand) { log( chalk.blue(suggestedCommand.name) +