Skip to content

Commit 1ef28cc

Browse files
committed
refactor(completion): remove custom whitespace handling
The latest cobra version handles bash completions also when value contains whitespace, so replacing whitespace with non-breaking spaces is not needed anymore.
1 parent 95082f1 commit 1ef28cc

4 files changed

Lines changed: 10 additions & 22 deletions

File tree

internal/completion/helpers.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
package completion
22

33
import (
4-
"regexp"
54
"strings"
65
)
76

8-
var oneOrMoreSpace = regexp.MustCompile(` +`)
9-
10-
// RemoveWordBreaks replaces all whitespaces in input strings with non-breaking spaces to prevent bash from splitting completion with whitespace into multiple completions.
11-
//
12-
// This hack allows us to use cobras built-in completion logic and can be removed once cobra supports whitespace in bash completions (See https://github.com/spf13/cobra/issues/1740).
13-
func RemoveWordBreaks(input string) string {
14-
return oneOrMoreSpace.ReplaceAllString(input, "\u00A0")
15-
}
16-
177
// 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
188
func MatchStringPrefix(vals []string, key string, caseSensitive bool) []string {
199
var r []string
@@ -22,7 +12,7 @@ func MatchStringPrefix(vals []string, key string, caseSensitive bool) []string {
2212
if (caseSensitive && strings.HasPrefix(v, key)) ||
2313
(!caseSensitive && strings.HasPrefix(strings.ToLower(v), strings.ToLower(key))) ||
2414
key == "" {
25-
r = append(r, RemoveWordBreaks(v))
15+
r = append(r, v)
2616
}
2717
}
2818
return r

internal/completion/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestMatchStringPrefix(t *testing.T) {
7777
vals: []string{"a a ", "a(0)", "aab", "a;<!`'", "bbb"},
7878
key: "a",
7979
caseSensitive: false,
80-
expected: []string{"a\u00A0a\u00A0", "a(0)", "aab", "a;<!`'"},
80+
expected: []string{"a a ", "a(0)", "aab", "a;<!`'"},
8181
},
8282
} {
8383
t.Run(test.name, func(t *testing.T) {

internal/core/core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestInputValidation(t *testing.T) {
108108
test.setupFixture()
109109
}
110110

111-
cmd.SetOutput(stdout) // prevent noisy prints
111+
cmd.SetOut(stdout) // prevent noisy prints
112112
cmd.SetArgs(test.args)
113113

114114
err := cmd.Execute()

internal/resolver/matchers.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@ package resolver
33
import (
44
"path/filepath"
55
"strings"
6-
7-
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
86
)
97

10-
// MatchStringWithWhitespace checks if arg that may include whitespace matches given value. This checks both quoted args and auto-completed args handled with completion.RemoveWordBreaks.
11-
func MatchArgWithWhitespace(arg, value string) MatchType {
12-
if completion.RemoveWordBreaks(value) == arg || value == arg {
8+
// MatchArgWithEqualFold checks if arg is a exact or case-insensitive match.
9+
func MatchArgWithEqualFold(arg, value string) MatchType {
10+
if value == arg {
1311
return MatchTypeExact
1412
}
15-
if strings.EqualFold(completion.RemoveWordBreaks(value), arg) || strings.EqualFold(value, arg) {
13+
if strings.EqualFold(value, arg) {
1614
return MatchTypeCaseInsensitive
1715
}
1816
return MatchTypeNone
1917
}
2018

21-
// MatchStringWithWhitespace checks if arg matches given value as a unix style glob pattern.
19+
// MatchArgWithGlobPattern checks if arg matches given value as a unix style glob pattern.
2220
func MatchArgWithGlobPattern(arg, value string) MatchType {
2321
if matched, _ := filepath.Match(arg, value); matched {
2422
return MatchTypeGlobPattern
2523
}
2624
return MatchTypeNone
2725
}
2826

29-
// MatchTitle checks if arg matches any of the given values by using MatchArgWithWhitespace and MatchArgWithGlobPattern matchers.
27+
// MatchTitle checks if arg matches any of the given values by using MatchArgWithEqualFold and MatchArgWithGlobPattern matchers.
3028
func MatchTitle(arg string, values ...string) MatchType {
3129
match := MatchTypeNone
3230
for _, value := range values {
33-
match = max(match, MatchArgWithWhitespace(arg, value))
31+
match = max(match, MatchArgWithEqualFold(arg, value))
3432
match = max(match, MatchArgWithGlobPattern(arg, value))
3533
}
3634
return match

0 commit comments

Comments
 (0)