Skip to content

Commit

Permalink
ActionMacro: support format
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 14, 2024
1 parent 6d1ce29 commit 7c82fa3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion action.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ func executable() string {
}

// ActionMacro completes given macro
func ActionMacro(s string) carapace.Action {
func ActionMacro(s string, a ...any) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(a) > 0 {
s = fmt.Sprintf(s, a...)
}
r := regexp.MustCompile(`^\$(?P<macro>[^(]*)(\((?P<arg>.*)\))?$`)
matches := r.FindStringSubmatch(s)
if matches == nil {
Expand Down
6 changes: 3 additions & 3 deletions macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func AddMacro(s string, m Macro) {

func MacroN(f func() carapace.Action) Macro {
return Macro{
macro: macro.MacroN[carapace.Action](func() (*carapace.Action, error) {
macro: macro.MacroN(func() (*carapace.Action, error) {
a := f()
return &a, nil
}),
Expand All @@ -43,7 +43,7 @@ func MacroN(f func() carapace.Action) Macro {

func MacroI[T any](f func(t T) carapace.Action) Macro {
return Macro{
macro: macro.MacroI[T, carapace.Action](func(t T) (*carapace.Action, error) {
macro: macro.MacroI(func(t T) (*carapace.Action, error) {
a := f(t)
return &a, nil
}),
Expand All @@ -52,7 +52,7 @@ func MacroI[T any](f func(t T) carapace.Action) Macro {

func MacroV[T any](f func(t ...T) carapace.Action) Macro {
return Macro{
macro: macro.MacroV[T, carapace.Action](func(t ...T) (*carapace.Action, error) {
macro: macro.MacroV(func(t ...T) (*carapace.Action, error) {
a := f(t...)
return &a, nil
}),
Expand Down

0 comments on commit 7c82fa3

Please sign in to comment.