hemanth/bower — pull request #1 by mklabs (2012-12-01 21:05:09+00:00)
Title: Completion

Should be good to review :)

I decided to fork from your fork, so that you can decide to merge and adjust before issuing a new PR on the original repo.

I've branched off from twitter/bower@9f3ee1d06949d70737e0a5786a80bcc9b58cc02, you should be able to update the diff by pushing upstream's master changes to your repo.

zsh support should be ok but I still need to test it with a zsh environment. @sindresorhus if you have some time, can you test it out?

Quick walkthrough:

```sh
$ bower help completion

Usage:

bower completion

Synopsis:

. <(bower completion)

Description:

Tab Completion for bower
```

`bower completion` is the bridge between bash / zsh completion and node land.

When you run `bower completion`, it will dump the bash script to add to your ~/.bashrc or ~/.zshrc:

```sh
# for bash
$ bower completion >> ~/.bashrc

# for zsh
$ bower completion >> ~/.zshrc
```

Then, when you tab complete after a command like `bower insta`, the bash script triggers a `bower completion -- bower insta`.

If you tab complete after a known command, then it will try to execute `bower.commands[command].completion()` if the handler exists, with an Hash of completion config (nb of words, the completed line, partials if the cursos is in the middle of a word, and so on) and a callback to execute with an array of results.

Ex, for the uninstall command:

```js
module.exports.completion = function (opts, cb) {
var word = opts.word;

// completing options?
if (opts.words[0] === 'uninstall' && word.charAt(0) === '-') {
return cb(null, Object.keys(optionTypes).map(function (option) {
return '--' + option;
}));
}

fs.readdir(config.directory, function (err, dirs) {
// ignore ENOENT, ./components not created yet
if (err && err.code === 'ENOENT') return cb(null, []);
cb(err, dirs);
});
};
```

This is really close to the completion system used in npm, everything in this pull request is based on Isaac's work in npm.

That's pretty much it. Let me know what you think.