Code to complete executables in PATH

This commit is contained in:
Kovid Goyal
2022-09-09 20:56:16 +05:30
parent 10cf7f06c6
commit 8c2e435793
2 changed files with 63 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
)
var _ = fmt.Print
@@ -54,3 +56,21 @@ func complete_files(prefix string, callback CompleteFilesCallback) error {
return nil
}
func complete_executables_in_path(prefix string, paths ...string) []string {
ans := make([]string, 0, 1024)
if len(paths) == 0 {
paths = filepath.SplitList(os.Getenv("PATH"))
}
for _, dir := range paths {
entries, err := os.ReadDir(dir)
if err == nil {
for _, e := range entries {
if strings.HasPrefix(e.Name(), prefix) && !e.IsDir() && unix.Access(filepath.Join(dir, e.Name()), unix.X_OK) == nil {
ans = append(ans, e.Name())
}
}
}
}
return ans
}