Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use this module #33

Closed
rotimi-best opened this issue Nov 19, 2018 · 4 comments
Closed

How to use this module #33

rotimi-best opened this issue Nov 19, 2018 · 4 comments

Comments

@rotimi-best
Copy link

rotimi-best commented Nov 19, 2018

I don't understand where the code from Get started should be placed and how do I get the auto completion functional in my terminal. It isn't clear enough how to get started using this module practically.

Thanks.

@f
Copy link
Owner

f commented Nov 19, 2018

0. Create a folder called omelette-test

$ cd omelette-test

1. Create a file called index.js in it, and edit that file.

$ touch index.js
$ vim index.js

2. Put the following code into index.js.

console.log('Hello i am the program');

3. Run it to see it's working:

$ node index.js
hello i am the program

4. Install omelette:

npm i omelette

5. Require it in your program:

var omelette = require('omelette');

console.log('Hello i am the program');

6. Initialize omelette:

var omelette = require('omelette');

// initialize
omelette('hello <name> <surname>').init();

console.log('Hello i am the program');

7. See if it works:

$ node index.js --completion

This should generate an output like this:

### hello completion - begin. generated by omelette.js ###
if type compdef &>/dev/null; then
  _hello_completion() {
    compadd -- `hello --compzsh --compgen "${CURRENT}" "${words[CURRENT-1]}" "${BUFFER}"`
  }
  compdef _hello_completion hello
elif type complete &>/dev/null; then
  _hello_completion() {
    local cur prev nb_colon
    _get_comp_words_by_ref -n : cur prev
    nb_colon=$(grep -o ":" <<< "$COMP_LINE" | wc -l)

    COMPREPLY=( $(compgen -W '$(hello --compbash --compgen "$((COMP_CWORD - (nb_colon * 2)))" "$prev" "${COMP_LINE}")' -- "$cur") )

    __ltrim_colon_completions "$cur"
  }
  complete -F _hello_completion hello
fi
### hello completion - end ###

8. Alias the command:

But as you can see, this bash script tries to call an executable called hello, so we need to alias node index.js to be called by hello command. So let's alias first:

alias hello="node index.js"

Let's test if it works:

$ hello
Hello i am the program

9. Run the completion bash script that generated by Omelette (follow https://github.com/f/omelette#manual-installation):

(I'm using Fish shell so I'm running:)

node index.js --completion-fish | source

10. Edit index.js to complete the completion logic:

var omelette = require('omelette');
var completion = omelette('hello <name> <surname>')

// This is your logic
completion.on('name', ({reply}) => { reply(['fatih', 'rotimi']) });
completion.on('surname', ({reply}) => { reply(['akin', 'best']) });

completion.init();

console.log('Hello i am the program');

11. See if it works:

$ hello <press tab>
fatih rotimi
$ hello fatih <press tab>
akin best
$ hello fatih akin
Hello i am the program

That's all.

@rotimi-best
Copy link
Author

rotimi-best commented Nov 23, 2018

Thanks man for the explanation. It seems too much of a process for my use case. I just need a simple module that can help me make when I want to run npm run <suggest_script_to_me>. The module should show me the list of my scripts and help with auto completing to run the right script. Can this module help me do this?

@rotimi-best
Copy link
Author

I am closing this issue because I discovered now that I just need to type npm run <press_tab>, this would give me the list of all my scripts then when I begin typing and press tab it helps me with auto completion.

Thanks for the detailed answer BTW.

@fvictorio
Copy link

alias hello="node index.js"

Just a tip in case anyone runs into this: the alias + completion doesn't seem to quite work in zsh, at least for me. I think it might be more robust to use a function: hello() { node index.js $@ }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants