Skip to content

Commit

Permalink
add completer options, decouple completed process from completer process
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Oct 21, 2011
1 parent 33c272b commit c864c9d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ var fs = require('fs'),
//
// Simply log in the callback to show completion output.

exports.complete = function(name, cb) {
exports.complete = function(name, completer, cb) {

cb = cb || function(){};
// cb not there, assume callback is completer and
// the completer is the executable itself
if(!cb) {
cb = completer;
completer = name;
}

// if not a complete command, return here.
if(!complete) return cb();

// if the COMP_* are not in the env, then dump the install script.
if(!words || !point || !line) return script(name, cb);
if(!words || !point || !line) return script(name, completer, cb);

var partial = line.substr(0, point),
last = line.split(' ').slice(-1).join(''),
Expand Down Expand Up @@ -50,12 +55,14 @@ exports.isComplete = function() {
// output the completion.sh script to the console for install instructions.
// This is actually a 'template' where the package name is used to setup
// the completion on the right command, and properly name the bash/zsh functions.
function script (name, cb) {
function script (name, completer, cb) {
var p = path.resolve(__dirname, 'completion/completion.sh');

fs.readFile(p, 'utf8', function (er, d) {
if (er) return cb(er);
d = d.replace(/\{pkgname\}/g, name);
d = d
.replace(/\{pkgname\}/g, name)
.replace(/{completer}/g, completer);
console.log(d);
cb();
});
Expand Down
6 changes: 3 additions & 3 deletions lib/completion/completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# npm command completion script
#
# Installation: {pkgname} completion >> ~/.bashrc (or ~/.zshrc)
# Installation: {completer} completion >> ~/.bashrc (or ~/.zshrc)
#

COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
Expand All @@ -19,7 +19,7 @@ if complete &>/dev/null; then
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
{pkgname} completion -- "${COMP_WORDS[@]}" \
{completer} completion -- "${COMP_WORDS[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
Expand All @@ -36,7 +36,7 @@ elif compctl &>/dev/null; then
IFS=$'\n' reply=($(COMP_CWORD="$cword" \
COMP_LINE="$line" \
COMP_POINT="$point" \
{pkgname} completion -- "${words[@]}" \
{completer} completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"url": "git://github.com/mklabs/pkgrc.git"
},
"main": "./lib/completion",
"bin": "./bin/pkgrc",
"directories": {
"bin": "./bin"
},
"engines": {
"node": "> 0.4"
},
Expand Down

0 comments on commit c864c9d

Please sign in to comment.