Skip to content

Commit 093178b

Browse files
authored
Merge pull request #316 from carapace-sh/improve-bridge-detection
improve bridge discovery: fix bash bugs, expand builtins
2 parents 0977ec5 + 8a18afb commit 093178b

3 files changed

Lines changed: 46 additions & 47 deletions

File tree

pkg/actions/bridge/aws.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func ActionAws(command ...string) carapace.Action {
2222
return carapace.ActionValues()
2323
}
2424
for index, line := range lines {
25-
if strings.HasSuffix(line, " ") {
26-
lines[index] = strings.TrimSuffix(line, " ") // v1 has space suffix
25+
if before, ok := strings.CutSuffix(line, " "); ok {
26+
lines[index] = before // v1 has space suffix
2727
}
2828
}
2929
a := carapace.ActionValues(lines[:len(lines)-1]...)

pkg/bridges/bash.go

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,14 @@ func Bash() []string {
4343

4444
// bashCompletionLocations return folders containing bash completion scripts
4545
//
46-
// https://github.com/scop/bash-completion/blob/7864377cacb7858893a475e69c9c7461c2727e02/bash_completion#L3159C5-L3194C61
46+
// https://github.com/scop/bash-completion/blob/main/bash_completion (function _comp_load)
4747
func bashCompletionLocations() []string {
48-
locations := []string{
49-
// TODO fix order
50-
"/data/data/com.termux/files/etc/bash_completion.d", // termux
51-
"/etc/bash_completion.d", // linux
52-
"/usr/local/etc/bash_completion.d", // osx
53-
}
48+
var locations []string
5449

55-
// # Lookup order:
56-
// # 1) From BASH_COMPLETION_USER_DIR (e.g. ~/.local/share/bash-completion):
57-
// # User installed completions.
58-
// if [[ ${BASH_COMPLETION_USER_DIR-} ]]; then
59-
// _comp_split -F : paths "$BASH_COMPLETION_USER_DIR" &&
60-
// dirs+=("${paths[@]/%//completions}")
61-
// else
62-
// dirs=("${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions")
63-
// fi
50+
// 1) From BASH_COMPLETION_USER_DIR or XDG_DATA_HOME:
51+
// User installed completions are looked up first.
6452
if userDirs, ok := os.LookupEnv("BASH_COMPLETION_USER_DIR"); ok {
65-
for userDir := range strings.Split(userDirs, string(os.PathListSeparator)) {
53+
for userDir := range strings.SplitSeq(userDirs, string(os.PathListSeparator)) {
6654
locations = append(locations, fmt.Sprintf("%v/completions", userDir))
6755
}
6856
} else {
@@ -73,42 +61,21 @@ func bashCompletionLocations() []string {
7361
}
7462
}
7563

76-
// # 2) From the location of bash_completion: Completions relative to the main
77-
// # script. This is primarily for run-in-place-from-git-clone setups, where
78-
// # we want to prefer in-tree completions over ones possibly coming with a
79-
// # system installed bash-completion. (Due to usual install layouts, this
80-
// # often hits the correct completions in system installations, too.)
81-
// if [[ $BASH_SOURCE == */* ]]; then
82-
// dirs+=("${BASH_SOURCE%/*}/completions")
83-
// else
84-
// dirs+=(./completions)
85-
// fi
64+
// 2) From the location of bash_completion (completions relative to the main script).
65+
// In system installations this often hits the right directory.
8666
if sourceDir, ok := os.LookupEnv("BASH_SOURCE"); ok {
8767
locations = append(locations, fmt.Sprintf("%v/completions", sourceDir))
88-
} else {
89-
locations = append(locations, "./completions")
9068
}
9169

92-
// # 3) From bin directories extracted from the specified path to the command,
93-
// # the real path to the command, and $PATH
94-
// paths=()
95-
// [[ $cmd == /* ]] && paths+=("${cmd%/*}")
96-
// _comp_realcommand "$cmd" && paths+=("${REPLY%/*}")
97-
// _comp_split -aF : paths "$PATH"
98-
// for dir in "${paths[@]%/}"; do
99-
// [[ $dir == ?*/@(bin|sbin) ]] &&
100-
// dirs+=("${dir%/*}/share/bash-completion/completions")
101-
// done
102-
for _, pathDir := range strings.Split(os.Getenv("PATH"), string(os.PathListSeparator)) {
70+
// 3) From bin directories extracted from $PATH.
71+
// For each PATH entry ending in bin or sbin, look at ../share/bash-completion/completions.
72+
for pathDir := range strings.SplitSeq(os.Getenv("PATH"), string(os.PathListSeparator)) {
10373
locations = append(locations, fmt.Sprintf("%v/share/bash-completion/completions", pathDir))
10474
}
10575

106-
// # 4) From XDG_DATA_DIRS or system dirs (e.g. /usr/share, /usr/local/share):
107-
// # Completions in the system data dirs.
108-
// _comp_split -F : paths "${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" &&
109-
// dirs+=("${paths[@]/%//bash-completion/completions}")
76+
// 4) From XDG_DATA_DIRS or system data dirs.
11077
if dataDirs, ok := os.LookupEnv("XDG_DATA_DIRS"); ok {
111-
for _, dataDir := range strings.Split(dataDirs, string(os.PathListSeparator)) {
78+
for dataDir := range strings.SplitSeq(dataDirs, string(os.PathListSeparator)) {
11279
locations = append(locations, fmt.Sprintf("%v/bash-completion/completions", dataDir))
11380
}
11481
} else {
@@ -119,5 +86,12 @@ func bashCompletionLocations() []string {
11986
)
12087
}
12188

89+
// 5) Legacy/fallback locations (pre-XDG system dirs).
90+
locations = append(locations,
91+
"/data/data/com.termux/files/etc/bash_completion.d", // termux
92+
"/etc/bash_completion.d", // linux
93+
"/usr/local/etc/bash_completion.d", // osx
94+
)
95+
12296
return locations
12397
}

pkg/bridges/bash_builtins.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,81 @@
11
package bridges
22

3+
// compgen -b; compgen -k
34
var bashBuiltins = []string{
5+
"!",
46
".",
57
":",
68
"[",
9+
"[[",
10+
"]]",
11+
"{",
12+
"}",
713
"alias",
814
"bg",
915
"bind",
1016
"break",
1117
"builtin",
18+
"caller",
1219
"case",
1320
"cd",
1421
"command",
1522
"compgen",
1623
"complete",
24+
"compopt",
1725
"continue",
26+
"coproc",
1827
"declare",
1928
"dirs",
2029
"disown",
30+
"do",
31+
"done",
2132
"echo",
33+
"elif",
34+
"else",
2235
"enable",
36+
"esac",
2337
"eval",
2438
"exec",
2539
"exit",
2640
"export",
41+
"false",
2742
"fc",
2843
"fg",
44+
"fi",
45+
"for",
46+
"function",
2947
"getopts",
3048
"hash",
3149
"help",
3250
"history",
3351
"if",
52+
"in",
3453
"jobs",
3554
"kill",
3655
"let",
3756
"local",
3857
"logout",
58+
"mapfile",
3959
"popd",
4060
"printf",
4161
"pushd",
4262
"pwd",
4363
"read",
64+
"readarray",
4465
"readonly",
4566
"return",
67+
"select",
4668
"set",
4769
"shift",
4870
"shopt",
4971
"source",
5072
"suspend",
5173
"test",
74+
"then",
75+
"time",
5276
"times",
5377
"trap",
78+
"true",
5479
"type",
5580
"typeset",
5681
"ulimit",

0 commit comments

Comments
 (0)