Skip to content

Commit

Permalink
Merge pull request #42 from rsteube/cleanup-snippet-invocation
Browse files Browse the repository at this point in the history
small cleanup regarding snippet invocation
  • Loading branch information
rsteube authored May 3, 2020
2 parents 0cd5fbc + 62e81d9 commit 48d1cc2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
8 changes: 8 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
44 changes: 13 additions & 31 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{
Expand Down

0 comments on commit 48d1cc2

Please sign in to comment.