Skip to content

Commit

Permalink
Merge pull request #607 from rsteube/action-tag
Browse files Browse the repository at this point in the history
Action: added Tag
  • Loading branch information
rsteube authored Nov 23, 2022
2 parents 902c5f7 + 61fd67b commit 6ddeb5f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
32 changes: 29 additions & 3 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,40 @@ func (a Action) StyleF(f func(s string) string) Action {
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
for index, v := range invoked.rawValues {
if v.Value != "ERR" && v.Value != "_" {
if !v.IsMessage() {
invoked.rawValues[index].Style = f(v.Value)
}
}
return invoked.ToA()
})
}

// Tag sets the tag.
//
// ActionValues("192.168.1.1", "127.0.0.1").Tag("interfaces").
func (a Action) Tag(tag string) Action {
return a.TagF(func(value string) string {
return tag
})
}

// Tag sets the tag using a function.
//
// ActionValues("192.168.1.1", "127.0.0.1").TagF(func(value string) string {
// return "interfaces"
// })
func (a Action) TagF(f func(value string) string) Action {
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
for index, v := range invoked.rawValues {
if !v.IsMessage() {
invoked.rawValues[index].Tag = f(v.Value)
}
}
return invoked.ToA()
})
}

// Chdir changes the current working directory to the named directory during invocation.
func (a Action) Chdir(dir string) Action {
return ActionCallback(func(c Context) Action {
Expand All @@ -136,7 +162,7 @@ func (a Action) Suppress(expr ...string) Action {
invoked := a.Invoke(c)
filter := false
for _, rawValue := range invoked.rawValues {
if rawValue.Display == "ERR" {
if rawValue.IsMessage() && rawValue.Description != "" {
for _, e := range expr {
r, err := regexp.Compile(e)
if err != nil {
Expand All @@ -153,7 +179,7 @@ func (a Action) Suppress(expr ...string) Action {
if filter {
filtered := make([]common.RawValue, 0)
for _, r := range invoked.rawValues {
if r.Display != "ERR" && r.Display != "_" {
if !r.IsMessage() {
filtered = append(filtered, r)
}
}
Expand Down
8 changes: 5 additions & 3 deletions defaultActions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ func TestActionImport(t *testing.T) {
"Value": "positional1",
"Display": "positional1",
"Description": "",
"Style": ""
"Style": "",
"Tag": "first"
},
{
"Value": "p1",
"Display": "p1",
"Description": "",
"Style": ""
"Style": "",
"Tag": "first"
}
]
}`
assertEqual(t, ActionValues("positional1", "p1").Invoke(Context{}), ActionImport([]byte(s)).Invoke(Context{}))
assertEqual(t, ActionValues("positional1", "p1").Tag("first").Invoke(Context{}), ActionImport([]byte(s)).Invoke(Context{}))
}

func TestActionFlags(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions example/cmd/action/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ActionGroups() carapace.Action {
}
}
return carapace.ActionValuesDescribed(groups...)
})
}).Tag("groups")
}

// ActionKillSignals completes kill signals
Expand Down Expand Up @@ -110,7 +110,7 @@ func ActionUsers() carapace.Action {
}
}
return carapace.ActionValuesDescribed(users...)
})
}).Tag("users")
}

// ActionUserGroup completes user:group
Expand Down
6 changes: 3 additions & 3 deletions example/cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func init() {
carapace.Gen(batchCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.Batch(
carapace.ActionValues("A", "B"),
carapace.ActionValues("C", "D"),
carapace.ActionValues("E", "F"),
carapace.ActionValues("A", "B").Tag("first"),
carapace.ActionValues("C", "D").Tag("second"),
carapace.ActionValues("E", "F").TagF(func(value string) string { return "third" }),
).Invoke(c).Merge().ToA()
}),
)
Expand Down
10 changes: 8 additions & 2 deletions internal/common/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import (
type RawValue struct {
Value string
Display string
Description string
Style string
Description string `json:",omitempty"`
Style string `json:",omitempty"`
Tag string `json:",omitempty"`
}

// IsMessage checks if the value is a message (ActionMessage)
func (r RawValue) IsMessage() bool {
return r.Value == "ERR" || r.Value == "_"
}

// TrimmedDescription returns the trimmed description
Expand Down
14 changes: 7 additions & 7 deletions invokedAction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func TestToMultiParts(t *testing.T) {
_test("A/a:1", `{"Value":"A/a:1","Display":"1","Description":"one","Style":"green"}`, "/", ":")
_test("A/a:1", `{"Value":"A/a:1","Display":"1","Description":"one","Style":"green"}`, ":", "/")
_test("A/a:1", `{"Value":"A/a:1","Display":"a:1","Description":"one","Style":"green"}`, "/")
_test("A", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/", ":")
_test("A", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/")
_test("", `{"Value":"A/","Display":"A/","Description":"","Style":""}`, "/")
_test("A/", `{"Value":"A/a:","Display":"a:","Description":"","Style":""}`, "/", ":")
_test("A", `{"Value":"A/","Display":"A/"}`, "/", ":")
_test("A", `{"Value":"A/","Display":"A/"}`, "/")
_test("", `{"Value":"A/","Display":"A/"}`, "/")
_test("A/", `{"Value":"A/a:","Display":"a:"}`, "/", ":")
_test("A/", `{"Value":"A/a:1","Display":"a:1","Description":"one","Style":"green"}`, "/")
_test("B/", `{"Value":"B/c/","Display":"c/","Description":"withsuffix","Style":"underlined"}`, "/")
_test("B/c:5", `{"Value":"B/c:5:2/","Display":"c:5:2/","Description":"","Style":""}`, "/")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:","Description":"","Style":""}`, "/", ":")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:","Description":"","Style":""}`, ":", "/")
_test("B/c:5", `{"Value":"B/c:5:2/","Display":"c:5:2/"}`, "/")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:"}`, "/", ":")
_test("B/c:5", `{"Value":"B/c:5:","Display":"5:"}`, ":", "/")

_test("C/d/1", `{"Value":"C/d/1()2","Display":"1()2","Description":"withbrackets","Style":"yellow"}`, "/")
}

0 comments on commit 6ddeb5f

Please sign in to comment.