Skip to content

Commit

Permalink
Merge pull request #60 from rsteube/macro-disableflagparsing
Browse files Browse the repository at this point in the history
macro: added DisableFlagParsing()
  • Loading branch information
rsteube authored May 11, 2022
2 parents 81ffd24 + b937390 commit 93bb53f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
28 changes: 6 additions & 22 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
@@ -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()
})
}))
}
8 changes: 5 additions & 3 deletions macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 93bb53f

Please sign in to comment.