Skip to content

Commit

Permalink
completion - install instruction and simple line parsing/callback api
Browse files Browse the repository at this point in the history
Run `<pkgname> completion` for install instruction.
Run `<pkgname> completion >> datauri completion >> ~/.bashrc` (or ~/.zshrc)

Simply log in the callback to show completion output.
  • Loading branch information
mklabs committed Oct 17, 2011
1 parent 94b103f commit ce1f1f3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
30 changes: 27 additions & 3 deletions bin/pkgrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
#!/usr/bin/env node
var completion = require('../lib/completion');

console.log('Executing pkgrc bin file');
// this file is mainly just an example of usage

var args = process.argv.slice(2);
console.log('Args are: ', args);
// pass the module name, will change to module
// reference + dir walk to find a package.json
completion.complete('pkgrc', function(err, o) {
if(err || !o) return;

// options:
// - line: the full line from compgen
// - partial: the partial line, part of the line where the cursor is
// - words: the word counter, a number mapping the number of words from compgen
// - point: cursor offset

//
// Setting completion output is simply a matter of writing to the console
//

console.log(o.line);
console.log(o.partial);
console.log(o.words);
console.log(o.point);
console.log(o.last);
console.log(o.lastPartial);

if(o.partial === 'hello') {
console.log('World');
}
});
61 changes: 61 additions & 0 deletions lib/completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

var fs = require('fs'),
path = require('path'),
args = process.argv.slice(2),
complete = args[0] === 'completion',
words = process.env.COMP_CWORD,
point = process.env.COMP_POINT,
line = process.env.COMP_LINE;

// check bin/pkgrc for usage example.
//
// Run `<pkgname> completion` for install instruction.
// Run `<pkgname> completion >> datauri completion >> ~/.bashrc` (or ~/.zshrc)
//
// Simply log in the callback to show completion output.

exports.complete = function(name, cb) {

cb = cb || function(){};

// 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);

var partial = line.substr(0, point),
last = line.split(' ').slice(-1).join(''),
lastPartial = partial.split(' ').slice(-1).join('');

cb(null, {
line: line,
words: words,
point: point,
partial: partial,
last: last,
lastPartial: lastPartial
});
};

// simple helper function to know if the script is run
// in the context of a completion command. Also mapping the
// special `<pkgname> completion` cmd.
exports.isComplete = function() {
return complete || (words && point && line);
};

// 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) {
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);
console.log(d);
cb();
});
}

22 changes: 11 additions & 11 deletions lib/completion/completion.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
#!/bin/bash
###-begin-pkgrc-completion-###

###-begin-{pkgname}-completion-###
### credits to npm, this file is coming directly from isaacs/npm repo
#
# Just testing for now. (trying to learn this cool stuff)
#
# npm command completion script
#
# Installation: pkgrc completion >> ~/.bashrc (or ~/.zshrc)
# Or, maybe: pkgrc completion > /usr/local/etc/bash_completion.d/npm
# Installation: {pkgname} completion >> ~/.bashrc (or ~/.zshrc)
#

COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
COMP_WORDBREAKS=${COMP_WORDBREAKS/@/}
export COMP_WORDBREAKS

if complete &>/dev/null; then
_npm_completion () {
_{pkgname}_completion () {
local si="$IFS"
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
pkgrc completion -- "${COMP_WORDS[@]}" \
{pkgname} completion -- "${COMP_WORDS[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
complete -F _npm_completion pkgrc
complete -F _{pkgname}_completion {pkgname}
elif compctl &>/dev/null; then
_npm_completion () {
_{pkgname}_completion () {
local cword line point words si
read -Ac words
read -cn cword
Expand All @@ -37,10 +36,11 @@ elif compctl &>/dev/null; then
IFS=$'\n' reply=($(COMP_CWORD="$cword" \
COMP_LINE="$line" \
COMP_POINT="$point" \
pkgrc completion -- "${words[@]}" \
{pkgname} completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
compctl -K _npm_completion pkgrc
compctl -K _{pkgname}_completion {pkgname}
fi
###-end-npm-completion-###
###-end-{pkgname}-completion-###

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "git",
"url": "git://github.com/mklabs/pkgrc.git"
},
"main": "./lib/completion",
"bin": "./bin/pkgrc",
"engines": {
"node": "> 0.4"
Expand Down

0 comments on commit ce1f1f3

Please sign in to comment.