From 00852d8d98fe3bca202648063da6e8bbcfee1e60 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 3 Aug 2023 01:52:26 +0200 Subject: [PATCH] action: added FilterArgs and FilterParts - changed Filter argument to vararg --- action.go | 22 ++- action_test.go | 12 +- defaultActions.go | 2 +- docs/src/SUMMARY.md | 2 + docs/src/carapace/action/filter.md | 2 +- docs/src/carapace/action/filterArgs.cast | 131 ++++++++++++++++++ docs/src/carapace/action/filterArgs.md | 15 ++ docs/src/carapace/action/filterParts.cast | 51 +++++++ docs/src/carapace/action/filterParts.md | 17 +++ .../defaultActions/actionMultiParts.md | 2 +- docs/src/carapace/invokedAction/filter.md | 2 +- example-nonposix/cmd/root.go | 2 +- example/cmd/action.go | 2 +- example/cmd/modifier.go | 16 ++- example/cmd/modifier_test.go | 53 +++++++ example/cmd/multiparts.go | 12 +- invokedAction.go | 2 +- pkg/traverse/git.go | 4 +- pkg/traverse/traverse.go | 13 ++ 19 files changed, 335 insertions(+), 27 deletions(-) create mode 100644 docs/src/carapace/action/filterArgs.cast create mode 100644 docs/src/carapace/action/filterArgs.md create mode 100644 docs/src/carapace/action/filterParts.cast create mode 100644 docs/src/carapace/action/filterParts.md diff --git a/action.go b/action.go index c91f1b5fc..0a4d76287 100644 --- a/action.go +++ b/action.go @@ -86,10 +86,24 @@ func (a Action) ChdirF(f func(tc pkgtraverse.Context) (string, error)) Action { // Filter filters given values. // -// carapace.ActionValues("A", "B", "C").Filter([]string{"B"}) // ["A", "C"] -func (a Action) Filter(values []string) Action { +// carapace.ActionValues("A", "B", "C").Filter("B") // ["A", "C"] +func (a Action) Filter(values ...string) Action { return ActionCallback(func(c Context) Action { - return a.Invoke(c).Filter(values).ToA() + return a.Invoke(c).Filter(values...).ToA() + }) +} + +// FilterArgs filters Context.Args +func (a Action) FilterArgs() Action { + return ActionCallback(func(c Context) Action { + return a.Filter(c.Args...) + }) +} + +// FilterArgs filters Context.Parts +func (a Action) FilterParts() Action { + return ActionCallback(func(c Context) Action { + return a.Filter(c.Parts...) }) } @@ -400,7 +414,7 @@ func (a Action) Timeout(d time.Duration, alternative Action) Action { // UniqueList wraps the Action in an ActionMultiParts with given divider. func (a Action) UniqueList(divider string) Action { return ActionMultiParts(divider, func(c Context) Action { - return a.Invoke(c).Filter(c.Parts).ToA().NoSpace() + return a.FilterParts().NoSpace() }) } diff --git a/action_test.go b/action_test.go index a9c32f9f5..64ee89ecb 100644 --- a/action_test.go +++ b/action_test.go @@ -81,7 +81,7 @@ func TestSkipCache(t *testing.T) { ActionCallback(func(c Context) Action { return ActionMessage("skipcache") }).Invoke(c)). - Filter([]string{""}). + Filter(""). Prefix(""). Suffix(""). ToA() @@ -100,7 +100,7 @@ func TestNoSpace(t *testing.T) { ActionMultiParts("", func(c Context) Action { return ActionMessage("nospace") }).Invoke(c)). - Filter([]string{""}). + Filter(""). Prefix(""). Suffix(""). ToA() @@ -123,7 +123,7 @@ func TestActionDirectories(t *testing.T) { "pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), "third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), ).NoSpace('/').Tag("directories").Invoke(Context{}), - ActionDirectories().Invoke(Context{Value: ""}).Filter([]string{"vendor/"}), + ActionDirectories().Invoke(Context{Value: ""}).Filter("vendor/"), ) assertEqual(t, @@ -135,7 +135,7 @@ func TestActionDirectories(t *testing.T) { "pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), "third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), ).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("./"), - ActionDirectories().Invoke(Context{Value: "./"}).Filter([]string{"./vendor/"}), + ActionDirectories().Invoke(Context{Value: "./"}).Filter("./vendor/"), ) assertEqual(t, @@ -165,7 +165,7 @@ func TestActionFiles(t *testing.T) { "pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), "third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold), ).NoSpace('/').Tag("files").Invoke(Context{}), - ActionFiles(".md").Invoke(Context{Value: ""}).Filter([]string{"vendor/"}), + ActionFiles(".md").Invoke(Context{Value: ""}).Filter("vendor/"), ) assertEqual(t, @@ -176,7 +176,7 @@ func TestActionFiles(t *testing.T) { "main.go", style.Of("fg-default", "bg-default"), "main_test.go", style.Of("fg-default", "bg-default"), ).NoSpace('/').Tag("files").Invoke(Context{}).Prefix("example/"), - ActionFiles().Invoke(Context{Value: "example/"}).Filter([]string{"example/example"}), + ActionFiles().Invoke(Context{Value: "example/"}).Filter("example/example"), ) } diff --git a/defaultActions.go b/defaultActions.go index 3a254ca45..e3eb98db9 100644 --- a/defaultActions.go +++ b/defaultActions.go @@ -293,7 +293,7 @@ func ActionStyleConfig() Action { }) case 1: return ActionMultiParts(",", func(c Context) Action { - return ActionStyles(c.Parts...).Invoke(c).Filter(c.Parts).ToA().NoSpace() + return ActionStyles(c.Parts...).Invoke(c).Filter(c.Parts...).ToA().NoSpace() }) default: return ActionValues() diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index b2a2fe93d..ef459ae7b 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -21,6 +21,8 @@ - [Chdir](./carapace/action/chdir.md) - [ChdirF](./carapace/action/chdirF.md) - [Filter](./carapace/action/filter.md) + - [FilterArgs](./carapace/action/filterArgs.md) + - [FilterParts](./carapace/action/filterParts.md) - [Invoke](./carapace/action/invoke.md) - [List](./carapace/action/list.md) - [MultiParts](./carapace/action/multiParts.md) diff --git a/docs/src/carapace/action/filter.md b/docs/src/carapace/action/filter.md index 394e4eaa0..4df0611f4 100644 --- a/docs/src/carapace/action/filter.md +++ b/docs/src/carapace/action/filter.md @@ -8,7 +8,7 @@ carapace.ActionValuesDescribed( "2", "two", "3", "three", "4", "four", -).Filter([]string{"2", "4"}) +).Filter("2", "4") ``` ![](./filter.cast) diff --git a/docs/src/carapace/action/filterArgs.cast b/docs/src/carapace/action/filterArgs.cast new file mode 100644 index 000000000..37f734878 --- /dev/null +++ b/docs/src/carapace/action/filterArgs.cast @@ -0,0 +1,131 @@ +{"version": 2, "width": 108, "height": 24, "timestamp": 1691080278, "env": {"SHELL": "elvish", "TERM": "tmux-256color"}} +[0.082843, "o", "\u001b[?7h\u001b[7m⏎\u001b[m \r \r\u001b[?7l\u001b[?2004h"] +[0.08353, "o", "\u001b[?25l\r???> ???> \r\u001b[5C\u001b[?25h\u001b[?25l\r\u001b[5C\u001b[K\r\u001b[5C\u001b[?25h"] +[0.099332, "o", "\u001b[?25l\r\r\u001b[5C\u001b[?25h\u001b[?25l\r\u001b[K\r\n\u001b[0;1;36mcarapace/example\u001b[0;m on \u001b[0;1;35m action-filter\u001b[0;m \u001b[0;1;31m[$!?]\u001b[0;m via \u001b[0;1;36m🐹 v1.20.6 \r\n\u001b[0;1;37mesh\u001b[0;m \u001b[0;1;32m❯\u001b[0;m \r\u001b[6C\u001b[?25h"] +[0.51724, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h"] +[0.517716, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.534221, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.534414, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.68954, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[8C\u001b[?25h"] +[0.823651, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexa\u001b[0;m\r\u001b[9C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"] +[0.925065, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;31mexam\u001b[0;m\r\u001b[10C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[10C\u001b[?25h"] +[0.959447, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[10C\u001b[0;31mp\u001b[0;m\r\u001b[11C\u001b[?25h"] +[0.959545, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[11C\u001b[?25h"] +[1.139069, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[11C\u001b[0;31ml\u001b[0;m\r\u001b[12C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[12C\u001b[?25h"] +[1.24924, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexample\u001b[0;m\r\u001b[13C\u001b[?25h"] +[1.250402, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"] +[1.250993, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"] +[1.251965, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"] +[1.25207, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"] +[1.331976, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[13C \r\u001b[14C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[14C\u001b[?25h"] +[1.722869, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[14Cm\r\u001b[15C\u001b[?25h"] +[1.722939, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[15C\u001b[?25h"] +[1.77182, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[15Co\r\u001b[16C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[16C\u001b[?25h"] +[1.881458, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[16Cdifier \r\u001b[23C\u001b[?25h"] +[2.236164, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23C-\r\u001b[24C\u001b[?25h"] +[2.236256, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[24C\u001b[?25h"] +[2.366244, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[24C-\r\u001b[25C\u001b[?25h"] +[2.366307, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[25C\u001b[?25h"] +[2.569906, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[25Cf\r\u001b[26C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[26C\u001b[?25h"] +[2.676738, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[26Ci\r\u001b[27C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[27C\u001b[?25h"] +[2.725302, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27Cl\r\u001b[28C\u001b[?25h"] +[2.725408, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[28C\u001b[?25h"] +[2.887586, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28Cter\r\u001b[31C\u001b[?25h"] +[3.296656, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23C\u001b[K\u001b[0;4m--filter \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7;34m--filter\u001b[0;2;7m (Filter())\u001b[0;m \u001b[0;34m--filterargs\u001b[0;2m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[4.148108, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[31C\u001b[K\u001b[0;4margs \r\n\r\n\u001b[0;m\u001b[K\u001b[0;34m--filter\u001b[0;2m (Filter())\u001b[0;m \u001b[0;7;34m--filterargs\u001b[0;2;7m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[4.456621, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[23C\u001b[K--filterargs \r\n\u001b[J\u001b[A\r\u001b[36C\u001b[?25h"] +[4.456666, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[36C\u001b[?25h"] +[4.819485, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[36C\u001b[0;4mone \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mone\u001b[0;m three two\u001b[1A\r\u001b[22C\u001b[?25h"] +[5.571394, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[36C\u001b[K\u001b[0;4mthree \r\n\r\n\u001b[0;m\u001b[Kone \u001b[0;7mthree\u001b[0;m two\u001b[1A\r\u001b[22C\u001b[?25h"] +[5.947624, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;4mwo \r\n\r\n\u001b[5C\u001b[0;m\u001b[Kthree \u001b[0;7mtwo\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[6.906531, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[6.906608, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[7.101044, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[7.101149, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[7.540131, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[7.540211, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[8.017748, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[36C\u001b[K\r\n\u001b[J\u001b[A\r\u001b[36C\u001b[?25h"] +[8.333587, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[35C\u001b[K\r\u001b[35C\u001b[?25h"] +[8.504602, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[34C\u001b[K\r\u001b[34C\u001b[?25h"] +[8.64329, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[33C\u001b[K\r\u001b[33C\u001b[?25h"] +[8.7951, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[32C\u001b[K\r\u001b[32C\u001b[?25h"] +[8.940472, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[31C\u001b[K\r\u001b[31C\u001b[?25h"] +[9.102937, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[30C\u001b[K\r\u001b[30C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[30C\u001b[?25h"] +[9.263461, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[29C\u001b[K\r\u001b[29C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[29C\u001b[?25h"] +[9.421295, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28C\u001b[K\r\u001b[28C\u001b[?25h"] +[9.563985, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27C\u001b[K\r\u001b[27C\u001b[?25h"] +[9.718051, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[26C\u001b[K\r\u001b[26C\u001b[?25h"] +[9.88858, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[25C\u001b[K\r\u001b[25C\u001b[?25h"] +[10.037164, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[24C\u001b[K\r\u001b[24C\u001b[?25h"] +[10.204707, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23C\u001b[K\r\u001b[23C\u001b[?25h"] +[10.384287, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23Co\r\u001b[24C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[24C\u001b[?25h"] +[10.513507, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[24Cn\r\u001b[25C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[25C\u001b[?25h"] +[10.586936, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[25Ce\r\u001b[26C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[26C\u001b[?25h"] +[10.75475, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[26C \r\u001b[27C\u001b[?25h"] +[10.754851, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[27C\u001b[?25h"] +[10.896976, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27C-\r\u001b[28C\u001b[?25h"] +[10.897064, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[28C\u001b[?25h"] +[11.048213, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28C-\r\u001b[29C\u001b[?25h"] +[11.048311, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[29C\u001b[?25h"] +[11.161215, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[29Cf\r\u001b[30C\u001b[?25h"] +[11.242127, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[30Ci\r\u001b[31C\u001b[?25h"] +[11.242226, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[31C\u001b[?25h"] +[11.324027, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[31Cl\r\u001b[32C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[32C\u001b[?25h"] +[11.537274, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[32Cter\r\u001b[35C\u001b[?25h"] +[11.924532, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27C\u001b[K\u001b[0;4m--filter \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7;34m--filter\u001b[0;2;7m (Filter())\u001b[0;m \u001b[0;34m--filterargs\u001b[0;2m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[12.56374, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[35C\u001b[K\u001b[0;4margs \r\n\r\n\u001b[0;m\u001b[K\u001b[0;34m--filter\u001b[0;2m (Filter())\u001b[0;m \u001b[0;7;34m--filterargs\u001b[0;2;7m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[12.890301, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[27C\u001b[K--filterargs \r\n\u001b[J\u001b[A\r\u001b[40C\u001b[?25h"] +[12.890385, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[40C\u001b[?25h"] +[13.143496, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[40Ct\r\u001b[41C\u001b[?25h"] +[13.530496, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[40C\u001b[K\u001b[0;4mthree \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mthree\u001b[0;m two\u001b[1A\r\u001b[22C\u001b[?25h"] +[13.53082, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[13.531554, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[13.531692, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[14.819055, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[40C\u001b[Kt\r\n\u001b[J\u001b[A\r\u001b[41C\u001b[?25h"] +[15.260369, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[41C\u001b[?25h"] +[15.260646, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[41C\u001b[?25h"] +[15.424458, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[40C\u001b[K\r\u001b[40C\u001b[?25h"] +[15.55328, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[39C\u001b[K\r\u001b[39C\u001b[?25h"] +[15.716462, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[38C\u001b[K\r\u001b[38C\u001b[?25h"] +[15.880004, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[37C\u001b[K\r\u001b[37C\u001b[?25h"] +[16.027939, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[36C\u001b[K\r\u001b[36C\u001b[?25h"] +[16.171078, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[35C\u001b[K\r\u001b[35C\u001b[?25h"] +[16.311475, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[34C\u001b[K\r\u001b[34C\u001b[?25h"] +[16.461213, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[33C\u001b[K\r\u001b[33C\u001b[?25h"] +[16.601871, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[32C\u001b[K\r\u001b[32C\u001b[?25h"] +[16.746535, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[31C\u001b[K\r\u001b[31C\u001b[?25h"] +[16.890745, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[30C\u001b[K\r\u001b[30C\u001b[?25h"] +[17.030053, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[29C\u001b[K\r\u001b[29C\u001b[?25h"] +[17.183118, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28C\u001b[K\r\u001b[28C\u001b[?25h"] +[17.326949, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27C\u001b[K\r\u001b[27C\u001b[?25h"] +[17.749093, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27Ct\r\u001b[28C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[28C\u001b[?25h"] +[17.83807, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28Cw\r\u001b[29C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[29C\u001b[?25h"] +[17.933865, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[29Co\r\u001b[30C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[30C\u001b[?25h"] +[18.329518, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[30C \r\u001b[31C\u001b[?25h"] +[18.329584, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[31C\u001b[?25h"] +[18.445902, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[31C-\r\u001b[32C\u001b[?25h"] +[18.445979, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[32C\u001b[?25h"] +[18.569703, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[32C-\r\u001b[33C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[33C\u001b[?25h"] +[18.684891, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[33Cf\r\u001b[34C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[34C\u001b[?25h"] +[18.765799, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[34Ci\r\u001b[35C\u001b[?25h"] +[18.7659, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[35C\u001b[?25h"] +[18.868392, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[35Cl\r\u001b[36C\u001b[?25h"] +[18.868465, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[36C\u001b[?25h"] +[18.970671, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[36Cter\r\u001b[39C\u001b[?25h"] +[19.209788, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[31C\u001b[K\u001b[0;4m--filter \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7;34m--filter\u001b[0;2;7m (Filter())\u001b[0;m \u001b[0;34m--filterargs\u001b[0;2m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[19.424769, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[39C\u001b[K\u001b[0;4margs \r\n\r\n\u001b[0;m\u001b[K\u001b[0;34m--filter\u001b[0;2m (Filter())\u001b[0;m \u001b[0;7;34m--filterargs\u001b[0;2;7m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[20.18151, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[31C\u001b[K--filterargs \r\n\u001b[J\u001b[A\r\u001b[44C\u001b[?25h"] +[20.181569, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[44C\u001b[?25h"] +[20.537282, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[44Cthree \r\u001b[50C\u001b[?25h"] +[20.537505, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[50C\u001b[?25h"] +[20.537639, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[50C\u001b[?25h"] +[22.570548, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\r\u001b[6C\u001b[?25h"] +[22.571582, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"] +[22.589133, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"] +[23.127108, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[23.326407, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h"] +[23.4593, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[8C\u001b[0;31mi\u001b[0;m\r\u001b[9C\u001b[?25h"] +[23.459343, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"] +[23.568426, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexit\u001b[0;m\r\u001b[10C\u001b[?25h"] +[23.568628, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[10C\u001b[?25h"] +[23.652474, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\n\r\u001b[?25h\u001b[?7h\u001b[?2004l\r"] diff --git a/docs/src/carapace/action/filterArgs.md b/docs/src/carapace/action/filterArgs.md new file mode 100644 index 000000000..e963560c1 --- /dev/null +++ b/docs/src/carapace/action/filterArgs.md @@ -0,0 +1,15 @@ +# FilterArgs + +[`FilterArgs`] filters `Context.Args`. + +```go +carapace.ActionValues( + "one", + "two", + "three", +).FilterArgs() +``` + +![](./filterArgs.cast) + +[`FilterArgs`]: https://pkg.go.dev/github.com/rsteube/carapace#Action.FilterArgs diff --git a/docs/src/carapace/action/filterParts.cast b/docs/src/carapace/action/filterParts.cast new file mode 100644 index 000000000..cc464676b --- /dev/null +++ b/docs/src/carapace/action/filterParts.cast @@ -0,0 +1,51 @@ +{"version": 2, "width": 108, "height": 24, "timestamp": 1691080404, "env": {"SHELL": "elvish", "TERM": "tmux-256color"}} +[0.092554, "o", "\u001b[?7h\u001b[7m⏎\u001b[m \r \r\u001b[?7l\u001b[?2004h\u001b[?25l\r???> ???> \r\u001b[5C\u001b[?25h"] +[0.093294, "o", "\u001b[?25l\r\u001b[5C\u001b[K\r\u001b[5C\u001b[?25h"] +[0.107801, "o", "\u001b[?25l\r\r\u001b[5C\u001b[?25h\u001b[?25l\r\u001b[K\r\n\u001b[0;1;36mcarapace/example\u001b[0;m on \u001b[0;1;35m action-filter\u001b[0;m \u001b[0;1;31m[$!?]\u001b[0;m via \u001b[0;1;36m🐹 v1.20.6 \r\n\u001b[0;1;37mesh\u001b[0;m \u001b[0;1;32m❯\u001b[0;m \r\u001b[6C\u001b[?25h"] +[0.717477, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h"] +[0.718261, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.718601, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.732971, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.733035, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[0.891989, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[8C\u001b[?25h"] +[1.030322, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexa\u001b[0;m\r\u001b[9C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"] +[1.128147, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;31mexam\u001b[0;m\r\u001b[10C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[10C\u001b[?25h"] +[1.180927, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[10C\u001b[0;31mp\u001b[0;m\r\u001b[11C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[11C\u001b[?25h"] +[1.328944, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[11C\u001b[0;31ml\u001b[0;m\r\u001b[12C\u001b[?25h"] +[1.449192, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexample\u001b[0;m\r\u001b[13C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"] +[1.54703, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[13C \r\u001b[14C\u001b[?25h"] +[1.634043, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[14Cm\r\u001b[15C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[15C\u001b[?25h"] +[1.702533, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[15Co\r\u001b[16C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[16C\u001b[?25h"] +[1.803433, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[16Cdifier \r\u001b[23C\u001b[?25h"] +[2.177973, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23C-\r\u001b[24C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[24C\u001b[?25h"] +[2.314795, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[24C-\r\u001b[25C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[25C\u001b[?25h"] +[2.406759, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[25Cf\r\u001b[26C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[26C\u001b[?25h"] +[2.503458, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[26Ci\r\u001b[27C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[27C\u001b[?25h"] +[2.561262, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[27Cl\r\u001b[28C\u001b[?25h"] +[2.561367, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[28C\u001b[?25h"] +[2.636817, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[28Cter\r\u001b[31C\u001b[?25h"] +[2.823269, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[23C\u001b[K\u001b[0;4m--filter \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7;34m--filter\u001b[0;2;7m (Filter())\u001b[0;m \u001b[0;34m--filterargs\u001b[0;2m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[3.103748, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[22C\u001b[?25h"] +[3.17669, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[31C\u001b[K\u001b[0;4margs \r\n\r\n\u001b[0;m\u001b[K\u001b[0;34m--filter\u001b[0;2m (Filter())\u001b[0;m \u001b[0;7;34m--filterargs\u001b[0;2;7m (FilterArgs())\u001b[0;m \u001b[0;34m--filterparts\u001b[0;2m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[3.506477, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[31C\u001b[K\u001b[0;4mparts \r\n\r\n\u001b[21C\u001b[0;m\u001b[K\u001b[0;34m--filterargs\u001b[0;2m (FilterArgs())\u001b[0;m \u001b[0;7;34m--filterparts\u001b[0;2;7m (FilterParts())\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[3.664665, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[23C\u001b[K--filterparts \r\n\u001b[J\u001b[A\r\u001b[37C\u001b[?25h"] +[3.782685, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[37C\u001b[0;33m'\u001b[0;m\r\u001b[38C\u001b[?25h"] +[4.19607, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;4;33m'one,'\r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mone\u001b[0;m three two\u001b[1A\r\u001b[22C\u001b[?25h"] +[4.701716, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;33m'one,'\u001b[0;m\r\n\u001b[J\u001b[A\r\u001b[43C\u001b[?25h"] +[4.701823, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[43C\u001b[?25h"] +[5.059395, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;4;33m'one,three,'\r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mthree\u001b[0;m two\u001b[1A\r\u001b[22C\u001b[?25h"] +[5.813043, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[43C\u001b[K\u001b[0;4;33mwo,'\r\n\r\n\u001b[0;m\u001b[Kthree \u001b[0;7mtwo\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[6.001911, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;33m'one,two,'\u001b[0;m\r\n\u001b[J\u001b[A\r\u001b[47C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[47C\u001b[?25h"] +[6.317148, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;4;33m'one,two,three,'\r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mthree\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"] +[8.384838, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[37C\u001b[K\u001b[0;33m'one,two,three,'\u001b[0;m\r\n\u001b[J\u001b[A\r\u001b[53C\u001b[?25h"] +[8.384915, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[53C\u001b[?25h"] +[8.845131, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\r\u001b[6C\u001b[?25h"] +[8.845421, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"] +[8.846864, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"] +[8.865538, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"] +[9.112227, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h"] +[9.112288, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"] +[9.308427, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[8C\u001b[?25h"] +[9.433525, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[8C\u001b[0;31mi\u001b[0;m\r\u001b[9C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"] +[9.52154, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexit\u001b[0;m\r\u001b[10C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[10C\u001b[?25h"] +[9.665608, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\n\r\u001b[?25h\u001b[?7h\u001b[?2004l\r"] diff --git a/docs/src/carapace/action/filterParts.md b/docs/src/carapace/action/filterParts.md new file mode 100644 index 000000000..34214417c --- /dev/null +++ b/docs/src/carapace/action/filterParts.md @@ -0,0 +1,17 @@ +# FilterParts + +[`FilterParts`] filters `Context.Parts`. + +```go +carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action { + return carapace.ActionValues( + "one", + "two", + "three", + ).FilterParts() +}) +``` + +![](./filterParts.cast) + +[`FilterParts`]: https://pkg.go.dev/github.com/rsteube/carapace#Action.FilterParts diff --git a/docs/src/carapace/defaultActions/actionMultiParts.md b/docs/src/carapace/defaultActions/actionMultiParts.md index 5037a09e1..a9a7415cd 100644 --- a/docs/src/carapace/defaultActions/actionMultiParts.md +++ b/docs/src/carapace/defaultActions/actionMultiParts.md @@ -39,7 +39,7 @@ carapace.ActionMultiParts(",", func(cEntries carapace.Context) carapace.Action { for index, entry := range cEntries.Parts { keys[index] = strings.Split(entry, "=")[0] } - return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Invoke(c).Filter(keys).Suffix("=").ToA() + return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Filter(keys...).Suffix("=") case 1: switch c.Parts[0] { case "FILE": diff --git a/docs/src/carapace/invokedAction/filter.md b/docs/src/carapace/invokedAction/filter.md index deb2e5e82..d03621e08 100644 --- a/docs/src/carapace/invokedAction/filter.md +++ b/docs/src/carapace/invokedAction/filter.md @@ -5,6 +5,6 @@ E.g. completing a unique list of values in an [ActionMultiParts](../defaultActio ```go carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action { - return carapace.ActionValues("one", "two", "three").Invoke(c).Filter(c.Parts).ToA() + return carapace.ActionValues("one", "two", "three").Invoke(c).Filter(c.Parts...).ToA() } ``` diff --git a/example-nonposix/cmd/root.go b/example-nonposix/cmd/root.go index ffc158593..5b9a87e48 100644 --- a/example-nonposix/cmd/root.go +++ b/example-nonposix/cmd/root.go @@ -45,7 +45,7 @@ func init() { "delim-colon": carapace.ActionValues("d1", "d2", "d3"), "delim-slash": carapace.ActionValues("d1", "d2", "d3"), "nargs-any": carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionValues("na1", "na2", "na3").Invoke(c).Filter(c.Parts).ToA() // only filters current occurrence + return carapace.ActionValues("na1", "na2", "na3").Invoke(c).Filter(c.Parts...).ToA() // only filters current occurrence }), "nargs-two": carapace.ActionCallback(func(c carapace.Context) carapace.Action { switch len(c.Parts) { diff --git a/example/cmd/action.go b/example/cmd/action.go index 4500a5b3c..bd7acc9e0 100644 --- a/example/cmd/action.go +++ b/example/cmd/action.go @@ -110,7 +110,7 @@ func init() { for index, entry := range cEntries.Parts { keys[index] = strings.Split(entry, "=")[0] } - return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Invoke(c).Filter(keys).Suffix("=").ToA() + return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Invoke(c).Filter(keys...).Suffix("=").ToA() case 1: switch c.Parts[0] { case "FILE": diff --git a/example/cmd/modifier.go b/example/cmd/modifier.go index f1c54da81..8efe3b1b2 100644 --- a/example/cmd/modifier.go +++ b/example/cmd/modifier.go @@ -28,6 +28,8 @@ func init() { modifierCmd.Flags().String("chdir", "", "Chdir()") modifierCmd.Flags().String("chdirf", "", "ChdirF()") modifierCmd.Flags().String("filter", "", "Filter()") + modifierCmd.Flags().String("filterargs", "", "FilterArgs()") + modifierCmd.Flags().String("filterparts", "", "FilterParts()") modifierCmd.Flags().String("invoke", "", "Invoke()") modifierCmd.Flags().String("list", "", "List()") modifierCmd.Flags().String("multiparts", "", "MultiParts()") @@ -92,7 +94,19 @@ func init() { "2", "two", "3", "three", "4", "four", - ).Filter([]string{"2", "4"}), + ).Filter("2", "4"), + "filterargs": carapace.ActionValues( + "one", + "two", + "three", + ).FilterArgs(), + "filterparts": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action { + return carapace.ActionValues( + "one", + "two", + "three", + ).FilterParts().Suffix(",") + }), "list": carapace.ActionValues("one", "two", "three").List(","), "invoke": carapace.ActionCallback(func(c carapace.Context) carapace.Action { switch { diff --git a/example/cmd/modifier_test.go b/example/cmd/modifier_test.go index 7c0168aab..c3b211534 100644 --- a/example/cmd/modifier_test.go +++ b/example/cmd/modifier_test.go @@ -159,6 +159,59 @@ func TestPrefix(t *testing.T) { }) } +func TestFilterArgs(t *testing.T) { + sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) { + s.Run("modifier", "--filterargs", ""). + Expect(carapace.ActionValues( + "one", + "two", + "three", + ).Usage("FilterArgs()")) + + s.Run("modifier", "one", "--filterargs", ""). + Expect(carapace.ActionValues( + "two", + "three", + ).Usage("FilterArgs()")) + + s.Run("modifier", "one", "three", "--filterargs", ""). + Expect(carapace.ActionValues( + "two", + ).Usage("FilterArgs()")) + }) +} + +func TestFilterParts(t *testing.T) { + sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) { + s.Run("modifier", "--filterparts", ""). + Expect(carapace.ActionValues( + "one", + "two", + "three", + ).Suffix(","). + NoSpace(','). + Usage("FilterParts()")) + + s.Run("modifier", "--filterparts", "one,"). + Expect(carapace.ActionValues( + "two", + "three", + ).Suffix(","). + NoSpace(','). + Prefix("one,"). + Usage("FilterParts()")) + + s.Run("modifier", "--filterparts", "one,three,"). + Expect(carapace.ActionValues( + "two", + ).Suffix(","). + NoSpace(','). + Prefix("one,three,"). + Usage("FilterParts()")) + + }) +} + func TestMultiPartsP(t *testing.T) { sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) { s.Run("modifier", "--multipartsp", ""). diff --git a/example/cmd/multiparts.go b/example/cmd/multiparts.go index 56d7cf9cd..cf7fc896d 100644 --- a/example/cmd/multiparts.go +++ b/example/cmd/multiparts.go @@ -37,9 +37,7 @@ func init() { "dot": actionMultipartsTest("."), "dotdotdot": actionMultipartsTest("..."), "equals": actionMultipartsTest("="), - "none": carapace.ActionMultiParts("", func(c carapace.Context) carapace.Action { - return carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").Invoke(c).Filter(c.Parts).ToA() - }), + "none": carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").UniqueList(""), "none-zero": carapace.ActionMultiPartsN("", 0, func(c carapace.Context) carapace.Action { return carapace.ActionMessage("unreachable") }), @@ -97,7 +95,7 @@ func init() { for index, entry := range cEntries.Parts { keys[index] = strings.Split(entry, "=")[0] } - return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Invoke(c).Filter(keys).Suffix("=").ToA() + return carapace.ActionValues("FILE", "DIRECTORY", "VALUE").Filter(keys...).Suffix("=") case 1: switch c.Parts[0] { case "FILE": @@ -122,11 +120,11 @@ func actionMultipartsTest(divider string) carapace.Action { return carapace.ActionMultiParts(divider, func(c carapace.Context) carapace.Action { switch len(c.Parts) { case 0: - return actionTestValues().Invoke(c).Suffix(divider).ToA() + return actionTestValues().Suffix(divider) case 1: - return actionTestValues().Invoke(c).Filter(c.Parts).Suffix(divider).ToA() + return actionTestValues().FilterParts().Suffix(divider) case 2: - return actionTestValues().Invoke(c).Filter(c.Parts).ToA() + return actionTestValues().FilterParts() default: return carapace.ActionValues() } diff --git a/invokedAction.go b/invokedAction.go index 21fba4603..dffb51811 100644 --- a/invokedAction.go +++ b/invokedAction.go @@ -21,7 +21,7 @@ func (a InvokedAction) export() export.Export { // // a := carapace.ActionValues("A", "B", "C").Invoke(c) // b := a.Filter([]string{"B"}) // ["A", "C"] -func (a InvokedAction) Filter(values []string) InvokedAction { +func (a InvokedAction) Filter(values ...string) InvokedAction { a.rawValues = a.rawValues.Filter(values...) return a } diff --git a/pkg/traverse/git.go b/pkg/traverse/git.go index c9a0599f6..ad1f6d142 100644 --- a/pkg/traverse/git.go +++ b/pkg/traverse/git.go @@ -4,7 +4,7 @@ import ( "path/filepath" ) -// GitDir is the location of the .git folder. +// GitDir returns the location of the .git folder. func GitDir(tc Context) (string, error) { if dir, ok := tc.LookupEnv("GIT_DIR"); ok { return filepath.ToSlash(dir), nil @@ -12,7 +12,7 @@ func GitDir(tc Context) (string, error) { return Parent(".git")(tc) } -// GitWorkTree is the location of the root of the working directory for a non-bare repository. +// GitWorkTree returns the location of the root of the working directory for a non-bare repository. func GitWorkTree(tc Context) (string, error) { if dir, ok := tc.LookupEnv("GIT_WORK_TREE"); ok { return filepath.ToSlash(dir), nil diff --git a/pkg/traverse/traverse.go b/pkg/traverse/traverse.go index cdfdffbf4..1242a9755 100644 --- a/pkg/traverse/traverse.go +++ b/pkg/traverse/traverse.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/spf13/pflag" ) type Context interface { @@ -14,6 +16,7 @@ type Context interface { LookupEnv(key string) (string, bool) } +// Parent returns the first parent directory containing any of the given names/directories. func Parent(names ...string) func(tc Context) (string, error) { return func(tc Context) (string, error) { wd, err := tc.Abs("") @@ -49,3 +52,13 @@ func traverse(path string, name string) (target string, err error) { } return } + +// Flag returns the value of given flag. +func Flag(f *pflag.Flag) func(tc Context) (string, error) { + return func(tc Context) (string, error) { + if f == nil { + return "", errors.New("invalid argument [traverse.Flag]") + } + return f.Value.String(), nil + } +}