Skip to content

Commit

Permalink
bash: fix compline patching
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 2, 2023
1 parent 8da259a commit 401c006
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions complete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package carapace

import (
"os"

"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/shell/bash"
"github.com/rsteube/carapace/internal/shell/nushell"
Expand All @@ -22,6 +24,8 @@ func complete(cmd *cobra.Command, args []string) (string, error) {
args = nushell.Patch(args) // handle open quotes
LOG.Printf("patching args to %#v", args)
case "bash": // TODO what about oil and such?
LOG.Printf("COMP_LINE is %#v", os.Getenv("COMP_LINE"))
LOG.Printf("COMP_POINT is %#v", os.Getenv("COMP_POINT"))
var err error
args, err = bash.Patch(args) // handle redirects
LOG.Printf("patching args to %#v", args)
Expand Down
1 change: 1 addition & 0 deletions example/cmd/_test/bash-ble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
_example_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT

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

Expand Down
1 change: 1 addition & 0 deletions example/cmd/_test/bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
_example_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT

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

Expand Down
22 changes: 21 additions & 1 deletion internal/shell/bash/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bash

import (
"os"
"strconv"

shlex "github.com/rsteube/carapace-shlex"
)
Expand All @@ -17,6 +18,25 @@ func (r RedirectError) Error() string {
// introduces state and hides what is happening but works for now
var wordbreakPrefix string = ""

func CompLine() (string, bool) {
line, ok := os.LookupEnv("COMP_LINE")
if !ok {
return "", false
}

point, ok := os.LookupEnv("COMP_POINT")
if !ok {
return "", false
}

pointI, err := strconv.Atoi(point)
if err != nil || len(line) < pointI {
return "", false
}

return line[:pointI], true
}

// Patch patches args if `COMP_LINE` environment variable is set.
//
// Bash passes redirects to the completion function so these need to be filtered out.
Expand All @@ -25,7 +45,7 @@ var wordbreakPrefix string = ""
// ["example", "action", ">", "/tmp/stdout.txt", "--values", "2", ">", "/tmp/stderr.txt", "fi"]
// ["example", "action", "--values", "fi"]
func Patch(args []string) ([]string, error) { // TODO document and fix wordbreak splitting (e.g. `:`)
compline, ok := os.LookupEnv("COMP_LINE")
compline, ok := CompLine()
if !ok {
return args, nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/shell/bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Snippet(cmd *cobra.Command) string {
_%v_completion() {
export COMP_WORDBREAKS
export COMP_LINE
export COMP_POINT
local nospace data compline="${COMP_LINE:0:${COMP_POINT}}"
Expand Down

0 comments on commit 401c006

Please sign in to comment.