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

bash: handle COMP_TYPE #993

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading