From 0379f6bc35badfacf46d7f938c9621ec52995665 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 16 Oct 2018 08:01:55 -0400 Subject: [PATCH] use fuzzy to do search; but still do sorting --- lib/modules/console/suggestions.js | 48 +++++++++--------------------- lib/utils/utils.js | 8 ++++- package-lock.json | 5 ++++ package.json | 1 + 4 files changed, 27 insertions(+), 35 deletions(-) diff --git a/lib/modules/console/suggestions.js b/lib/modules/console/suggestions.js index 4af7b8ee..decc34c3 100644 --- a/lib/modules/console/suggestions.js +++ b/lib/modules/console/suggestions.js @@ -26,51 +26,31 @@ class Suggestions { getSuggestions(cmd) { cmd = cmd.toLowerCase() - if (cmd === 'web3' || cmd === 'web3.') { - return [ - {value: 'web3.eth', command_type: "web3 object", description: "module for interacting with the Ethereum network"}, - {value: 'web3.net', command_type: "web3 object", description: "module for interacting with network properties"}, - {value: 'web3.shh', command_type: "web3 object", description: "module for interacting with the whisper protocol"}, - {value: 'web3.bzz', command_type: "web3 object", description: "module for interacting with the swarm network"}, - {value: 'web3.eth.getAccounts()', command_type: "web3 object", description: "get list of accounts"} - ] - } + suggestions.push({value: 'web3.eth', command_type: "web3 object", description: "module for interacting with the Ethereum network"}) + suggestions.push({value: 'web3.net', command_type: "web3 object", description: "module for interacting with network properties"}) + suggestions.push({value: 'web3.shh', command_type: "web3 object", description: "module for interacting with the whisper protocol"}) + suggestions.push({value: 'web3.bzz', command_type: "web3 object", description: "module for interacting with the swarm network"}) + suggestions.push({value: 'web3.eth.getAccounts()', command_type: "web3 object", description: "get list of accounts"}) let suggestions = [] for (let contractName in this.contracts) { let contract = this.contracts[contractName] - if (contract.className.toLowerCase().indexOf(cmd) >= 0 || contract.deployedAddress.indexOf(cmd) >= 0 || cmd.indexOf('contract') >= 0) { - suggestions.push({value: contract.className, command_type: "web3 object", description: "contract deployed at " + contract.deployedAddress}); - } + suggestions.push({value: contract.className, command_type: "web3 object", description: "contract deployed at " + contract.deployedAddress}); - if ('profile'.indexOf(cmd) >= 0) { - suggestions.push({value: "profile " + contract.className, command_type: "embark command", description: "profile " + contract.className + " contract"}); - } + suggestions.push({value: "profile " + contract.className, command_type: "embark command", description: "profile " + contract.className + " contract"}); } // TODO: make this registered through an API instead - if ('help'.indexOf(cmd) >= 0) { - suggestions.push({value: 'help', command_type: "embark command", description: "displays quick list of some of the available embark commands"}); - } - if ('versions'.indexOf(cmd) >= 0) { - suggestions.push({value: 'versions', command_type: "embark command", description: "display versions in use for libraries and tools like web3 and solc"}); - } - if ('ipfs'.indexOf(cmd) >= 0) { - suggestions.push({value: 'ipfs', command_type: "javascript object", description: "instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"}); - } - if ('swarm'.indexOf(cmd) >= 0) { - suggestions.push({value: 'swarm', command_type: "javascript object", description: "instantiated swarm-api object configured to the current environment (available if swarm is enabled)"}); - } - if (cmd == 'w' || cmd === 'we' || cmd === 'web') { - suggestions.push({value: 'web3', command_type: "javascript object", description: "instantiated web3.js object configured to the current environment"}); - } - if ('embarkjs'.indexOf(cmd) >= 0) { - suggestions.push({value: 'EmbarkJS', command_type: "javascript object", description: "EmbarkJS static functions for Storage, Messages, Names, etc."}); - } + suggestions.push({value: 'help', command_type: "embark command", description: "displays quick list of some of the available embark commands"}); + suggestions.push({value: 'versions', command_type: "embark command", description: "display versions in use for libraries and tools like web3 and solc"}); + suggestions.push({value: 'ipfs', command_type: "javascript object", description: "instantiated js-ipfs object configured to the current environment (available if ipfs is enabled)"}); + suggestions.push({value: 'swarm', command_type: "javascript object", description: "instantiated swarm-api object configured to the current environment (available if swarm is enabled)"}); + suggestions.push({value: 'web3', command_type: "javascript object", description: "instantiated web3.js object configured to the current environment"}); + suggestions.push({value: 'EmbarkJS', command_type: "javascript object", description: "EmbarkJS static functions for Storage, Messages, Names, etc."}); // sort first the ones that match the command at the beginning of the string, then prefer smaller commands first - return suggestions.sort((x,y) => { + return utils.fuzzySearch(cmd, suggestions, (result) => { return result.value + " " + result.description }).map((x) => x.original).sort((x,y) => { let diff = x.value.indexOf(cmd) - y.value.indexOf(cmd) if (diff !== 0) return diff; return x.value.length - y.value.length; diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 56f7452d..b1496393 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -519,6 +519,11 @@ function copyToClipboard(text) { clipboardy.writeSync(text); } +function fuzzySearch(text, list, filter) { + const fuzzy = require('fuzzy'); + return fuzzy.filter(text, list, {extract: (filter || function () {})}) +} + module.exports = { joinPath, dirname, @@ -564,5 +569,6 @@ module.exports = { errorMessage, timer, fileTreeSort, - copyToClipboard + copyToClipboard, + fuzzySearch }; diff --git a/package-lock.json b/package-lock.json index 0bf0cecc..8e745299 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6610,6 +6610,11 @@ "resolved": "https://registry.npmjs.org/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz", "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=" }, + "fuzzy": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/fuzzy/-/fuzzy-0.1.3.tgz", + "integrity": "sha1-THbsL/CsGjap3M+aAN+GIweNTtg=" + }, "ganache-cli": { "version": "6.1.8", "resolved": "https://registry.npmjs.org/ganache-cli/-/ganache-cli-6.1.8.tgz", diff --git a/package.json b/package.json index f3ff2b9d..0afbfe94 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "flatted": "0.2.3", "follow-redirects": "1.5.7", "fs-extra": "2.1.2", + "fuzzy": "0.1.3", "ganache-cli": "6.1.8", "glob": "7.1.3", "globule": "1.2.1",