Skip to content

Commit

Permalink
Merge pull request #861 from rsteube/multipartsp-renamed-param
Browse files Browse the repository at this point in the history
MultiPartsP: renamed param
  • Loading branch information
rsteube authored Jul 30, 2023
2 parents 1ce366b + b4f6f96 commit 64e2b3a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ func (a Action) MultiParts(dividers ...string) Action {
})
}

// MultiPartsP is like MultiParts but with placeholder completion.
func (a Action) MultiPartsP(delimiter string, placeholderPattern string, f func(segment string, matches map[string]string) Action) Action {
// MultiPartsP is like MultiParts but with placeholders
func (a Action) MultiPartsP(delimiter string, pattern string, f func(placeholder string, matches map[string]string) Action) Action {
// TODO add delimiter as suffix for proper nospace handling (some values/placeholders might appear with and without suffix)
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)

return ActionMultiParts(delimiter, func(c Context) Action {
placeholder := regexp.MustCompile(placeholderPattern)
rPlaceholder := regexp.MustCompile(pattern)
matchedData := make(map[string]string)
matchedSegments := make(map[string]common.RawValue)
staticMatches := make(map[int]bool)
Expand All @@ -135,7 +136,7 @@ func (a Action) MultiPartsP(delimiter string, placeholderPattern string, f func(
break segment
} else {
if segment != c.Parts[index] {
if !placeholder.MatchString(segment) {
if !rPlaceholder.MatchString(segment) {
continue path // skip this path as it doesn't match and is not a placeholder
} else {
matchedData[segment] = c.Parts[index] // store entered data for placeholder (overwrite if duplicate)
Expand All @@ -160,7 +161,7 @@ func (a Action) MultiPartsP(delimiter string, placeholderPattern string, f func(
if len(segments) == (len(c.Parts) + 1) {
matchedSegments[segments[len(c.Parts)]] = invoked.rawValues[index]
} else {
if segment := segments[len(c.Parts)]; placeholder.MatchString(segment) {
if segment := segments[len(c.Parts)]; rPlaceholder.MatchString(segment) {
matchedSegments[segment] = common.RawValue{}
} else {
matchedSegments[segment+delimiter] = common.RawValue{}
Expand All @@ -170,7 +171,7 @@ func (a Action) MultiPartsP(delimiter string, placeholderPattern string, f func(

actions := make([]Action, 0, len(matchedSegments))
for key, value := range matchedSegments {
if placeholder.MatchString(key) {
if rPlaceholder.MatchString(key) {
actions = append(actions, f(key, matchedData))
} else {
actions = append(actions, ActionStyledValuesDescribed(key, value.Description, value.Style)) // TODO tag,..
Expand Down
4 changes: 2 additions & 2 deletions docs/src/carapace/action/multiPartsP.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# MultiPartsP

[`MultiPartsP`] is like [MultiParts] but with placeholder completion.
[`MultiPartsP`] is like [MultiParts] but with placeholders.

```go
carapace.ActionStyledValuesDescribed(
"keys/<key>/<value>", "key/value example", style.Default,
"styles/custom", "custom style", style.Of(style.Blue, style.Blink),
"styles", "list", style.Yellow,
"styles/<style>", "details", style.Default,
).MultiPartsP("/", "<.*>", func(segment string, matches map[string]string) carapace.Action {
).MultiPartsP("/", "<.*>", func(placeholder string, matches map[string]string) carapace.Action {
switch segment {
case "<key>":
return carapace.ActionValues("key1", "key2")
Expand Down
4 changes: 2 additions & 2 deletions example/cmd/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func init() {
"styles/custom", "custom style", style.Of(style.Blue, style.Blink),
"styles", "list", style.Yellow,
"styles/<style>", "details", style.Default,
).MultiPartsP("/", "<.*>", func(segment string, matches map[string]string) carapace.Action {
switch segment {
).MultiPartsP("/", "<.*>", func(placeholder string, matches map[string]string) carapace.Action {
switch placeholder {
case "<key>":
return carapace.ActionValues("key1", "key2")
case "<style>":
Expand Down

0 comments on commit 64e2b3a

Please sign in to comment.