Skip to content

Commit

Permalink
Merge pull request #210 from rsteube/traverse
Browse files Browse the repository at this point in the history
added traverse
  • Loading branch information
rsteube authored Sep 3, 2023
2 parents ec91ac7 + fcc171b commit 487c572
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
27 changes: 27 additions & 0 deletions docs/src/carapace-spec/macros/modifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ These can be set generic `["<macro>", "<value>", "<modifier>"]` specific `["<mac
["$files", "$chdir(/tmp)"]
```

The following macros can be passed as well instead of a static directory:

- [`$gitdir`] .git folder
- [`$gitworktree`] root of the working directory for a non-bare repository
- [`$parent(\[file, dir\])`] first parent directory containing any of the given names/directories
- [`$tempdir`] default directory to use for temporary files
- [`$usercachedir`] root directory to use for user-specific cached data
- [`$userconfigdir`] default root directory to use for user-specific configuration data
- [`$userhomedir`] current user's home directory
- [`$xdgcachehome`] cache directory (fallback to UserCacheDir)
- [`$xdgconfighome`] home directory (fallback to UserConfigDir)

```yaml
["$files", "$chdir($gitdir)"]
```

## filter

[`$filter([<value>])`](https://rsteube.github.io/carapace/carapace/action/filter.html) filters given values.
Expand Down Expand Up @@ -139,3 +155,14 @@ These can be set generic `["<macro>", "<value>", "<modifier>"]` specific `["<mac
```yaml
["$usage(custom)"]
```


[`$gitdir`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#GitDir
[`$gitworktree`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#GitWorkTree
[`$parent(\[file, dir\])`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#Parent
[`$tempdir`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#TempDir
[`$usercachedir`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#UserCacheDir
[`$userconfigdir`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#UserConfigDir
[`$userhomedir`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#UserHomeDir
[`$xdgcachehome`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#XdgCacheHome
[`$xdgconfighome`]:https://pkg.go.dev/github.com/rsteube/carapace/pkg/traverse#XdgConfigHome
22 changes: 22 additions & 0 deletions example/traverse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: traverse
flags:
--gitdir=: .git folder
--gitworktree=: root of the working directory for a non-bare repository
--parent=: first parent directory containing any of the given names/directories
--tempdir=: default directory to use for temporary files
--usercachedir=: root directory to use for user-specific cached data
--userconfigdir=: default root directory to use for user-specific configuration data
--userhomedir=: current user's home directory
--xdgcachehome=: cache directory (fallback to UserCacheDir)
--xdgconfighome=: home directory (fallback to UserConfigDir)
completion:
flag:
gitdir: ["$files", "$chdir($gitdir)"]
gitworktree: ["$files", "$chdir($gitworktree)"]
parent: ["$files", "$chdir($parent([go.mod, go.sum]))"]
tempdir: ["$files", "$chdir($tempdir)"]
usercachedir: ["$files", "$chdir($usercachedir)"]
userconfigdir: ["$files", "$chdir($userconfigdir)"]
userhomedir: ["$files", "$chdir($userhomedir)"]
xdgcachehome: ["$files", "$chdir($xdgcachehome)"]
xdgconfighome: ["$files", "$chdir($xdgconfighome)"]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/invopop/jsonschema v0.7.0
github.com/rsteube/carapace v0.43.1
github.com/rsteube/carapace v0.43.2
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy77
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsteube/carapace v0.43.1 h1:u1cOxV49qKdlM6uFToyaCkFbRy4m6a4aUa0ksBeRgCM=
github.com/rsteube/carapace v0.43.1/go.mod h1:1l4sZA+/sGW9sBLqEholUd+kn9xKgh2ghBFDYC3D/zA=
github.com/rsteube/carapace v0.43.2 h1:lVlKJx3d9ZxBfxclAjNU2AmbtQXl7Cg4tFZfV2YPueg=
github.com/rsteube/carapace v0.43.2/go.mod h1:1l4sZA+/sGW9sBLqEholUd+kn9xKgh2ghBFDYC3D/zA=
github.com/rsteube/carapace-shlex v0.0.4 h1:3GVn8PaM2RCxPTAiwVy9vDQI8Mi7DqrbdpDgf5ZzQmY=
github.com/rsteube/carapace-shlex v0.0.4/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
25 changes: 24 additions & 1 deletion modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/traverse"
)

type modifier struct {
Expand All @@ -21,7 +22,7 @@ func (m modifier) Parse(s string) carapace.Action {
}

modifiers := map[string]Macro{
"$chdir": MacroI(m.Action.Chdir),
"$chdir": MacroI(m.chdir),
"$filter": MacroV(m.Action.Filter),
"$filterargs": MacroN(m.Action.FilterArgs),
"$list": MacroI(m.Action.List),
Expand All @@ -47,6 +48,28 @@ func (m modifier) Parse(s string) carapace.Action {
})
}

func (m modifier) chdir(s string) carapace.Action {
if !strings.HasPrefix(s, "$") {
return m.Action.Chdir(s)
}

traverse := map[string]Macro{
"$gitdir": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.GitDir) }),
"$gitworktree": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.GitWorkTree) }),
"$parent": MacroV(func(s ...string) carapace.Action { return m.Action.ChdirF(traverse.Parent(s...)) }),
"$tempdir": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.TempDir) }),
"$usercachedir": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.UserCacheDir) }),
"$userconfigdir": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.UserConfigDir) }),
"$userhomedir": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.UserHomeDir) }),
"$xdgcachehome": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.XdgCacheHome) }),
"$xdgconfighome": MacroN(func() carapace.Action { return m.Action.ChdirF(traverse.XdgConfigHome) }),
}
if modifier, ok := traverse[strings.SplitN(s, "(", 2)[0]]; ok {
return modifier.parse(s)
}
return carapace.ActionMessage("unknown macro: %#v", s)
}

func updateEnv(a carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
for index, arg := range c.Parts {
Expand Down

0 comments on commit 487c572

Please sign in to comment.