Skip to content
Myr

Fish does not accept - or : in vars (#1122)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #1121. This is for programs that may contain a : or - in their name.

authored by Marc Khouzam · committed by GitHub · parent 5d52907 · Browse files · 3 files+88 added−46 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Diff stats

3 files+88 added−46 removed

  • other:+88 added−46 removed

3 files+88 added−46 removed

modified:custom_completions_test.go-33 removed
1405 unmodified lines
14061407 check(t, output, ShellCompNoDescRequestCmd)1408}14091410func TestCompleteNoDesCmdInFishScript(t *testing.T) {1411 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1412 child := &Command{1413 Use: "child",1414 ValidArgsFunction: validArgsFunc,1415 Run: emptyRun,1416 }1417 rootCmd.AddCommand(child)14181419 buf := new(bytes.Buffer)1420 rootCmd.GenFishCompletion(buf, false)1421 output := buf.String()14221423 check(t, output, ShellCompNoDescRequestCmd)1424}14251426func TestCompleteCmdInFishScript(t *testing.T) {1427 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1428 child := &Command{1429 Use: "child",1430 ValidArgsFunction: validArgsFunc,1431 Run: emptyRun,1432 }1433 rootCmd.AddCommand(child)14341435 buf := new(bytes.Buffer)1436 rootCmd.GenFishCompletion(buf, true)1437 output := buf.String()14381439 check(t, output, ShellCompRequestCmd)1440 checkOmit(t, output, ShellCompNoDescRequestCmd)1441}14421410func TestCompleteNoDesCmdInZshScript(t *testing.T) {1411 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1412 child := &Command{1413 Use: "child",
487 unmodified lines
added:fish_completions_test.go+69 added
1package cobra23import (4 "bytes"5 "testing"6)78func TestCompleteNoDesCmdInFishScript(t *testing.T) {9 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}10 child := &Command{11 Use: "child",12 ValidArgsFunction: validArgsFunc,13 Run: emptyRun,14 }15 rootCmd.AddCommand(child)1617 buf := new(bytes.Buffer)18 rootCmd.GenFishCompletion(buf, false)19 output := buf.String()2021 check(t, output, ShellCompNoDescRequestCmd)22}2324func TestCompleteCmdInFishScript(t *testing.T) {25 rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}26 child := &Command{27 Use: "child",28 ValidArgsFunction: validArgsFunc,29 Run: emptyRun,30 }31 rootCmd.AddCommand(child)3233 buf := new(bytes.Buffer)34 rootCmd.GenFishCompletion(buf, true)35 output := buf.String()3637 check(t, output, ShellCompRequestCmd)38 checkOmit(t, output, ShellCompNoDescRequestCmd)39}4041func TestProgWithDash(t *testing.T) {42 rootCmd := &Command{Use: "root-dash", Args: NoArgs, Run: emptyRun}43 buf := new(bytes.Buffer)44 rootCmd.GenFishCompletion(buf, false)45 output := buf.String()4647 // Functions name should have replace the '-'48 check(t, output, "__root_dash_perform_completion")49 checkOmit(t, output, "__root-dash_perform_completion")5051 // The command name should not have replaced the '-'52 check(t, output, "-c root-dash")53 checkOmit(t, output, "-c root_dash")54}5556func TestProgWithColon(t *testing.T) {57 rootCmd := &Command{Use: "root:colon", Args: NoArgs, Run: emptyRun}58 buf := new(bytes.Buffer)59 rootCmd.GenFishCompletion(buf, false)60 output := buf.String()6162 // Functions name should have replace the ':'63 check(t, output, "__root_colon_perform_completion")64 checkOmit(t, output, "__root:colon_perform_completion")6566 // The command name should not have replaced the ':'67 check(t, output, "-c root:colon")68 checkOmit(t, output, "-c root_colon")69}
modified:fish_completions.go+19 added-13 removed
3 unmodified lines
4 "bytes"5 "fmt"6 "io"7 "os"8 "strings"9)1011func genFishComp(buf *bytes.Buffer, name string, includeDesc bool) {12 // Variables should not contain a '-' or ':' character13 nameForVar := name14 nameForVar = strings.Replace(nameForVar, "-", "_", -1)15 nameForVar = strings.Replace(nameForVar, ":", "_", -1)1617 compCmd := ShellCompRequestCmd18 if !includeDesc {19 compCmd = ShellCompNoDescRequestCmd20 }
22 unmodified lines
43 end44 __%[1]s_debug "emptyArg: $emptyArg"4546 if not type -q "$args[1]"41 # This can happen when "complete --do-complete %[1]s" is called when running this script.47 # This can happen when "complete --do-complete %[2]s" is called when running this script.48 __%[1]s_debug "Cannot find $args[1]. No completions."49 return50 end5146 set requestComp "$args[1] %[2]s $args[2..-1] $emptyArg"52 set requestComp "$args[1] %[3]s $args[2..-1] $emptyArg"53 __%[1]s_debug "Calling $requestComp"5455 set results (eval $requestComp 2> /dev/null)56 set comps $results[1..-2]
47 unmodified lines
104105 __%[1]s_debug "Completions are: $__%[1]s_comp_results"106 __%[1]s_debug "Directive is: $directive"107102 set shellCompDirectiveError %[3]d103 set shellCompDirectiveNoSpace %[4]d104 set shellCompDirectiveNoFileComp %[5]d105 set shellCompDirectiveFilterFileExt %[6]d106 set shellCompDirectiveFilterDirs %[7]d108 set shellCompDirectiveError %[4]d109 set shellCompDirectiveNoSpace %[5]d110 set shellCompDirectiveNoFileComp %[6]d111 set shellCompDirectiveFilterFileExt %[7]d112 set shellCompDirectiveFilterDirs %[8]d113114 if test -z "$directive"115 set directive 0116 end
46 unmodified lines
163# Since Fish completions are only loaded once the user triggers them, we trigger them ourselves164# so we can properly delete any completions provided by another script.165# The space after the the program name is essential to trigger completion for the program166# and not completion of the program name itself.161complete --do-complete "%[1]s " &> /dev/null167complete --do-complete "%[2]s " &> /dev/null168169# Remove any pre-existing completions for the program since we will be handling all of them.164complete -c %[1]s -e170complete -c %[2]s -e171172# The order in which the below two lines are defined is very important so that __%[1]s_prepare_completions173# is called first. It is __%[1]s_prepare_completions that sets up the __%[1]s_comp_do_file_comp variable.174#175# This completion will be run second as complete commands are added FILO.176# It triggers file completion choices when __%[1]s_comp_do_file_comp is set.171complete -c %[1]s -n 'set --query __%[1]s_comp_do_file_comp'177complete -c %[2]s -n 'set --query __%[1]s_comp_do_file_comp'178179# This completion will be run first as complete commands are added FILO.174# The call to __%[1]s_prepare_completions will setup both __%[1]s_comp_results abd __%[1]s_comp_do_file_comp.180# The call to __%[1]s_prepare_completions will setup both __%[1]s_comp_results and __%[1]s_comp_do_file_comp.181# It provides the program's completion choices.176complete -c %[1]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'182complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'183178`, name, compCmd,184`, nameForVar, name, compCmd,185 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,186 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))187}188
18 unmodified lines