| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "strings" |
| 8 | "text/template" |
| 9 | ) |
| 10 | |
| 11 | // ToFishCompletion creates a fish completion string for the `*Command` |
| 12 | // The function errors if either parsing or writing of the string fails. |
| 13 | func (cmd *Command) ToFishCompletion() (string, error) { |
| 14 | var w bytes.Buffer |
| 15 | if err := cmd.writeFishCompletionTemplate(&w); err != nil { |
| 16 | return "", err |
| 17 | } |
| 18 | return w.String(), nil |
| 19 | } |
| 20 | |
| 21 | type fishCommandCompletionTemplate struct { |
| 22 | Command *Command |
| 23 | Completions []string |
| 24 | AllCommands []string |
| 25 | } |
| 26 | |
| 27 | func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error { |
| 28 | const name = "cli" |
| 29 | t, err := template.New(name).Parse(FishCompletionTemplate) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | // Add global flags |
| 35 | completions := prepareFishFlags(cmd.Name, cmd) |
| 36 | |
| 37 | if cmd.ShellComplete != nil { |
| 38 | var completion strings.Builder |
| 39 | fmt.Fprintf(&completion, |
| 40 | "complete -c %s -n '%s' -xa '(%s %s 2>/dev/null)'", |
| 41 | cmd.Name, |
| 42 | fishFlagHelper(cmd.Name, cmd), |
| 43 | cmd.Name, |
| 44 | completionFlag, |
| 45 | ) |
| 46 | completions = append(completions, completion.String()) |
| 47 | } |
| 48 | |
| 49 | // Add commands and their flags |
| 50 | completions = append( |
| 51 | completions, |
| 52 | prepareFishCommands(cmd.Name, cmd)..., |
| 53 | ) |
| 54 | |
| 55 | toplevelCommandNames := []string{} |
| 56 | for _, child := range cmd.Commands { |
| 57 | toplevelCommandNames = append(toplevelCommandNames, child.Names()...) |
| 58 | } |
| 59 | |
| 60 | return t.ExecuteTemplate(w, name, &fishCommandCompletionTemplate{ |
| 61 | Command: cmd, |
| 62 | Completions: completions, |
| 63 | AllCommands: toplevelCommandNames, |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | func prepareFishCommands(binary string, parent *Command) []string { |
| 68 | commands := parent.Commands |
| 69 | completions := []string{} |
| 70 | for _, command := range commands { |
| 71 | if !command.Hidden { |
| 72 | var completion strings.Builder |
| 73 | fmt.Fprintf(&completion, |
| 74 | "complete -x -c %s -n '%s' -a '%s'", |
| 75 | binary, |
| 76 | fishSubcommandHelper(binary, parent, commands), |
| 77 | command.Name, |
| 78 | ) |
| 79 | |
| 80 | if command.Usage != "" { |
| 81 | fmt.Fprintf(&completion, |
| 82 | " -d '%s'", |
| 83 | escapeSingleQuotes(command.Usage)) |
| 84 | } |
| 85 | completions = append(completions, completion.String()) |
| 86 | } |
| 87 | |
| 88 | if command.ShellComplete != nil { |
| 89 | var completion strings.Builder |
| 90 | var path []string |
| 91 | lineage := command.Lineage() |
| 92 | for i := len(lineage) - 2; i >= 0; i-- { |
| 93 | path = append(path, lineage[i].Name) |
| 94 | } |
| 95 | |
| 96 | fmt.Fprintf(&completion, |
| 97 | "complete -c %s -n '%s' -xa '(%s %s %s 2>/dev/null)'", |
| 98 | binary, |
| 99 | fishFlagHelper(binary, command), |
| 100 | binary, |
| 101 | strings.Join(path, " "), |
| 102 | completionFlag, |
| 103 | ) |
| 104 | completions = append(completions, completion.String()) |
| 105 | } |
| 106 | |
| 107 | completions = append( |
| 108 | completions, |
| 109 | prepareFishFlags(binary, command)..., |
| 110 | ) |
| 111 | |
| 112 | // recursively iterate subcommands |
| 113 | completions = append( |
| 114 | completions, |
| 115 | prepareFishCommands(binary, command)..., |
| 116 | ) |
| 117 | } |
| 118 | |
| 119 | return completions |
| 120 | } |
| 121 | |
| 122 | func prepareFishFlags(binary string, owner *Command) []string { |
| 123 | flags := owner.VisibleFlags() |
| 124 | completions := []string{} |
| 125 | for _, f := range flags { |
| 126 | completion := &strings.Builder{} |
| 127 | fmt.Fprintf(completion, |
| 128 | "complete -c %s -n '%s'", |
| 129 | binary, |
| 130 | fishFlagHelper(binary, owner), |
| 131 | ) |
| 132 | |
| 133 | fishAddFileFlag(f, completion) |
| 134 | |
| 135 | for idx, opt := range f.Names() { |
| 136 | if idx == 0 { |
| 137 | fmt.Fprintf(completion, |
| 138 | " -l %s", strings.TrimSpace(opt), |
| 139 | ) |
| 140 | } else { |
| 141 | fmt.Fprintf(completion, |
| 142 | " -s %s", strings.TrimSpace(opt), |
| 143 | ) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if flag, ok := f.(DocGenerationFlag); ok { |
| 148 | if flag.TakesValue() { |
| 149 | completion.WriteString(" -r") |
| 150 | } |
| 151 | |
| 152 | if flag.GetUsage() != "" { |
| 153 | fmt.Fprintf(completion, |
| 154 | " -d '%s'", |
| 155 | escapeSingleQuotes(flag.GetUsage())) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | completions = append(completions, completion.String()) |
| 160 | } |
| 161 | |
| 162 | return completions |
| 163 | } |
| 164 | |
| 165 | func fishAddFileFlag(flag Flag, completion *strings.Builder) { |
| 166 | switch f := flag.(type) { |
| 167 | case *StringFlag: |
| 168 | if f.TakesFile { |
| 169 | return |
| 170 | } |
| 171 | case *StringSliceFlag: |
| 172 | if f.TakesFile { |
| 173 | return |
| 174 | } |
| 175 | } |
| 176 | completion.WriteString(" -f") |
| 177 | } |
| 178 | |
| 179 | func fishSubcommandHelper(binary string, command *Command, siblings []*Command) string { |
| 180 | fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", binary) |
| 181 | if len(command.Lineage()) > 1 { |
| 182 | var siblingNames []string |
| 183 | for _, sibling := range siblings { |
| 184 | siblingNames = append(siblingNames, sibling.Names()...) |
| 185 | } |
| 186 | ancestry := commandAncestry(command) |
| 187 | fishHelper = fmt.Sprintf( |
| 188 | "%s; and not __fish_seen_subcommand_from %s", |
| 189 | ancestry, |
| 190 | strings.Join(siblingNames, " "), |
| 191 | ) |
| 192 | } |
| 193 | return fishHelper |
| 194 | } |
| 195 | |
| 196 | func fishFlagHelper(binary string, command *Command) string { |
| 197 | fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", binary) |
| 198 | if len(command.Lineage()) > 1 { |
| 199 | fishHelper = commandAncestry(command) |
| 200 | } |
| 201 | return fishHelper |
| 202 | } |
| 203 | |
| 204 | func commandAncestry(command *Command) string { |
| 205 | var ancestry []string |
| 206 | ancestors := command.Lineage() |
| 207 | for i := len(ancestors) - 2; i >= 0; i-- { |
| 208 | ancestry = append( |
| 209 | ancestry, |
| 210 | fmt.Sprintf( |
| 211 | "__fish_seen_subcommand_from %s", |
| 212 | strings.Join(ancestors[i].Names(), " "), |
| 213 | ), |
| 214 | ) |
| 215 | } |
| 216 | return strings.Join(ancestry, "; and ") |
| 217 | } |
| 218 | |
| 219 | func escapeSingleQuotes(input string) string { |
| 220 | return strings.ReplaceAll(input, `'`, `\'`) |
| 221 | } |