MCPcopy
hub / github.com/spf13/cobra / genFishComp

Function genFishComp

fish_completions.go:25–273  ·  view source on GitHub ↗
(buf io.StringWriter, name string, includeDesc bool)

Source from the content-addressed store, hash-verified

23)
24
25func genFishComp(buf io.StringWriter, name string, includeDesc bool) {
26 // Variables should not contain a '-' or ':' character
27 nameForVar := name
28 nameForVar = strings.ReplaceAll(nameForVar, "-", "_")
29 nameForVar = strings.ReplaceAll(nameForVar, ":", "_")
30
31 compCmd := ShellCompRequestCmd
32 if !includeDesc {
33 compCmd = ShellCompNoDescRequestCmd
34 }
35 WriteStringAndCheck(buf, fmt.Sprintf("# fish completion for %-36s -*- shell-script -*-\n", name))
36 WriteStringAndCheck(buf, fmt.Sprintf(`
37function __%[1]s_debug
38 set -l file "$BASH_COMP_DEBUG_FILE"
39 if test -n "$file"
40 echo "$argv" >> $file
41 end
42end
43
44function __%[1]s_perform_completion
45 __%[1]s_debug "Starting __%[1]s_perform_completion"
46
47 # Extract all args except the last one
48 set -l args (commandline -opc)
49 # Extract the last arg and escape it in case it is a space
50 set -l lastArg (string escape -- (commandline -ct))
51
52 __%[1]s_debug "args: $args"
53 __%[1]s_debug "last arg: $lastArg"
54
55 # Disable ActiveHelp which is not supported for fish shell
56 set -l requestComp "%[10]s=0 $args[1] %[3]s $args[2..-1] $lastArg"
57
58 __%[1]s_debug "Calling $requestComp"
59 set -l results (eval $requestComp 2> /dev/null)
60
61 # Some programs may output extra empty lines after the directive.
62 # Let's ignore them or else it will break completion.
63 # Ref: https://github.com/spf13/cobra/issues/1279
64 for line in $results[-1..1]
65 if test (string trim -- $line) = ""
66 # Found an empty line, remove it
67 set results $results[1..-2]
68 else
69 # Found non-empty line, we have our proper output
70 break
71 end
72 end
73
74 set -l comps $results[1..-2]
75 set -l directiveLine $results[-1]
76
77 # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
78 # completions must be prefixed with the flag
79 set -l flagPrefix (string match -r -- '-.*=' "$lastArg")
80
81 __%[1]s_debug "Comps: $comps"
82 __%[1]s_debug "DirectiveLine: $directiveLine"

Callers 1

GenFishCompletionMethod · 0.85

Calls 2

WriteStringAndCheckFunction · 0.85
activeHelpEnvVarFunction · 0.85

Tested by

no test coverage detected