From 62e81d94a9073e3faf28e78bae5cfe639559448c Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 3 May 2020 02:20:59 +0200 Subject: [PATCH] small cleanup regarding snippet invocation --- action.go | 8 ++++++++ carapace.go | 44 +++++++++++++------------------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/action.go b/action.go index d031b8194..404ec96f8 100644 --- a/action.go +++ b/action.go @@ -59,6 +59,14 @@ func (a Action) Value(shell string) string { } } +func (m *ActionMap) Shell(shell string) map[string]string { + actions := make(map[string]string, len(completions.actions)) + for key, value := range completions.actions { + actions[key] = value.Value(shell) + } + return actions +} + // ActionCallback invokes a go function during completion func ActionCallback(callback CompletionCallback) Action { return Action{Callback: callback} diff --git a/carapace.go b/carapace.go index 1b1c99990..67a78c096 100644 --- a/carapace.go +++ b/carapace.go @@ -59,43 +59,23 @@ func (c Carapace) FlagCompletion(actions ActionMap) { } func (c Carapace) Bash() string { - actions := make(map[string]string, len(completions.actions)) - for key, value := range completions.actions { - actions[key] = value.Bash - } - return bash.Snippet(c.cmd.Root(), actions) + return c.Snippet("bash") } func (c Carapace) Elvish() string { - actions := make(map[string]string, len(completions.actions)) - for key, value := range completions.actions { - actions[key] = value.Elvish - } - return elvish.Snippet(c.cmd.Root(), actions) + return c.Snippet("elvish") } func (c Carapace) Fish() string { - actions := make(map[string]string, len(completions.actions)) - for key, value := range completions.actions { - actions[key] = value.Fish - } - return fish.Snippet(c.cmd.Root(), actions) + return c.Snippet("fish") } func (c Carapace) Powershell() string { - actions := make(map[string]string, len(completions.actions)) - for key, value := range completions.actions { - actions[key] = value.Powershell - } - return powershell.Snippet(c.cmd.Root(), actions) + return c.Snippet("powershell") } func (c Carapace) Zsh() string { - actions := make(map[string]string, len(completions.actions)) - for key, value := range completions.actions { - actions[key] = value.Zsh - } - return zsh.Snippet(c.cmd.Root(), actions) + return c.Snippet("zsh") } func (c Carapace) Standalone() { @@ -108,20 +88,22 @@ func (c Carapace) Standalone() { } func (c Carapace) Snippet(shell string) string { + var snippet func(cmd *cobra.Command, actions map[string]string) string switch shell { case "bash": - return c.Bash() + snippet = bash.Snippet case "elvish": - return c.Elvish() + snippet = elvish.Snippet case "fish": - return c.Fish() + snippet = fish.Snippet case "powershell": - return c.Powershell() + snippet = powershell.Snippet case "zsh": - return c.Zsh() + snippet = zsh.Snippet default: - return fmt.Sprintf("expected 'bash', 'fish', 'powershell' or 'zsh' [was: %v]", shell) + return fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'powershell' or 'zsh' [was: %v]", shell) } + return snippet(c.cmd.Root(), completions.actions.Shell(shell)) } var completions = Completions{