Skip to content

Commit

Permalink
Merge pull request #40 from rsteube/add-modifiers
Browse files Browse the repository at this point in the history
modifiers: added `chdir` and `multiparts`
  • Loading branch information
rsteube authored May 8, 2022
2 parents 3c8bccd + 7942bf2 commit b30c0d7
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action {

listDelimiter := ""
nospace := false
chdir := ""
multiparts := ""

// TODO don't alter the map each time, solve this differently
addCoreMacro("list", MacroI(func(s string) carapace.Action {
Expand All @@ -95,6 +97,15 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action {
nospace = true
return carapace.ActionValues()
}))
addCoreMacro("chdir", MacroI(func(s string) carapace.Action {
chdir = s
return carapace.ActionValues()
}))
addCoreMacro("multiparts", MacroI(func(s string) carapace.Action {
multiparts = s
return carapace.ActionValues()
}))

addCoreMacro("files", MacroV(carapace.ActionFiles))
addCoreMacro("directories", MacroN(carapace.ActionDirectories))
addCoreMacro("message", MacroI(carapace.ActionMessage))
Expand Down Expand Up @@ -126,19 +137,30 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action {
}
batch = append(batch, carapace.ActionStyledValuesDescribed(vals...))

action := batch.ToA()
if chdir != "" {
action = action.Chdir(chdir)
}
if multiparts != "" {
actionCopy := action
action = carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return actionCopy.Invoke(c).ToMultiPartsA(multiparts)
})
}

if listDelimiter != "" {
return carapace.ActionMultiParts(listDelimiter, func(c carapace.Context) carapace.Action {
for index, arg := range c.Parts {
c.Setenv(fmt.Sprintf("C_PART%v", index), arg)
}
c.Setenv("C_CALLBACK", c.CallbackValue)

return batch.ToA().Invoke(c).Filter(c.Parts).ToA()
return action.Invoke(c).Filter(c.Parts).ToA()
})
} else if nospace {
return batch.ToA().NoSpace()
return action.NoSpace()
}
return batch.ToA().Invoke(c).ToA()
return action.Invoke(c).ToA()
})
}

Expand Down

0 comments on commit b30c0d7

Please sign in to comment.