Omelette

Omelette.js a simple autocompletion helper for node, published by Fatih.

It's a beautifuly crafted coffee code, that takes care of tab completions for your CLI tools.

It's takes care of --compgen, --completion and --compzsh for variations of SHELLs.

Say you need a simple CLI tool to greet user:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env node

var comp, omelette;

omelette = require("omelette");

comp = omelette("greeter <user>");

comp.on("user", function() {
  return this.reply(["hello", "cruel", "world"]);
});

comp.init();

Which on user completes "hello", "cruel", "world".

Say you saved the file as greeter all you have do to generator the completion is :

1
$ ./greeter --completion

That would result in the bleow for bash shell:

1
2
3
4
5
6
7
8
9
10
11
12
13
### greet completion - begin. generated by omelette ###
if type compdef &>/dev/null; then
  _greet_complette() {
    compadd -- `greet --compzsh --compgen "${CURRENT}" "${words[CURRENT-1]}" "${BUFFER}"`
  }
  compdef _greet_complette greet
elif type complete &>/dev/null; then
  _greet_complette() {
    COMPREPLY=( $(compgen -W '$(greet --compbash --compgen "${COMP_CWORD}" "${COMP_WORDS[COMP_CWORD-1]}" "${COMP_LINE}")' -- "${COMP_WORDS[COMP_CWORD]}") )
  }
  complete -F _greet_complette greet
fi
### greet completion - end ###

In zsh, you can write these:

echo '. <(./greeter --completion)' >> .zshrc

In bash, you should write:

1
2
./greeter --completion >> ~/greeter.completion.sh
echo 'source ~/greeter.completion.sh' >> .bash_profile

Now you must see tab completion for greeter!

You can also use:

1
2
3
4
5
6
7
8
9
10
11
12
// Listen all fragments by "complete" event

complete.on("complete", function(fragment, word, line) {
  return this.reply(["hello", "world"]);
});


// Listen events by its order.

complete.on("$1", function(word, line) {
  return this.reply(["hello", "world"]);
});

A wonderful GIF by the author:

omelette.gif

Enjoy your tab completions!

Suggest a module

Comments