From f17a0d1bb185d141dfab2eee1313d9e13a63a013 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 17 Aug 2024 20:54:57 +0200 Subject: [PATCH] ActionExecutables: accept explicit directories --- defaultActions.go | 20 ++++++++++++------- .../defaultActions/actionExecutables.md | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/defaultActions.go b/defaultActions.go index 1d2c1131..e5925e90 100644 --- a/defaultActions.go +++ b/defaultActions.go @@ -424,16 +424,17 @@ func ActionStyles(styles ...string) Action { }).Tag("styles") } -// ActionExecutables completes PATH executables +// ActionExecutables completes executables either from PATH or given directories // // nvim // chmod -func ActionExecutables() Action { +func ActionExecutables(dirs ...string) Action { return ActionCallback(func(c Context) Action { - // TODO allow additional descriptions to be registered somewhere for carapace-bin (key, value,...) + if len(dirs) == 0 { + dirs = strings.Split(os.Getenv("PATH"), string(os.PathListSeparator)) + } + manDescriptions := man.Descriptions(c.Value) // TODO allow additional descriptions to be registered somewhere for carapace-bin (key, value,...) batch := Batch() - manDescriptions := man.Descriptions(c.Value) - dirs := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator)) for i := len(dirs) - 1; i >= 0; i-- { batch = append(batch, actionDirectoryExecutables(dirs[i], c.Value, manDescriptions)) } @@ -443,12 +444,17 @@ func ActionExecutables() Action { func actionDirectoryExecutables(dir string, prefix string, manDescriptions map[string]string) Action { return ActionCallback(func(c Context) Action { - if files, err := os.ReadDir(dir); err == nil { + abs, err := c.Abs(dir) + if err != nil { + return ActionMessage(err.Error()) + } + + if files, err := os.ReadDir(abs); err == nil { vals := make([]string, 0) for _, f := range files { if match.HasPrefix(f.Name(), prefix) { if info, err := f.Info(); err == nil && !f.IsDir() && isExecAny(info.Mode()) { - vals = append(vals, f.Name(), manDescriptions[f.Name()], style.ForPath(dir+"/"+f.Name(), c)) + vals = append(vals, f.Name(), manDescriptions[f.Name()], style.ForPath(abs+"/"+f.Name(), c)) } } } diff --git a/docs/src/carapace/defaultActions/actionExecutables.md b/docs/src/carapace/defaultActions/actionExecutables.md index cb423ad2..350a83b9 100644 --- a/docs/src/carapace/defaultActions/actionExecutables.md +++ b/docs/src/carapace/defaultActions/actionExecutables.md @@ -1,13 +1,14 @@ # ActionExecutables -[`ActionExecutables`] completes [PATH] executables. +[`ActionExecutables`] completes executables either from [PATH] or given directories. ```go carapace.ActionExecutables() +carapace.ActionExecutables("~/.local/bin") ``` ![](./actionExecutables.cast) [`ActionExecutables`]:https://pkg.go.dev/github.com/carapace-sh/carapace#ActionExecutables -[PATH]:https://en.wikipedia.org/wiki/PATH_(variable) \ No newline at end of file +[PATH]:https://en.wikipedia.org/wiki/PATH_(variable)