Skip to content

Commit 010d813

Browse files
committed
bash: handle COMP_TYPE
1 parent 38c4ae6 commit 010d813

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

example/cmd/_test/bash-ble.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22
_example_completion() {
3-
export COMP_WORDBREAKS
43
export COMP_LINE
54
export COMP_POINT
5+
export COMP_TYPE
6+
export COMP_WORDBREAKS
67

78
local nospace data compline="${COMP_LINE:0:${COMP_POINT}}"
89

example/cmd/_test/bash.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/bin/bash
22
_example_completion() {
3-
export COMP_WORDBREAKS
43
export COMP_LINE
54
export COMP_POINT
5+
export COMP_TYPE
6+
export COMP_WORDBREAKS
67

78
local nospace data compline="${COMP_LINE:0:${COMP_POINT}}"
89

internal/shell/bash/action.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
7474
meta.Nospace.Add('*')
7575
}
7676

77-
nospace := true
77+
nospace := false
7878
vals := make([]string, len(values))
7979
for index, val := range values {
80-
if len(values) == 1 {
81-
if !meta.Nospace.Matches(val.Value) {
82-
nospace = false
83-
}
80+
if len(values) == 1 || compType != COMP_TYPE_LIST_SUCCESSIVE_TABS {
81+
nospace = nospace || meta.Nospace.Matches(val.Value)
8482

8583
vals[index] = sanitizer.Replace(val.Value)
8684
if requiresQuoting(vals[index]) {
@@ -98,6 +96,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
9896
}
9997
}
10098
} else {
99+
nospace = true
101100
val.Display = displayReplacer.Replace(val.Display)
102101
val.Description = displayReplacer.Replace(val.Description)
103102
if val.Description != "" {

internal/shell/bash/patch.go

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ func (r RedirectError) Error() string {
1717
// TODO yuck! - set by Patch which also unsets bash comp environment variables so that they don't affect further completion
1818
// introduces state and hides what is happening but works for now
1919
var wordbreakPrefix string = ""
20+
var compType = ""
21+
22+
const (
23+
COMP_TYPE_NORMAL = "9" // TAB, for normal completion
24+
COMP_TYPE_LIST_PARTIAL_WORD = "33" // ‘!’, for listing alternatives on partial word completion,
25+
COMP_TYPE_MENU_COMPLETION = "37" // ‘%’, for menu completion
26+
COMP_TYPE_LIST_SUCCESSIVE_TABS = "63" // ‘?’, for listing completions after successive tabs,
27+
COMP_TYPE_LIST_NOT_UNMODIFIED = "64" // ‘@’, to list completions if the word is not unmodified
28+
)
2029

2130
func CompLine() (string, bool) {
2231
line, ok := os.LookupEnv("COMP_LINE")
@@ -68,6 +77,7 @@ func Patch(args []string) ([]string, error) { // TODO document and fix wordbreak
6877

6978
// TODO find a better solution to pass the wordbreakprefix to bash/action.go
7079
wordbreakPrefix = tokens.CurrentPipeline().WordbreakPrefix()
80+
compType = os.Getenv("COMP_TYPE")
7181
unsetBashCompEnv()
7282

7383
return args, nil

internal/shell/bash/snippet.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
func Snippet(cmd *cobra.Command) string {
1313
result := fmt.Sprintf(`#!/bin/bash
1414
_%v_completion() {
15-
export COMP_WORDBREAKS
1615
export COMP_LINE
1716
export COMP_POINT
17+
export COMP_TYPE
18+
export COMP_WORDBREAKS
1819
1920
local nospace data compline="${COMP_LINE:0:${COMP_POINT}}"
2021

0 commit comments

Comments
 (0)