neperfepx / neper

Polycrystal generation and meshing
http://neper.info
GNU General Public License v3.0
205 stars 53 forks source link

Add bash autocompletion support #356

Closed rquey closed 2 years ago

rquey commented 2 years ago

Is your feature request related to a problem? Please describe.

Neper has several modules with many options each, which are not always easy to remember.

Describe the solution you'd like

Adding Bash autocompetion support to Neper would help to solve this problem.

What Bash (tab) autocompletion does is to

More specifically, for Neper:

There are many tutorials on the web on how bash autocompletion support can be added to a project.

RishiKumarRay commented 2 years ago

I would like to work on this, can you guide me

rquey commented 2 years ago

Thanks @RishiKumarRay! For what is Bash autocomplete, I do not know more that what I described above...

I would try to make it work step by step. First, make it so that neper <tab-key> shows all - options:

-M
-S
-T
-V
--help
--rcfile
--version

@xshang93 also declared to be interested.

RishiKumarRay commented 2 years ago

Ok let me know incase if he is not interested

xshang93 commented 2 years ago

Hi @RishiKumarRay , thank you for your interest in this. As @rquey said, I have been looking into this for a few days and I think I will come up with something soon. I could let you know later if there are any assist needed but at the moment it seems to be fine.

Cheer

xshang93 commented 2 years ago

Hey @rquey , I have made some progress on the autocompletion support and wanted to hear about your opinion before I move further.

What it does now:

  1. Everything you described in the original post, except for completing filenames and directories.
  2. For each module, as soon as one option is already used in the current line, it won't show up again in the completion list. I added this because each option is supposed to appear only once in one command:
    $ neper -T -morphooptilog
    -morphooptilogdis   -morphooptilogtesr  -morphooptilogtime  -morphooptilogval   -morphooptilogvar   
    $ neper -T -morphooptilogdis -morphooptilog
    -morphooptilogtesr  -morphooptilogtime  -morphooptilogval   -morphooptilogvar  

What still needs to be done:

  1. As mentioned above, the support for completing file names and directories for -M, -S, and -V. This should be rather easy to add.
  2. To make the auto completion work, one needs first to add to the .bash_profile or .bashrc the path to the completion script. I'm not sure how this can be integrated into the neper compilation/installation process. It would be nice if you could show me how to achieve this.

The current version is attached below.

neper-completion.zip

rquey commented 2 years ago

Hey @rquey , I have made some progress on the autocompletion support and wanted to hear about your opinion before I move further.

What it does now:

1. Everything you described in the original post, except for completing filenames and directories.

Nice!

2. For each module, as soon as one option is already used in the current line, it won't show up again in the completion list. I added this because each option is supposed to appear only once in one command:
$ neper -T -morphooptilog
-morphooptilogdis   -morphooptilogtesr  -morphooptilogtime  -morphooptilogval   -morphooptilogvar   
$ neper -T -morphooptilogdis -morphooptilog
-morphooptilogtesr  -morphooptilogtime  -morphooptilogval   -morphooptilogvar  

Regarding this, it is true that using the same option several times (in -T, -M and -S) overwrites the option argument, but Neper does allow this (it can be useful sometimes). So, I think that the autocompletion script should also allow this. Can you please may it so?

What still needs to be done:

1. As mentioned above, the support for completing file names and directories for -M, -S, and -V. This should be rather easy to add.

Great.

2. To make the auto completion work, one needs first to add to the .bash_profile or .bashrc the path to the completion script. I'm not sure how this can be integrated into the neper compilation/installation process. It would be nice if you could show me how to achieve this.

As far as I understand (https://unix.stackexchange.com/questions/194428/install-an-additional-completion-function-for-bash), the script can be added to .bash_profile or .bashrc, but this would only make it available to the user. For a system-wide installation (which is what we want, and is what make install does, for Neper), the script can be copied to /usr/share/bash-completion/. This is what make install should do. It can be figured out once the script is complete.

The current version is attached below.

neper-completion.zip

We are planning for a new release late next week. If ready, autocompletion support could be added.

rquey commented 2 years ago

@xshang93 - After more thoughts, regarding files and directories and at least in first approach, autocompletion could show/autocomplete all files and directories, not only valid inputs.

xshang93 commented 2 years ago

Regarding this, it is true that using the same option several times (in -T, -M and -S) overwrites the option argument, but Neper does allow this (it can be useful sometimes). So, I think that the autocompletion script should also allow this. Can you please may it so?

Just wanted to clarify: Do you mean, for example, if I do neper -T -n 10 -n 20, then 10 will be overwritten by 20? This would result in the 'bad arguments!' error. Or, do you mean that the autocompletion should allow neper -T -M in one command?

As far as I understand (https://unix.stackexchange.com/questions/194428/install-an-additional-completion-function-for-bash), the script can be added to .bash_profile or .bashrc, but this would only make it available to the user. For a system-wide installation (which is what we want, and is what make install does, for Neper), the script can be copied to /usr/share/bash-completion/. This is what make install should do. It can be figured out once the script is complete.

Yes, I think you are right, provided that one has already installed a working version of bash-completion. Also for mac users the bash completion files are located in a different folder: https://sourabhbajaj.com/mac-setup/BashCompletion/

We are planning for a new release late next week. If ready, autocompletion support could be added.

Awesome! I will try to work out a working version by then.

rquey commented 2 years ago

Regarding this, it is true that using the same option several times (in -T, -M and -S) overwrites the option argument, but Neper does allow this (it can be useful sometimes). So, I think that the autocompletion script should also allow this. Can you please may it so?

Just wanted to clarify: Do you mean, for example, if I do neper -T -n 10 -n 20, then 10 will be overwritten by 20? This would result in the 'bad arguments!' error. Or, do you mean that the autocompletion should allow neper -T -M in one command?

Yes, this is what I mean. It does fail for -n but would not for other options like -reg, -id, ...

As far as I understand (https://unix.stackexchange.com/questions/194428/install-an-additional-completion-function-for-bash), the script can be added to .bash_profile or .bashrc, but this would only make it available to the user. For a system-wide installation (which is what we want, and is what make install does, for Neper), the script can be copied to /usr/share/bash-completion/. This is what make install should do. It can be figured out once the script is complete.

Yes, I think you are right, provided that one has already installed a working version of bash-completion. Also for mac users the bash completion files are located in a different folder: https://sourabhbajaj.com/mac-setup/BashCompletion/

We are planning for a new release late next week. If ready, autocompletion support could be added.

Awesome! I will try to work out a working version by then.

xshang93 commented 2 years ago

Regarding this, it is true that using the same option several times (in -T, -M and -S) overwrites the option argument, but Neper does allow this (it can be useful sometimes). So, I think that the autocompletion script should also allow this. Can you please may it so?

Just wanted to clarify: Do you mean, for example, if I do neper -T -n 10 -n 20, then 10 will be overwritten by 20? This would result in the 'bad arguments!' error. Or, do you mean that the autocompletion should allow neper -T -M in one command?

Yes, this is what I mean. It does fail for -n but would not for other options like -reg, -id, ...

Ok I'll see what I can do. Removing that would be very easy - I couldn't picture a scenario where overwriting a previous option would be useful though, can you give an example? Maybe if I can think of a way to preserve both

rquey commented 2 years ago

A bad example is when you have a long command line and you are too lazy to to go way back to change an option - you can just retype... A good example is to change the value of an option that was set in the configuration file (internally, Neper reads the options from the configuration file and the command line successively, and in the same way). I agree that the option would still appear only once at the command line, but this is a good reason why Neper accepts to overwrite an option argument.

Anyway, it is better is bash autocompletion follows Neper's current behavior (and if this behavior needs to be changed, it can be discussed elsewhere). If you feel that the "selective autocompletion" can be useful at some point and it can be commented out in the script, it's even better.

xshang93 commented 2 years ago

A bad example is when you have a long command line and you are too lazy to to go way back to change an option - you can just retype... A good example is to change the value of an option that was set in the configuration file (internally, Neper reads the options from the configuration file and the command line successively, and in the same way). I agree that the option would still appear only once at the command line, but this is a good reason why Neper accepts to overwrite an option argument.

Ok this makes sense.

Anyway, it is better is bash autocompletion follows Neper's current behavior (and if this behavior needs to be changed, it can be discussed elsewhere). If you feel that the "selective autocompletion" can be useful at some point and it can be commented out in the script, it's even better.

Sure, that's exactly what I plan to do. I do realized some other issue(s) with my current code (such as completion only works when you are typing in an new option but not when you are editing an option), I will work on that over the weekend.

xshang93 commented 2 years ago

So the new version is ready. It should have all the functionalities described above. I have it uploaded to github so you can check it out here https://github.com/xshang93/neper_bash_comp

rquey commented 2 years ago

I see one problem, which is that, when one hits <tab> after an option, it starts to autocomplete to another option:

neper -T -loadtess <tab>

gives

neper -T -loadtess -

Instead, it should show all files and directories. This is true for all options.

xshang93 commented 2 years ago

I see one problem, which is that, when one hits <tab> after an option, it starts to autocomplete to another option:

neper -T -loadtess <tab>

gives

neper -T -loadtess -

Instead, it should show all files and directories. This is true for all options.

The new version should have fixed this. Just one thing I'm confused about: The -loadmesh option in -M, what exactly does it do? The documentations says it's for loading a mesh as output but I'm not sure if I understand what that means.

rquey commented 2 years ago

Thanks. It works for -loadtess and a few others, but all options should behave in this way. An option is always followed by one and only one argument. So, when one hits <tab> right after an option, it should not start to autocomplete to another option.

For example:

neper -T -loadtess file.tess -reg <tab>

autocompetes to

neper -T -loadtess file.tess -reg -

while it should show all files and directories.

EDIT I agree that hitting <tab> in this context does not make much sense, but it is confusing to me that it autocompletes to a new option, which we know is wrong.

-loadmesh can be used to load a mesh, which is then considered as the output mesh. Suppose you would like to compute the element volumes of an existing mesh, then:

neper -M -loadmesh my_mesh.msh -statelt vol
xshang93 commented 2 years ago

Thanks. It works for -loadtess and a few others, but all options should behave in this way. An option is always followed by one and only one argument. So, when one hits <tab> right after an option, it should not start to autocomplete to another option.

For example:

neper -T -loadtess file.tess -reg <tab>

autocompetes to

neper -T -loadtess file.tess -reg -

while it should show all files and directories.

EDIT I agree that hitting <tab> in this context does not make much sense, but it is confusing to me that it autocompletes to a new option, which we know is wrong.

I agree that this could be confusing. Let me revise it so that no two options can appear side by side.

-loadmesh can be used to load a mesh, which is then considered as the output mesh. Suppose you would like to compute the element volumes of an existing mesh, then:

neper -M -loadmesh my_mesh.msh -statelt vol

This makes sense! Thanks for the explaination.

xshang93 commented 2 years ago

@rquey FYI, the new version addressing the above issue is online now.

rquey commented 2 years ago

Okay. So, to have this included in the next official release, please:

I will modify the CMakeLists.txt so that it copies the competion script to a system location on make install, as discussed before.

I will add your name to the list of code contributors, which will appear in the code and / or on the website.

Thanks!

xshang93 commented 2 years ago

Okay. So, to have this included in the next official release, please:

  • write your name, as author, in the completion file
  • put the code in its own folder in src/contrib
  • make a pull request to main

I will modify the CMakeLists.txt so that it copies the competion script to a system location on make install, as discussed before.

I will add your name to the list of code contributors, which will appear in the code and / or on the website.

Thanks!

Just did it. Absolutely my great pleasure to help!