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(completion): use cobras default completion command #204

Merged
merged 5 commits into from Feb 6, 2023

Conversation

kangasta
Copy link
Contributor

@kangasta kangasta commented Feb 1, 2023

This requires removing whitespace from completions as cobras bash completions do not support values with whitespace (See spf13/cobra#1740). This is done by replacing whitespace with non-breaking spaces during completion generation. The argument resolvers with values that can include whitespace are also updated to handle both modified and original arguments, i.e. user can still input whitespaces normally when argument is quoted or escaped.

@kangasta kangasta marked this pull request as ready for review February 1, 2023 14:32
@kangasta
Copy link
Contributor Author

kangasta commented Feb 1, 2023

I know this is quite hacky, but so is our current bash completion override which prevents us from supporting zsh (#134) and other shell completions.

Copy link
Contributor

@peknur peknur left a comment

Choose a reason for hiding this comment

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

Looks good. Left non-blocking comment about RE.

Comment on lines 11 to 25
func RemoveWordBreaks(input string) string {
re := regexp.MustCompile(`\s+`)
return re.ReplaceAllString(input, "\u00A0")
}

// MatchStringPrefix returns a list of string in vals which have a prefix as specified in key. Quotes are removed from key and output strings are escaped according to completion rules
func MatchStringPrefix(vals []string, key string, caseSensitive bool) []string {
var r []string
key = strings.Trim(key, "'\"")

for _, v := range vals {
if (caseSensitive && strings.HasPrefix(v, key)) ||
(!caseSensitive && strings.HasPrefix(strings.ToLower(v), strings.ToLower(key))) ||
key == "" {
r = append(r, Escape(v))
r = append(r, RemoveWordBreaks(v))
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe compile that regular expression before loop and just do re.ReplaceAllString in loop.

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 idea! Moved the re definition to package-level var.

@kangasta
Copy link
Contributor Author

kangasta commented Feb 3, 2023

The linting error seems to come from generics usage in node group parameters parsing, I'll try to see if there a route around that

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