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

Omit trailing space from completion #87

Open
AdamSLevy opened this issue Apr 24, 2019 · 5 comments
Open

Omit trailing space from completion #87

AdamSLevy opened this issue Apr 24, 2019 · 5 comments

Comments

@AdamSLevy
Copy link
Contributor

When writing completion function in bash you can use compopt -o nospace to omit the trailing space from completed arguments.

How could something like this be achieved using this library? Calling out to compopt from the predict function doesn't work.

@AdamSLevy
Copy link
Contributor Author

From my reading really the only way to achieve this is to use the -o nospace option to the complete command on install, and then append a space to all completion options if it is needed.

The only place where this is needed is really for flags and arguments prediction functions. Sub commands pretty much always need a space so that isn't hard to just add. The downside to this is that existing prediction functions now need to append a space to their predictions. Trivial enough to add but annoying to need to know to add for existing functions. This could be added as a hook that appends a space to the return of all predictions but it would need a corresponding option for disabling it. The Flags map doesn't offer a clear way to add such an option at the individual flagname/predictor level. Perhaps a blacklist of flags that should not get a space appended.

All of that gets rather convoluted unfortunately for a minimal feature.

@posener
Copy link
Owner

posener commented Apr 26, 2019

I'm not sure what exactly is the problem that you are trying to solve. I am not sure how compopt relates to this library.

@AdamSLevy
Copy link
Contributor Author

AdamSLevy commented Apr 27, 2019

Most flags are like this:

--flag arg

Once arg is predicted you want to make a new flag prediction and so a space is inserted. This space is inserted by the bash completion system that invokes the completion commands.

Other times though, a flag might be like this:

--flag key=value

Where key= can be completed, but value may be a number or something that needn't be completed. In this case, once --flag key= is completed, it would be nice to avoid having that space be inserted, because the user has more to type. It's minor, deleting the space is rather trivial, but in a perfect world I would be able to specify when that space gets inserted.

Unfortunately the only way to control whether bash completion inserts a space, is via the options assigned to the completion. I believe a bash completion script can modify its own options, or at least the individual bash functions that would generate the completion may have separate options assigned to them. But since completion via this library is not that granular from the perspective of bash completion, it is difficult to modify the completion options.

If you modify the complete command to this:

complete -o nospace -C /path/to/cmd cmd

Then completions will occur without that space, unless the completion suggest itself has a trailing space.

Does that help clarify both the problem and the potential implementation I was suggesting?

I know its a very small detail and not really a big deal. I would understand if you close this as won't fix but I at least wanted to raise the use case on the off chance that someone sees it as desirable or sees a better way to implement the option.

@posener
Copy link
Owner

posener commented Apr 27, 2019

I see.
I think it is possible to add the nospace option by default, manually add space to completed terms, and enable an option for specific terms not to add a space. However, as you said, I agree that the use case is pretty rare, and might complicate the code.

@AdamSLevy
Copy link
Contributor Author

AdamSLevy commented Mar 1, 2020

I never followed up on this, but long ago I figured out an elegant work around from this SO answer: https://stackoverflow.com/questions/55842705/is-it-possible-to-modify-compopts-dynamically-from-an-external-completion-comman

To solve the problem you encountered, I output a dummy answer with an extra space. Since there are more than one answer, bash no longer automatically adds a space. So instead of just returning (in JSON notation):

["-o"]

you return:

["-o","-o "]

I also use this trick when doing path completion. To allow user completing a path by "drilling down", when there is a single directory match I output:

["dirname/","dirname/ "]

so the user can Tab again to drill down inside path instead of getting a space after "dirname/ " and having to backspace and Tab again.

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

2 participants