Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionMacro: support format #297

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading