Skip to content

Commit

Permalink
Merge pull request #993 from rsteube/bash-comptype
Browse files Browse the repository at this point in the history
bash: handle COMP_TYPE
  • Loading branch information
rsteube authored Feb 17, 2024
2 parents 38c4ae6 + 010d813 commit 91af433
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion example/cmd/_test/bash-ble.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
_example_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT
export COMP_TYPE
export COMP_WORDBREAKS

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

Expand Down
3 changes: 2 additions & 1 deletion example/cmd/_test/bash.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
_example_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT
export COMP_TYPE
export COMP_WORDBREAKS

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

Expand Down
9 changes: 4 additions & 5 deletions internal/shell/bash/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
meta.Nospace.Add('*')
}

nospace := true
nospace := false
vals := make([]string, len(values))
for index, val := range values {
if len(values) == 1 {
if !meta.Nospace.Matches(val.Value) {
nospace = false
}
if len(values) == 1 || compType != COMP_TYPE_LIST_SUCCESSIVE_TABS {
nospace = nospace || meta.Nospace.Matches(val.Value)

vals[index] = sanitizer.Replace(val.Value)
if requiresQuoting(vals[index]) {
Expand All @@ -98,6 +96,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
}
}
} else {
nospace = true
val.Display = displayReplacer.Replace(val.Display)
val.Description = displayReplacer.Replace(val.Description)
if val.Description != "" {
Expand Down
10 changes: 10 additions & 0 deletions internal/shell/bash/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func (r RedirectError) Error() string {
// TODO yuck! - set by Patch which also unsets bash comp environment variables so that they don't affect further completion
// introduces state and hides what is happening but works for now
var wordbreakPrefix string = ""
var compType = ""

const (
COMP_TYPE_NORMAL = "9" // TAB, for normal completion
COMP_TYPE_LIST_PARTIAL_WORD = "33" // ‘!’, for listing alternatives on partial word completion,
COMP_TYPE_MENU_COMPLETION = "37" // ‘%’, for menu completion
COMP_TYPE_LIST_SUCCESSIVE_TABS = "63" // ‘?’, for listing completions after successive tabs,
COMP_TYPE_LIST_NOT_UNMODIFIED = "64" // ‘@’, to list completions if the word is not unmodified
)

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

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

return args, nil
Expand Down
3 changes: 2 additions & 1 deletion internal/shell/bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
func Snippet(cmd *cobra.Command) string {
result := fmt.Sprintf(`#!/bin/bash
_%v_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT
export COMP_TYPE
export COMP_WORDBREAKS
local nospace data compline="${COMP_LINE:0:${COMP_POINT}}"
Expand Down

0 comments on commit 91af433

Please sign in to comment.