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

Allow case-insensitive filename globbing; add nocaseglob #1073

Merged
merged 1 commit into from
Apr 27, 2024

Conversation

theclapp
Copy link
Sponsor Collaborator

@theclapp theclapp commented Apr 16, 2024

  • Add a FoldCase flag to the expand.Config struct, which is used to configure how expand.Fields behaves.
  • Similarly, add a FoldCase Mode to pattern.Regexp. Setting the flag adds (?i) to the front of the generated regular expression.
  • Use the former to set the latter when calling expand.Fields.
  • Use all of the above to implement the "nocaseglob" shopt.

Rationale: I use expand.Fields to do filename expansion, for which I'd like to do case-insensitive matching. E.g. I'd like "c*" to match both "cmd" and "CHANGELOG.md"

@mvdan
Copy link
Owner

mvdan commented Apr 16, 2024

Rationale: I use expand.Fields to do filename expansion, for which I'd like to do case-insensitive matching. E.g. I'd like "c*" to match both "cmd" and "CHANGELOG.md"

Can you give some context in terms of when is this useful for a shell? I see nocaseglob in man bash, which alone is probably enough to need this Go API, but I wonder if your need comes from elsewhere.

@theclapp
Copy link
Sponsor Collaborator Author

I have an terminal/shell app that uses this package as its shell interpreter. It also, as mentioned in this PR, uses expand.Fields to do filename expansion, like bash does when you press tab. So I'd like c to expand to both "cmd" and "CHANGELOG.md". My app translates "c" to "c*" and calls expand.Fields.

This is similar to if you set shopt -s nocaseglob in bash and type "c*".

Would you like me to add the "nocaseglob" shopt and make it set this flag?

@mvdan
Copy link
Owner

mvdan commented Apr 16, 2024

OK gotcha, makes sense. If you want to wire it up to the interpreter that would be nice as a way to make this change a bit more well rounded and get the easy win, but it's not a must. I'll review later.

- Add a FoldCase flag to the expand.Config struct, which is used to
  configure how expand.Fields behaves.
- Similarly, add a FoldCase Mode to pattern.Regexp. Setting the flag
  adds (?i) to the front of the generated regular expression.
- Use the former to set the latter when calling expand.Fields.
- Use all of the above to implement the "nocaseglob" shopt.

Rationale: I use expand.Fields to do filename expansion, for which I'd
like to do case-insensitive matching. E.g. I'd like "c*" to match both
"cmd" and "CHANGELOG.md"
@theclapp theclapp changed the title Allow case-insensitive filename globbing Allow case-insensitive filename globbing; add nocaseglob Apr 16, 2024
@theclapp
Copy link
Sponsor Collaborator Author

I added the nocaseglob shopt, force-pushed, and updated the title and description of the PR.

Copy link
Owner

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

Thank you! Appreciate the added comments too.

@mvdan mvdan merged commit 9b91d69 into mvdan:master Apr 27, 2024
8 checks passed
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.

None yet

2 participants