Skip to content

Commit

Permalink
Merge pull request #609 from rsteube/added-tags
Browse files Browse the repository at this point in the history
Action: added tags
  • Loading branch information
rsteube authored Nov 24, 2022
2 parents cdaadd2 + 694c414 commit 292120b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
1 change: 0 additions & 1 deletion action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type CompletionCallback func(c Context) Action

// Cache cashes values of a CompletionCallback for given duration and keys
func (a Action) Cache(timeout time.Duration, keys ...pkgcache.Key) Action {
// TODO static actions are using callback now as well (for performance) - probably best to add a `static` bool to Action for this and check that here
if a.callback != nil { // only relevant for callback actions
cachedCallback := a.callback
_, file, line, _ := runtime.Caller(1) // generate uid from wherever Cache() was called
Expand Down
6 changes: 5 additions & 1 deletion action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ func TestActionFilesChdir(t *testing.T) {
}

func TestActionMessage(t *testing.T) {
expected := ActionStyledValuesDescribed("_", "", style.Default, "ERR", "example message", style.Carapace.Error).noSpace("*").skipCache(true).Invoke(Context{}).Prefix("docs/")
for index := range expected.rawValues {
expected.rawValues[index].Tag = "messages"
}
assertEqual(t,
ActionStyledValuesDescribed("_", "", style.Default, "ERR", "example message", style.Carapace.Error).noSpace("*").skipCache(true).Invoke(Context{}).Prefix("docs/"),
expected,
ActionMessage("example message").Invoke(Context{CallbackValue: "docs/"}),
)
}
Expand Down
11 changes: 7 additions & 4 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ func ActionMessage(msg string, a ...interface{}) Action {
if len(a) > 0 {
msg = fmt.Sprintf(msg, a...)
}
return ActionStyledValuesDescribed("_", "", style.Default, "ERR", msg, style.Carapace.Error).
Invoke(c).Prefix(c.CallbackValue).ToA(). // needs to be prefixed with current callback value to not be filtered out
noSpace("*").skipCache(true)
m := ActionStyledValuesDescribed("_", "", style.Default, "ERR", msg, style.Carapace.Error).Invoke(c)
for index := range m.rawValues {
m.rawValues[index].Tag = "messages"
}
return m.Prefix(c.CallbackValue).ToA(). // needs to be prefixed with current callback value to not be filtered out
noSpace("*").skipCache(true)
})
}

Expand Down Expand Up @@ -363,5 +366,5 @@ func ActionStyles(styles ...string) Action {
))

return batch.ToA()
})
}).Tag("styles")
}
2 changes: 1 addition & 1 deletion defaultActions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestActionFlags(t *testing.T) {

cmd.Flag("alpha").Changed = true
a := actionFlags(cmd).Invoke(Context{CallbackValue: "-a"})
assertEqual(t, ActionValuesDescribed("b", "").NoSpace().Invoke(Context{}).Prefix("-a"), a)
assertEqual(t, ActionValuesDescribed("b", "").Tag("flags").NoSpace().Invoke(Context{}).Prefix("-a"), a)
}

func TestActionExecCommandEnv(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/common/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RawValue struct {

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

// TrimmedDescription returns the trimmed description
Expand Down
22 changes: 12 additions & 10 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action {
return ActionStyledValues(vals...).Invoke(Context{}).Prefix("./").ToA()
}
return ActionStyledValues(vals...)
})
}).Tag("files")
}

func actionFlags(cmd *cobra.Command) Action {
Expand Down Expand Up @@ -120,20 +120,22 @@ func actionFlags(cmd *cobra.Command) Action {
}
}
return ActionValuesDescribed(vals...)
})
}).Tag("flags")
}

func actionSubcommands(cmd *cobra.Command) Action {
vals := make([]string, 0)
for _, subcommand := range cmd.Commands() {
if !subcommand.Hidden && subcommand.Deprecated == "" {
vals = append(vals, subcommand.Name(), subcommand.Short)
for _, alias := range subcommand.Aliases {
vals = append(vals, alias, subcommand.Short)
return ActionCallback(func(c Context) Action {
vals := make([]string, 0)
for _, subcommand := range cmd.Commands() {
if !subcommand.Hidden && subcommand.Deprecated == "" {
vals = append(vals, subcommand.Name(), subcommand.Short)
for _, alias := range subcommand.Aliases {
vals = append(vals, alias, subcommand.Short)
}
}
}
}
return ActionValuesDescribed(vals...)
return ActionValuesDescribed(vals...)
}).Tag("commands")
}

func actionRawValues(rawValues ...common.RawValue) Action {
Expand Down

0 comments on commit 292120b

Please sign in to comment.