iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
363 stars 35 forks source link

Support compopt in completion functions #78

Open NiklasReisser opened 2 years ago

NiklasReisser commented 2 years ago

Fixes #77

sourcery-ai[bot] commented 2 years ago

Sourcery Code Quality Report

Merging this PR leaves code quality unchanged.

Quality metrics Before After Change
Complexity 46.94 ⛔ 46.94 ⛔ 0.00
Method Length 135.50 😞 135.50 😞 0.00
Working memory 1.75 ⭐ 1.75 ⭐ 0.00
Quality 25.94% 😞 25.94% 😞 0.00%
Other metrics Before After Change
Lines 761 767 6
Changed files Quality Before Quality After Quality Change
shtab/init.py 25.94% 😞 25.94% 😞 0.00%

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
shtab/init.py get_bash_commands 78 ⛔ 586 ⛔ 1.23% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
shtab/init.py complete_zsh 62 ⛔ 893 ⛔ 2.38% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
shtab/init.py complete_tcsh 53 ⛔ 467 ⛔ 5.02% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
shtab/init.py complete_bash 2 ⭐ 105 🙂 11 😞 64.43% 🙂 Extract out complex expressions
shtab/init.py add_argument_to 4 ⭐ 88 🙂 11 😞 64.81% 🙂 Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

casperdcl commented 2 years ago

Thanks for this PR @NiklasReisser! Just merged #81, is this still required?

NiklasReisser commented 2 years ago

Hi @casperdcl ,

I am on holiday without my laptop from work, so I can't test it. But for my use case, I'd say yes, this is still required.

Here is what I want to do: I have a bunch of virtual resource paths which look like filesystem paths, but actually aren't.

For example host/class/subclass/name.

Now I want to complete that peacemeal just like a filesystem path. To make this work I need compopt -o nospace, otherwise readline adds a space after every part of the path.

I'd also be fine with completing the whole path at once, like any other comletion, but in this case the issue is that shtab enables -o filenames globally for all completions. This causes readline to only print the last bit of the path for the completion. So if I type hos<tab>, and return the full path from my custom completion function, I get name (and only name) as possible completion.

Best regards,

Niklas

NiklasReisser commented 2 years ago

@casperdcl Can confirm now, I'd still need this for my usecase.

Cheers Niklas

NiklasReisser commented 2 years ago

@casperdcl This issue seems to also prevent usage of bash completion builtins like _available_interfaces, which modify COMPREPLY directly.

casperdcl commented 2 years ago

There's some conflicts now though with the main branch.

Also, would making this line configurable be a simpler alternative? https://github.com/iterative/shtab/blob/60a8525707f10859513c4362ea9c62cd40db93b8/shtab/__init__.py#L433

NiklasReisser commented 2 years ago

Hi,

I've resolved the merge conflict.

Unfortunately, I think making the main "complete" configurable would only allow a global setting for all completions for a given program. My approach is more generic.

I admit I don't particularily like it due to the tmpfile. But it is the only approach I can think of without changing the API. (With a breaking API-Change, the inner, user-defined function could set or extend COMPREPLY, instead of of printing to stdout.)

omesser commented 1 year ago

@0x2b3bfa0 - PTAL - worth resolving conflicts and merging this?

0x2b3bfa0 commented 1 year ago

@omesser, this pull request uses mktemp in a hardly avoidable but barely convenient way; being unable to find solutions that don't involve creting a subshell, I still hesitate to merge it.