@@ -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)
4747func 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}
0 commit comments