Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 27, 2022
1 parent c655fa0 commit bf35d71
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Action](./carapace/action.md)
- [Custom](./carapace/action/custom.md)
- [Chdir](./carapace/action/chDir.md)
- [NoSpace](./carapace/action/noSpace.md)
- [Suppress](./carapace/action/suppress.md)
- [Style](./carapace/action/style.md)
- [Tag](./carapace/action/tag.md)
Expand Down
1 change: 1 addition & 0 deletions docs/src/carapace/action/noSpace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# NoSpace
24 changes: 15 additions & 9 deletions docs/src/carapace/defaultActions/actionMultiParts.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# ActionMultiParts

[`ActionMultiParts`] is a [callback action](./actionCallback.md) where parts of an argument can be completed separately (e.g. `user:group` from chown). Divider can be empty as well, but note that bash and fish will add the space suffix for anything other than `/=@:.,` (it still works, but after each selection backspace is needed to continue the completion).
[`ActionMultiParts`] completes parts of an argument separately (e.g. `user:group` from chown).
For this the `Context.CallbackValue` is split with given divider and then updated to only contain the currently completed part.
`Context.Parts` contains the preceding parts and can be used in a `switch` statement to return the corresponding [Action](../action.md).

> An empty divider splits per character, but be aware that fish will add space suffix for anything other than `/=@:.,`.
```go
carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
switch len(parts) {
switch len(c.Parts) {
case 0:
return ActionUsers().Invoke(c).Suffix(":").ToA()
return carapace.ActionValues("userA", "UserB").Invoke(c).Suffix(":").ToA()
case 1:
return ActionGroups()
return carapace.ActionValues("groupA", "groupB")
default:
return carapace.ActionValues()
}
})
```

- values **must not** contain the separator as a simple `strings.Split()` is used to separate the parts
- it is however **allowed as suffix** to enable fluent tab completion (like `/` for a directory)
- Values **must not** contain the separator as a simple `strings.Split()` is used to separate the parts.
- It is however **allowed as suffix** to enable fluent tab completion (like `/` for a directory).
- If no suffix is added [`NoSpace`] can be used in the preceding parts to prevent a space suffix.

## Nesting

Expand All @@ -35,11 +40,11 @@ carapace.ActionMultiParts(",", func(cEntries carapace.Context) carapace.Action {
case 1:
switch c.Parts[0] {
case "FILE":
return carapace.ActionFiles("")
return carapace.ActionFiles("").NoSpace()
case "DIRECTORY":
return carapace.ActionDirectories()
return carapace.ActionDirectories().NoSpace()
case "VALUE":
return carapace.ActionValues("one", "two", "three")
return carapace.ActionValues("one", "two", "three").NoSpace()
default:
return carapace.ActionValues()

Expand All @@ -53,3 +58,4 @@ carapace.ActionMultiParts(",", func(cEntries carapace.Context) carapace.Action {

[`carapace.CallbackValue`]:https://pkg.go.dev/github.com/rsteube/carapace#pkg-variables
[`ActionMultiParts`]:https://pkg.go.dev/github.com/rsteube/carapace#ActionMultiParts
[`NoSpace`]:../action/noSpace.md
38 changes: 38 additions & 0 deletions example/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func init() {
actionCmd.Flags().String("files", "", "ActionFiles()")
actionCmd.Flags().String("files-filtered", "", "ActionFiles(\".md\", \"go.mod\", \"go.sum\")")
actionCmd.Flags().String("message", "", "ActionMessage()")
actionCmd.Flags().String("multiparts", "", "ActionMultiParts()")
actionCmd.Flags().String("multiparts-nested", "", "ActionMultiParts(...ActionMultiParts...)")
actionCmd.Flags().String("styled-values", "", "ActionStyledValues()")
actionCmd.Flags().String("styled-values-described", "", "ActionStyledValuesDescribed()")
actionCmd.Flags().String("values", "", "ActionValues()")
Expand All @@ -47,6 +49,42 @@ func init() {
"files": carapace.ActionFiles(),
"files-filtered": carapace.ActionFiles(".md", "go.mod", "go.sum"),
"message": carapace.ActionMessage("example message"),
"multiparts": carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionValues("userA", "UserB").Invoke(c).Suffix(":").ToA()
case 1:
return carapace.ActionValues("groupA", "groupB")
default:
return carapace.ActionValues()
}
}),
"multiparts-nested": carapace.ActionMultiParts(",", func(cEntries carapace.Context) carapace.Action {
return carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
keys := make([]string, len(cEntries.Parts))
for index, entry := range cEntries.Parts {
keys[index] = strings.Split(entry, "=")[0]
}
return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Invoke(c).Filter(keys).Suffix("=").ToA()
case 1:
switch c.Parts[0] {
case "FILE":
return carapace.ActionFiles("").NoSpace()
case "DIRECTORY":
return carapace.ActionDirectories().NoSpace()
case "VALUE":
return carapace.ActionValues("one", "two", "three").NoSpace()
default:
return carapace.ActionValues()

}
default:
return carapace.ActionValues()
}
})
}),
"styled-values": carapace.ActionStyledValues(
"first", style.Default,
"second", style.Blue,
Expand Down

0 comments on commit bf35d71

Please sign in to comment.