(buf io.StringWriter, name string, includeDesc bool)
| 23 | ) |
| 24 | |
| 25 | func 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(` |
| 37 | function __%[1]s_debug |
| 38 | set -l file "$BASH_COMP_DEBUG_FILE" |
| 39 | if test -n "$file" |
| 40 | echo "$argv" >> $file |
| 41 | end |
| 42 | end |
| 43 | |
| 44 | function __%[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" |
no test coverage detected