Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionExecutables: accept explicit directories #1039

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)