Skip to content

Commit

Permalink
added prefix to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Mar 26, 2020
1 parent 43197d8 commit 776c37f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
7 changes: 4 additions & 3 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/rsteube/carapace/bash"
"github.com/rsteube/carapace/fish"
"github.com/rsteube/carapace/zsh"
"github.com/spf13/cobra"
)

type Action struct {
Expand All @@ -16,13 +17,13 @@ type ActionMap map[string]Action
type CompletionCallback func(args []string) Action

// finalize replaces value if a callback function is set
func (a Action) finalize(uid string) Action {
func (a Action) finalize(cmd *cobra.Command, uid string) Action {
if a.Callback != nil {
if a.Bash == "" {
a.Bash = bash.Callback(uid)
a.Bash = bash.Callback(cmd.Root().Name(), uid)
}
if a.Fish == "" {
a.Fish = fish.Callback(uid)
a.Fish = fish.Callback(cmd.Root().Name(), uid)
}
if a.Zsh == "" {
a.Zsh = zsh.Callback(uid)
Expand Down
4 changes: 2 additions & 2 deletions bash/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

func Callback(uid string) string {
return fmt.Sprintf(`eval $(_callback '%v')`, uid) // TODO update and use ActionExecute for eval?
func Callback(prefix string, uid string) string {
return fmt.Sprintf(`eval $(_%v_callback '%v')`, prefix, uid)
}

func ActionExecute(command string) string {
Expand Down
28 changes: 14 additions & 14 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c Completions) GenerateZshFunctions(cmd *cobra.Command) string {

//fish
func (c Completions) GenerateFish(cmd *cobra.Command) string {
result := fmt.Sprintf(`function _state
result := fmt.Sprintf(`function _%v_state
set -lx CURRENT (commandline -cp)
if [ "$LINE" != "$CURRENT" ]
set -gx LINE (commandline -cp)
Expand All @@ -113,13 +113,13 @@ func (c Completions) GenerateFish(cmd *cobra.Command) string {
[ "$STATE" = "$argv" ]
end
function _callback
function _%v_callback
set -lx CALLBACK (commandline -cp | sed "s/ \$/ _/" | xargs %v _carapace fish $argv )
eval "$CALLBACK"
end
complete -c %v -f
`, cmd.Name(), cmd.Name(), cmd.Name())
`, cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name())
result += c.GenerateFishFunctions(cmd)

return result
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c Completions) GenerateFishFunctions(cmd *cobra.Command) string {
if cmd.HasSubCommands() {
positionals = []string{}
for _, subcmd := range cmd.Commands() {
positionals = append(positionals, fmt.Sprintf(`complete -c %v -f -n '_state %v ' -a %v -d '%v'`, cmd.Root().Name(), uid.Command(cmd), subcmd.Name(), subcmd.Short))
positionals = append(positionals, fmt.Sprintf(`complete -c %v -f -n '_%v_state %v ' -a %v -d '%v'`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), subcmd.Name(), subcmd.Short))
// TODO repeat for aliases
// TODO filter hidden
}
Expand All @@ -160,7 +160,7 @@ func (c Completions) GenerateFishFunctions(cmd *cobra.Command) string {
if cmd.ValidArgs != nil {
//positionals = []string{" " + snippetPositionalCompletion(1, ActionValues(cmd.ValidArgs...))}
}
positionals = append(positionals, fmt.Sprintf(`complete -c %v -f -n '_state %v' -a '(_callback _)'`, cmd.Root().Name(), uid.Command(cmd)))
positionals = append(positionals, fmt.Sprintf(`complete -c %v -f -n '_%v_state %v' -a '(_%v_callback _)'`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), cmd.Root().Name()))
}
}

Expand All @@ -181,12 +181,12 @@ func (c Completions) GenerateFishFunctions(cmd *cobra.Command) string {
// bash
func (c Completions) GenerateBash(cmd *cobra.Command) string {
result := fmt.Sprintf(`#!/bin/bash
_%v_completions() {
_callback() {
local compline="${COMP_LINE:0:${COMP_POINT}}"
echo "$compline" | sed "s/ \$/ _/" | xargs %v _carapace bash "$1"
}
_%v_callback() {
local compline="${COMP_LINE:0:${COMP_POINT}}"
echo "$compline" | sed "s/ \$/ _/" | xargs %v _carapace bash "$1"
}
_%v_completions() {
local compline="${COMP_LINE:0:${COMP_POINT}}"
local state=$(echo "$compline" | sed "s/ \$/ _/" | xargs %v _carapace bash state)
local last="${COMP_WORDS[${COMP_CWORD}]}"
Expand All @@ -198,7 +198,7 @@ _%v_completions() {
}
complete -F _%v_completions %v
`, cmd.Name(), cmd.Name(), cmd.Name(), c.GenerateBashFunctions(cmd), cmd.Name(), cmd.Name())
`, cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name(), c.GenerateBashFunctions(cmd), cmd.Name(), cmd.Name())

return result
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (c Completions) GenerateBashFunctions(cmd *cobra.Command) string {

result := make([]string, 0)
// uid.Command, flagList, genflagcompletions, commandargumentcompletion
result = append(result, fmt.Sprintf(function_pattern, uid.Command(cmd), bash.SnippetFlagList(cmd.LocalFlags()), strings.Join(flags, "\n"), bash.Callback("_")))
result = append(result, fmt.Sprintf(function_pattern, uid.Command(cmd), bash.SnippetFlagList(cmd.LocalFlags()), strings.Join(flags, "\n"), bash.Callback(cmd.Root().Name(), "_")))
for _, subcmd := range cmd.Commands() {
if !subcmd.Hidden {
result = append(result, c.GenerateBashFunctions(subcmd))
Expand Down Expand Up @@ -282,14 +282,14 @@ func Gen(cmd *cobra.Command) *Carapace {

func (zsh Carapace) PositionalCompletion(action ...Action) {
for index, a := range action {
completions.actions[uid.Positional(zsh.cmd, index+1)] = a.finalize(uid.Positional(zsh.cmd, index+1))
completions.actions[uid.Positional(zsh.cmd, index+1)] = a.finalize(zsh.cmd, uid.Positional(zsh.cmd, index+1))
}
}

func (zsh Carapace) FlagCompletion(actions ActionMap) {
for name, action := range actions {
flag := zsh.cmd.Flag(name) // TODO only allowed for local flags
completions.actions[uid.Flag(zsh.cmd, flag)] = action.finalize(uid.Flag(zsh.cmd, flag))
completions.actions[uid.Flag(zsh.cmd, flag)] = action.finalize(zsh.cmd, uid.Flag(zsh.cmd, flag))
}
}

Expand Down
4 changes: 2 additions & 2 deletions fish/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

func Callback(uid string) string {
return ActionExecute(fmt.Sprintf(`_callback %v`, uid))
func Callback(prefix string, uid string) string {
return ActionExecute(fmt.Sprintf(`_%v_callback %v`, prefix, uid))
}

func ActionExecute(command string) string {
Expand Down
4 changes: 2 additions & 2 deletions fish/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func SnippetFlagCompletion(cmd *cobra.Command, flag *pflag.Flag, action *string)
}

if flag.Shorthand == "" { // no shorthannd
snippet = fmt.Sprintf(`complete -c %v -f -n '_state %v' -l %v -d '%v'%v`, cmd.Root().Name(), uid.Command(cmd), flag.Name, replacer.Replace(flag.Usage), suffix)
snippet = fmt.Sprintf(`complete -c %v -f -n '_%v_state %v' -l %v -d '%v'%v`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), flag.Name, replacer.Replace(flag.Usage), suffix)
} else {
snippet = fmt.Sprintf(`complete -c %v -f -n '_state %v' -l %v -s %v -d '%v'%v`, cmd.Root().Name(), uid.Command(cmd), flag.Name, flag.Shorthand, replacer.Replace(flag.Usage), suffix)
snippet = fmt.Sprintf(`complete -c %v -f -n '_%v_state %v' -l %v -s %v -d '%v'%v`, cmd.Root().Name(), cmd.Root().Name(), uid.Command(cmd), flag.Name, flag.Shorthand, replacer.Replace(flag.Usage), suffix)
}
return
}
Expand Down

0 comments on commit 776c37f

Please sign in to comment.