| 7 | ) |
| 8 | |
| 9 | func newShellCompletionCommand() *cobra.Command { |
| 10 | cmd := &cobra.Command{ |
| 11 | Use: "shell-completion", |
| 12 | Short: "Set up tab completion for your shell", |
| 13 | Annotations: map[string]string{ |
| 14 | "agent_notes": "Subcommands: install, generate. install writes the script where the shell reads it and is the usual way in; generate prints it to stdout for a package or a dotfile to place itself.", |
| 15 | }, |
| 16 | } |
| 17 | |
| 18 | cmd.AddCommand(newCompletionInstallCommand().cmd) |
| 19 | cmd.AddCommand(newCompletionGenerateCommand()) |
| 20 | |
| 21 | return cmd |
| 22 | } |
| 23 | |
| 24 | func newCompletionGenerateCommand() *cobra.Command { |
| 25 | return &cobra.Command{ |
| 26 | Use: "generate <bash|zsh|fish|powershell>", |
| 27 | Short: "Print a completion script to stdout", |
| 28 | Long: `Print hey's completion script for a shell to stdout. |
| 29 | |
| 30 | This is the by-hand route, for a package that places the file itself or a dotfile |
| 31 | that sources it. To just make tab completion work, use hey shell-completion install |
| 32 | — it writes the script where your shell already looks. |
| 33 | |
| 34 | Load it for the current session: |
| 35 | |
| 36 | bash $ source <(hey shell-completion generate bash) |
| 37 | zsh $ source <(hey shell-completion generate zsh) |
| 38 | fish $ hey shell-completion generate fish | source |
| 39 | pwsh PS> hey shell-completion generate powershell | Out-String | Invoke-Expression |
| 40 | |
| 41 | Keep it for every session by writing it where the shell reads completions from. |
| 42 | Those directories differ per shell and per machine, and a file outside them is read |
| 43 | by nothing — hey shell-completion install works out the right one and reports it.`, |
| 44 | Example: ` hey shell-completion generate bash |
| 45 | hey shell-completion generate zsh > "$HOME/.local/share/zsh/site-functions/_hey"`, |
| 46 | Args: cobra.ExactArgs(1), |
| 47 | ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 48 | RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | root := cmd.Root() |
| 50 | switch args[0] { |
| 51 | case "bash": |
| 52 | return root.GenBashCompletion(cmd.OutOrStdout()) |
| 53 | case "zsh": |
| 54 | return root.GenZshCompletion(cmd.OutOrStdout()) |
| 55 | case "fish": |
| 56 | return root.GenFishCompletion(cmd.OutOrStdout(), true) |
| 57 | case "powershell": |
| 58 | return root.GenPowerShellCompletionWithDesc(cmd.OutOrStdout()) |
| 59 | default: |
| 60 | return apierr.ErrUsage("unsupported shell: " + args[0]) |
| 61 | } |
| 62 | }, |
| 63 | } |
| 64 | } |