From b937390e0a6feb8582194bb5490bcdd79ea1a43a Mon Sep 17 00:00:00 2001 From: rsteube Date: Wed, 11 May 2022 14:14:17 +0200 Subject: [PATCH] macro: added DisableFlagParsing() --- action.go | 28 ++++++---------------------- core.go | 29 +++++++++++++++++++++++++++++ macro.go | 8 +++++--- 3 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 core.go diff --git a/action.go b/action.go index 8870d8f..9cdb0d4 100644 --- a/action.go +++ b/action.go @@ -37,9 +37,12 @@ func ActionSpec(path string) carapace.Action { func parseAction(cmd *cobra.Command, arr []string) carapace.Action { if !cmd.DisableFlagParsing { for _, entry := range arr { - if strings.HasPrefix(entry, "$spec(") { - cmd.DisableFlagParsing = true // implicitly disable flag parsing - break + if strings.HasPrefix(entry, "$") { + macro := strings.SplitN(strings.TrimPrefix(entry, "$"), "(", 2)[0] + if m, ok := macros[macro]; ok && m.disableFlagParsing { + cmd.DisableFlagParsing = true // implicitly disable flag parsing + break + } } } } @@ -78,25 +81,6 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action { return carapace.ActionValues() })) - addCoreMacro("directories", MacroN(carapace.ActionDirectories)) - addCoreMacro("files", MacroV(carapace.ActionFiles)) - addCoreMacro("message", MacroI(carapace.ActionMessage)) - addCoreMacro("spec", MacroI(ActionSpec)) - addCoreMacro("", MacroI(func(s string) carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionExecCommand("sh", "-c", s)(func(output []byte) carapace.Action { - lines := strings.Split(string(output), "\n") - vals := make([]string, 0) - for _, line := range lines { - if line != "" { - vals = append(vals, parseValue(line)...) - } - } - return carapace.ActionStyledValuesDescribed(vals...) - }).Invoke(c).ToA() - }) - })) - batch := carapace.Batch() vals := make([]string, 0) for _, elem := range arr { diff --git a/core.go b/core.go new file mode 100644 index 0000000..6de3248 --- /dev/null +++ b/core.go @@ -0,0 +1,29 @@ +package spec + +import ( + "strings" + + "github.com/rsteube/carapace" +) + +func init() { + addCoreMacro("directories", MacroN(carapace.ActionDirectories)) + addCoreMacro("disableflagparsing", MacroN(func() carapace.Action { return carapace.ActionValues() }).DisableFlagParsing()) + addCoreMacro("files", MacroV(carapace.ActionFiles)) + addCoreMacro("message", MacroI(carapace.ActionMessage)) + addCoreMacro("spec", MacroI(ActionSpec).DisableFlagParsing()) + addCoreMacro("", MacroI(func(s string) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("sh", "-c", s)(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + vals := make([]string, 0) + for _, line := range lines { + if line != "" { + vals = append(vals, parseValue(line)...) + } + } + return carapace.ActionStyledValuesDescribed(vals...) + }).Invoke(c).ToA() + }) + })) +} diff --git a/macro.go b/macro.go index 01b80ee..98d5566 100644 --- a/macro.go +++ b/macro.go @@ -11,11 +11,13 @@ import ( ) type Macro struct { - f func(string) carapace.Action - s func() string + f func(string) carapace.Action + s func() string + disableFlagParsing bool } -func (m Macro) Signature() string { return m.s() } +func (m Macro) Signature() string { return m.s() } +func (m Macro) DisableFlagParsing() Macro { m.disableFlagParsing = true; return m } var macros = make(map[string]Macro)