From 694c41499be5a927e60b33cf799293973f7c8e00 Mon Sep 17 00:00:00 2001 From: rsteube Date: Wed, 23 Nov 2022 22:45:39 +0100 Subject: [PATCH] Action: added tags --- action.go | 1 - action_test.go | 6 +++++- defaultActions.go | 11 +++++++---- defaultActions_test.go | 2 +- internal/common/value.go | 2 +- internalActions.go | 22 ++++++++++++---------- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/action.go b/action.go index ca7ee4263..8e33bafe9 100644 --- a/action.go +++ b/action.go @@ -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 diff --git a/action_test.go b/action_test.go index 6c86a8c6d..b8d9b72ae 100644 --- a/action_test.go +++ b/action_test.go @@ -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/"}), ) } diff --git a/defaultActions.go b/defaultActions.go index 17ba7230a..a97b5424d 100644 --- a/defaultActions.go +++ b/defaultActions.go @@ -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) }) } @@ -363,5 +366,5 @@ func ActionStyles(styles ...string) Action { )) return batch.ToA() - }) + }).Tag("styles") } diff --git a/defaultActions_test.go b/defaultActions_test.go index 77538e430..9ffff9f39 100644 --- a/defaultActions_test.go +++ b/defaultActions_test.go @@ -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) { diff --git a/internal/common/value.go b/internal/common/value.go index 57c7a2201..62767cdf1 100644 --- a/internal/common/value.go +++ b/internal/common/value.go @@ -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 diff --git a/internalActions.go b/internalActions.go index 2ed2046c0..7768495c1 100644 --- a/internalActions.go +++ b/internalActions.go @@ -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 { @@ -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 {