Skip to content

Commit

Permalink
ActionExecutables: accept explicit directories
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 17, 2024
1 parent 07d8bbe commit f17a0d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 13 additions & 7 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions docs/src/carapace/defaultActions/actionExecutables.md
Original file line number Diff line number Diff line change
@@ -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)
[PATH]:https://en.wikipedia.org/wiki/PATH_(variable)

0 comments on commit f17a0d1

Please sign in to comment.