Skip to content

feat(completion): use cobras default completion command#204

Merged
kangasta merged 5 commits intomainfrom
feat/completion-refactor
Feb 6, 2023
Merged

feat(completion): use cobras default completion command#204
kangasta merged 5 commits intomainfrom
feat/completion-refactor

Conversation

@kangasta
Copy link
Copy Markdown
Member

@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
Copy Markdown
Member 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member 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
Copy Markdown
Member 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.

2 participants