From 2b8f024b4ec16ba23daa1a52be52c6f6312a7a56 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 16 Mar 2011 13:24:54 -0700 Subject: completion for search --- lib/search.js | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'lib/search.js') diff --git a/lib/search.js b/lib/search.js index d9902732f..8fe3cb7ec 100644 --- a/lib/search.js +++ b/lib/search.js @@ -9,31 +9,25 @@ var npm = require("../npm") search.usage = "npm search [some search terms ...]" -search.completion = function (args, index, cb) { - var compl = [] - , getCompletions = require("./utils/completion/get-completions") - , name = (args.length + 1 === index) && args[args.length - 1] || "" - , priors = name ? args.slice(0, args.length - 1) : args +search.completion = function (opts, cb) { + var compl = {} + , partial = opts.partialWord + , ipartial = partial.toLowerCase() + , plen = partial.length + // get the batch of data that matches so far. // this is an example of using npm.commands.search programmatically // to fetch data that has been filtered by a set of arguments. - search(priors, true, 3600, function (er, data) { + search(opts.conf.argv.remain.slice(2), true, function (er, data) { if (er) return cb(er) Object.keys(data).forEach(function (name) { - compl.push.apply(compl, [name].concat(data[name].words)) - }) - var last = null - compl = compl.sort(strcmp).filter(function (c) { - if (args.indexOf(c) !== -1) return false - var r = c !== last - last = c - return r + data[name].words.split(" ").forEach(function (w) { + if (w.toLowerCase().indexOf(ipartial) === 0) { + compl[partial + w.substr(plen)] = true + } + }) }) - var matches = getCompletions(name || "", compl) - if (name && !name.match(/^=/)) { - matches = matches.concat(getCompletions("="+name, compl)) - } - cb(null, matches) + cb(null, Object.keys(compl)) }) } @@ -57,9 +51,8 @@ function search (args, silent, staleness, cb_) { // now data is the list of data that we want to show. // prettify and print it, and then provide the raw // data to the cb. - if (er) return cb_(er) + if (er || silent) return cb_(er, data) function cb (er) { return cb_(er, data) } - if (silent) return cb() output = output || require("./utils/output") output.write(prettify(data, args), cb) }) @@ -189,7 +182,7 @@ function addColorMarker (str, arg, i) { var m = i % cl + 1 , markStart = String.fromCharCode(m) , markEnd = String.fromCharCode(0) - , pieces = str.toLowerCase().split(arg) + , pieces = str.toLowerCase().split(arg.toLowerCase()) , p = 0 return pieces.map(function (piece, i) { piece = str.substr(p, piece.length) -- cgit v1.2.3