|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/bborn/workflow/internal/db" |
| 10 | +) |
| 11 | + |
| 12 | +// newCompletionCmd creates the completion command with subcommands for each shell. |
| 13 | +func newCompletionCmd(rootCmd *cobra.Command) *cobra.Command { |
| 14 | + completionCmd := &cobra.Command{ |
| 15 | + Use: "completion [bash|zsh|fish|powershell]", |
| 16 | + Short: "Generate shell completion scripts", |
| 17 | + Long: `Generate shell completion scripts for ty. |
| 18 | +
|
| 19 | +To load completions: |
| 20 | +
|
| 21 | +Bash: |
| 22 | + $ source <(ty completion bash) |
| 23 | +
|
| 24 | + # To load completions for each session, execute once: |
| 25 | + # Linux: |
| 26 | + $ ty completion bash > /etc/bash_completion.d/ty |
| 27 | + # macOS: |
| 28 | + $ ty completion bash > $(brew --prefix)/etc/bash_completion.d/ty |
| 29 | +
|
| 30 | +Zsh: |
| 31 | + # If shell completion is not already enabled in your environment, |
| 32 | + # you will need to enable it. You can execute the following once: |
| 33 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 34 | +
|
| 35 | + # To load completions for each session, execute once: |
| 36 | + $ ty completion zsh > "${fpath[1]}/_ty" |
| 37 | +
|
| 38 | + # You will need to start a new shell for this setup to take effect. |
| 39 | +
|
| 40 | +Fish: |
| 41 | + $ ty completion fish | source |
| 42 | +
|
| 43 | + # To load completions for each session, execute once: |
| 44 | + $ ty completion fish > ~/.config/fish/completions/ty.fish |
| 45 | +
|
| 46 | +PowerShell: |
| 47 | + PS> ty completion powershell | Out-String | Invoke-Expression |
| 48 | +
|
| 49 | + # To load completions for every new session, run: |
| 50 | + PS> ty completion powershell > ty.ps1 |
| 51 | + # and source this file from your PowerShell profile. |
| 52 | +`, |
| 53 | + DisableFlagsInUseLine: true, |
| 54 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 55 | + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 56 | + Run: func(cmd *cobra.Command, args []string) { |
| 57 | + switch args[0] { |
| 58 | + case "bash": |
| 59 | + rootCmd.GenBashCompletion(os.Stdout) |
| 60 | + case "zsh": |
| 61 | + rootCmd.GenZshCompletion(os.Stdout) |
| 62 | + case "fish": |
| 63 | + rootCmd.GenFishCompletion(os.Stdout, true) |
| 64 | + case "powershell": |
| 65 | + rootCmd.GenPowerShellCompletionWithDesc(os.Stdout) |
| 66 | + } |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + return completionCmd |
| 71 | +} |
| 72 | + |
| 73 | +// completeTaskIDs returns a completion function that suggests task IDs with their titles. |
| 74 | +func completeTaskIDs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 75 | + if len(args) >= 1 { |
| 76 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 77 | + } |
| 78 | + return fetchTaskCompletions(toComplete) |
| 79 | +} |
| 80 | + |
| 81 | +// completeTaskIDsThenStatus completes task ID for first arg, status for second. |
| 82 | +func completeTaskIDsThenStatus(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 83 | + if len(args) == 0 { |
| 84 | + return fetchTaskCompletions(toComplete) |
| 85 | + } |
| 86 | + if len(args) == 1 { |
| 87 | + return validStatuses(), cobra.ShellCompDirectiveNoFileComp |
| 88 | + } |
| 89 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 90 | +} |
| 91 | + |
| 92 | +// completeTaskIDsThenProject completes task ID for first arg, project name for second. |
| 93 | +func completeTaskIDsThenProject(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 94 | + if len(args) == 0 { |
| 95 | + return fetchTaskCompletions(toComplete) |
| 96 | + } |
| 97 | + if len(args) == 1 { |
| 98 | + return fetchProjectCompletions() |
| 99 | + } |
| 100 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 101 | +} |
| 102 | + |
| 103 | +// completeProjectNames returns a completion function for project names. |
| 104 | +func completeProjectNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 105 | + if len(args) >= 1 { |
| 106 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 107 | + } |
| 108 | + return fetchProjectCompletions() |
| 109 | +} |
| 110 | + |
| 111 | +// completeSettingKeys returns a completion function for settings set first arg. |
| 112 | +func completeSettingKeys(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 113 | + if len(args) == 0 { |
| 114 | + return []string{ |
| 115 | + "anthropic_api_key\tAPI key for ghost text autocomplete", |
| 116 | + "autocomplete_enabled\tEnable/disable ghost text (true/false)", |
| 117 | + "idle_suspend_timeout\tIdle timeout before suspending (e.g. 6h)", |
| 118 | + }, cobra.ShellCompDirectiveNoFileComp |
| 119 | + } |
| 120 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 121 | +} |
| 122 | + |
| 123 | +// completeTypeNames returns a completion function for task type names. |
| 124 | +func completeTypeNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 125 | + if len(args) >= 1 { |
| 126 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 127 | + } |
| 128 | + return fetchTypeCompletions() |
| 129 | +} |
| 130 | + |
| 131 | +// completeFlagProjects provides completions for --project flag values. |
| 132 | +func completeFlagProjects(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 133 | + return fetchProjectCompletions() |
| 134 | +} |
| 135 | + |
| 136 | +// completeFlagExecutors provides completions for --executor flag values. |
| 137 | +func completeFlagExecutors(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 138 | + return []string{ |
| 139 | + "claude\tAnthropic Claude (default)", |
| 140 | + "codex\tOpenAI Codex", |
| 141 | + "gemini\tGoogle Gemini", |
| 142 | + "pi\tInflection Pi", |
| 143 | + "opencode\tOpenCode", |
| 144 | + "openclaw\tOpenClaw", |
| 145 | + }, cobra.ShellCompDirectiveNoFileComp |
| 146 | +} |
| 147 | + |
| 148 | +// completeFlagTypes provides completions for --type flag values. |
| 149 | +func completeFlagTypes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 150 | + comps, directive := fetchTypeCompletions() |
| 151 | + if len(comps) > 0 { |
| 152 | + return comps, directive |
| 153 | + } |
| 154 | + // Fallback to built-in types if DB unavailable |
| 155 | + return []string{"code", "writing", "thinking"}, cobra.ShellCompDirectiveNoFileComp |
| 156 | +} |
| 157 | + |
| 158 | +// completeFlagStatuses provides completions for --status flag values. |
| 159 | +func completeFlagStatuses(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 160 | + return validStatuses(), cobra.ShellCompDirectiveNoFileComp |
| 161 | +} |
| 162 | + |
| 163 | +// fetchTaskCompletions opens the DB and returns task ID completions. |
| 164 | +func fetchTaskCompletions(toComplete string) ([]string, cobra.ShellCompDirective) { |
| 165 | + database, err := db.Open(db.DefaultPath()) |
| 166 | + if err != nil { |
| 167 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 168 | + } |
| 169 | + defer database.Close() |
| 170 | + |
| 171 | + tasks, err := database.ListTasks(db.ListTasksOptions{IncludeClosed: true, Limit: 50}) |
| 172 | + if err != nil { |
| 173 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 174 | + } |
| 175 | + |
| 176 | + var completions []string |
| 177 | + for _, t := range tasks { |
| 178 | + desc := t.Title |
| 179 | + if len(desc) > 40 { |
| 180 | + desc = desc[:37] + "..." |
| 181 | + } |
| 182 | + completions = append(completions, fmt.Sprintf("%d\t[%s] %s", t.ID, t.Status, desc)) |
| 183 | + } |
| 184 | + return completions, cobra.ShellCompDirectiveNoFileComp |
| 185 | +} |
| 186 | + |
| 187 | +// fetchProjectCompletions opens the DB and returns project name completions. |
| 188 | +func fetchProjectCompletions() ([]string, cobra.ShellCompDirective) { |
| 189 | + database, err := db.Open(db.DefaultPath()) |
| 190 | + if err != nil { |
| 191 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 192 | + } |
| 193 | + defer database.Close() |
| 194 | + |
| 195 | + projects, err := database.ListProjects() |
| 196 | + if err != nil { |
| 197 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 198 | + } |
| 199 | + |
| 200 | + var completions []string |
| 201 | + for _, p := range projects { |
| 202 | + desc := p.Name |
| 203 | + if p.Path != "" { |
| 204 | + desc = p.Path |
| 205 | + } |
| 206 | + completions = append(completions, fmt.Sprintf("%s\t%s", p.Name, desc)) |
| 207 | + } |
| 208 | + return completions, cobra.ShellCompDirectiveNoFileComp |
| 209 | +} |
| 210 | + |
| 211 | +// fetchTypeCompletions opens the DB and returns task type completions. |
| 212 | +func fetchTypeCompletions() ([]string, cobra.ShellCompDirective) { |
| 213 | + database, err := db.Open(db.DefaultPath()) |
| 214 | + if err != nil { |
| 215 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 216 | + } |
| 217 | + defer database.Close() |
| 218 | + |
| 219 | + types, err := database.ListTaskTypes() |
| 220 | + if err != nil { |
| 221 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 222 | + } |
| 223 | + |
| 224 | + var completions []string |
| 225 | + for _, t := range types { |
| 226 | + label := t.Label |
| 227 | + if label == "" { |
| 228 | + label = t.Name |
| 229 | + } |
| 230 | + completions = append(completions, fmt.Sprintf("%s\t%s", t.Name, label)) |
| 231 | + } |
| 232 | + return completions, cobra.ShellCompDirectiveNoFileComp |
| 233 | +} |
0 commit comments