Skip to content

[Bash completion]: Respects .file() extensions, uses _filedir #590

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

Merged

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented Oct 29, 2023

This PR implements two TODOs left over in the Bash completion script generator code:

  1. CompletionKind.file(extensions:) did not actually honor the extensions array. It now does so.
  2. CompletionKind.file(extensions:) and .directory now both preferentially invoke the _filedir shell function from bash-completion (v1 for Bash 3.2 & v2 for Bash 4.2+), if it is available in the shell environment at the time, falling back on compgen if it is not.

Note: I could not find any open issues referring to these concerns.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

…ionKind.file()` and uses `_filedir` when available for `.file` and `.directory`.
@rauhul
Copy link
Contributor

rauhul commented Oct 29, 2023

@swift-ci please test

Copy link
Contributor

@rauhul rauhul left a comment

Choose a reason for hiding this comment

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

I know I don't have enough experience with bash to say if these changes are the most correct implementation. the general shape looks good to me. If possible could you link to clap's or another language's arg parser script generation that does similar to compare?

@gwynne
Copy link
Contributor Author

gwynne commented Oct 29, 2023

@rauhul I have to be honest; this is my first time tinkering with Bash completion 😅. Here are the references I used in coming up with this:


return """
if declare -F _filedir >/dev/null; then
\(safeExts.map { "_filedir '\($0)'" }.joined(separator:"\n "))
Copy link
Member

Choose a reason for hiding this comment

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

I think we need an additional call to _filedir -d here – when I run completions on an extension-constrained .file item, it should show me the matching files plus the available directories, since I might need to navigate before finding the correct file.

Copy link
Member

@natecook1000 natecook1000 Nov 3, 2023

Choose a reason for hiding this comment

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

bash-3.2$ math stats quantiles --file 
CHANGELOG.md        CMakeLists.txt      CODE_OF_CONDUCT.md  CONTRIBUTING.md     LICENSE.txt         README.md           
bash-3.2$ math stats quantiles --file Tes
<no output>
bash-3.2$ math stats quantiles --file Tests/
<auto-completes to:>
bash-3.2$ math stats quantiles --file Tests/CMakeLists.txt

Here, Tests and the other directories should be visible and autocomplete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, done and done! 👍

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Thanks so much for this fix, @gwynne! One note about the files with extensions case, otherwise this looks great. 👏🏻👏🏻👏🏻👏🏻

@rauhul
Copy link
Contributor

rauhul commented Nov 12, 2023

@swift-ci please test

@rauhul
Copy link
Contributor

rauhul commented Nov 12, 2023

@gwynne it looks like some tests need to be updated:

             if declare -F _filedir >/dev/null; then
               _filedir 'txt'
               _filedir 'md'
               _filedir 'TXT'
               _filedir 'MD'
+              _filedir -d
             else

@gwynne
Copy link
Contributor Author

gwynne commented Nov 12, 2023

@gwynne it looks like some tests need to be updated:

Whoops, sorry! Incoming momentarily 😅

@rauhul
Copy link
Contributor

rauhul commented Nov 13, 2023

@swift-ci please test

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Thanks again, @gwynne! 👏🏻👏🏻

@natecook1000 natecook1000 merged commit ecac862 into apple:main Nov 14, 2023
gwynne added a commit to vapor/console-kit that referenced this pull request Nov 14, 2023
…sons learned from apple/swift-argument-parser#590, pretty much just for the hell of it. Deprecate `ConsoleErrror`.
@gwynne gwynne deleted the gwynne/add-bash-completion-_filedir-support branch November 14, 2023 16:56
gwynne added a commit to vapor/console-kit that referenced this pull request Dec 1, 2023
…sons learned from apple/swift-argument-parser#590, pretty much just for the hell of it. Deprecate `ConsoleErrror`.
gwynne added a commit to vapor/console-kit that referenced this pull request Dec 1, 2023
* Split ConsoleKit into two functional targets - one for terminal interaction, one for command handling, plus an umbrella for compatibility. Also update README, and docs images. Clean up several bits of code.
* Apply some of the lessons learned from apple/swift-argument-parser#590, pretty much just for the hell of it. Deprecate `ConsoleErrror`.
* Readme cleanup
* Make the levenshteinDistance method noticeably more efficient.

---------

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy