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

feat(_filedir): add -f to manually suffix / to directory names #847

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

scop
Copy link
Owner

@scop scop commented Nov 21, 2022

This attempts to implement _filedir -C as discussed in #827.

@scop scop marked this pull request as draft November 21, 2022 22:08
bash_completion Outdated
# Ignored with `-d`.
# OPTIONS
# -C dir Change to dir before generating completions
# -d Complete only on directories
_filedir()
Copy link
Owner Author

@scop scop Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem in this approach is that the -o filenames at end of the function won't take effect with -C somewhere as we are no longer in the dir we changed to at the end. So e.g. appending slashes to dir names does not happen. Not sure what to do about that.

(Commenting here because it seems the UI here won't let me comment that far away from changed lines.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing the compopt -o filenames inside each compgen subshell doesn't seem to make any difference.

Copy link
Collaborator

@akinomyoga akinomyoga Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is a good point. But after another thinking of it, I guess this issue also existed in the other approach in #827, so I think this is not an issue specific to this approach.

If we are to solve this issue within this PR, maybe we need to manually suffix a slash in the shell code.

  • One way is to test the filename in a for loop and suffix / if it is a directory name.
  • Another way is to use compgen -S / -d for directory names, and get non-directory filenames using another way (find, etc.).
  • Or we may switch between the above two based on the number of files in the specified directory.

Maybe we don't have to too much care about it, but the opposite issue is that when a filename in the specified directory has the same name as a directory in the current directory, it would be unexpectedly suffixed by /.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the problem existed in the other implementation in #827, I don't know why I didn't notice that. In my later tests I also noticed that it ends up changing the dir to /usr/share/mime in various scenarios, something I didn't notice originally either for some reason.

I'm going to try out yet another different approach for #827 before returning to this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked some, tests pass but there are still issues:

$ xx() { _filedir -C foo; }
$ mkdir -p foo/bar foo/quux
$ complete -F xx x
$ x <TAB>
bar/   quux/   # <-- this is all good
$ x ba<TAB>
bar/   quux/   # <-- this is not 

The problem of filename elsewhere matching a dir in current dir you mentioned also persists.

@scop
Copy link
Owner Author

scop commented May 11, 2023

Obsoleted by #973

@scop scop closed this May 11, 2023
@scop scop deleted the feat/filedir-C branch May 11, 2023 19:39
@akinomyoga akinomyoga restored the feat/filedir-C branch September 24, 2023 09:14
@akinomyoga
Copy link
Collaborator

akinomyoga commented Sep 24, 2023

Let me reopen this. The problem that the suffix slash is not added to directory names for the paths generated in another directory is not solved.

Also, I'd like to use the feature to suffix slashes to directory names to use it in another PR. There are many cases where the suffix slash is not added because the generated word does not exactly match a directory name in the current directory. In addition to the cases we change the directory, this also happens when the generated words are prefixed by xxx,yyy,, @, etc.

@akinomyoga
Copy link
Collaborator

akinomyoga commented Sep 24, 2023

Rebased and added a fix. Now -C dir is not needed because it is supported by _comp_compgen, but I'd like to have another flag
-f to instruct _filedir that a slash needs to be manually suffixed to directory names.

@akinomyoga akinomyoga marked this pull request as ready for review September 24, 2023 10:28
@akinomyoga akinomyoga changed the title feat(_filedir): add -C, use it feat(_filedir): add -f to manually suffix / to directory names Sep 24, 2023
scop and others added 3 commits August 23, 2024 18:35
If we are completing entries somewhere else besides the current dir,
`compopt -o filenames` won't do its usual thing; append slashes
ourselves.

Co-authored-by: Koichi Murase <[email protected]>
This fixes test_15d in test/t/unit/test_unit_filedir.py.
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

Successfully merging this pull request may close these issues.

2 participants