|
| 1 | +/** |
| 2 | + * Shell completion scripts for devproc |
| 3 | + */ |
| 4 | + |
| 5 | +const COMMANDS = ["up", "down", "restart", "status", "init", "validate", "completions"] |
| 6 | +const OPTIONS = ["-c", "--config", "-w", "--watch", "-h", "--help", "-v", "--version"] |
| 7 | +const SHELLS = ["bash", "zsh", "fish"] |
| 8 | + |
| 9 | +/** |
| 10 | + * Generate bash completion script |
| 11 | + */ |
| 12 | +export function bashCompletion(): string { |
| 13 | + return `# devproc bash completion |
| 14 | +# Add to ~/.bashrc or ~/.bash_profile: |
| 15 | +# eval "$(devproc completions bash)" |
| 16 | +# Or save to a file: |
| 17 | +# devproc completions bash > /usr/local/etc/bash_completion.d/devproc |
| 18 | +
|
| 19 | +_devproc() { |
| 20 | + local cur prev words cword |
| 21 | + _init_completion || return |
| 22 | +
|
| 23 | + local commands="${COMMANDS.join(" ")}" |
| 24 | + local options="${OPTIONS.join(" ")}" |
| 25 | + local shells="${SHELLS.join(" ")}" |
| 26 | +
|
| 27 | + case "\${prev}" in |
| 28 | + -c|--config) |
| 29 | + # Complete yaml files |
| 30 | + _filedir '@(yaml|yml)' |
| 31 | + return |
| 32 | + ;; |
| 33 | + completions) |
| 34 | + COMPREPLY=( $(compgen -W "\${shells}" -- "\${cur}") ) |
| 35 | + return |
| 36 | + ;; |
| 37 | + esac |
| 38 | +
|
| 39 | + if [[ "\${cur}" == -* ]]; then |
| 40 | + COMPREPLY=( $(compgen -W "\${options}" -- "\${cur}") ) |
| 41 | + elif [[ \${cword} -eq 1 ]]; then |
| 42 | + COMPREPLY=( $(compgen -W "\${commands}" -- "\${cur}") ) |
| 43 | + fi |
| 44 | +} |
| 45 | +
|
| 46 | +complete -F _devproc devproc |
| 47 | +` |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Generate zsh completion script |
| 52 | + */ |
| 53 | +export function zshCompletion(): string { |
| 54 | + return `#compdef devproc |
| 55 | +# devproc zsh completion |
| 56 | +# Add to ~/.zshrc: |
| 57 | +# eval "$(devproc completions zsh)" |
| 58 | +# Or save to a file in your fpath: |
| 59 | +# devproc completions zsh > ~/.zsh/completions/_devproc |
| 60 | +
|
| 61 | +_devproc() { |
| 62 | + local -a commands options shells |
| 63 | +
|
| 64 | + commands=( |
| 65 | + 'up:Start all services (default)' |
| 66 | + 'down:Stop all services' |
| 67 | + 'restart:Restart all services' |
| 68 | + 'status:Show service status' |
| 69 | + 'init:Create a new devproc.yaml config file' |
| 70 | + 'validate:Validate the config file' |
| 71 | + 'completions:Generate shell completions' |
| 72 | + ) |
| 73 | +
|
| 74 | + options=( |
| 75 | + '-c[Path to config file]:config file:_files -g "*.y(a|)ml"' |
| 76 | + '--config[Path to config file]:config file:_files -g "*.y(a|)ml"' |
| 77 | + '-w[Watch config file for changes]' |
| 78 | + '--watch[Watch config file for changes]' |
| 79 | + '-h[Show help]' |
| 80 | + '--help[Show help]' |
| 81 | + '-v[Show version]' |
| 82 | + '--version[Show version]' |
| 83 | + ) |
| 84 | +
|
| 85 | + shells=( |
| 86 | + 'bash:Generate bash completions' |
| 87 | + 'zsh:Generate zsh completions' |
| 88 | + 'fish:Generate fish completions' |
| 89 | + ) |
| 90 | +
|
| 91 | + _arguments -s \\ |
| 92 | + '1: :->command' \\ |
| 93 | + '*: :->args' \\ |
| 94 | + && return 0 |
| 95 | +
|
| 96 | + case "\$state" in |
| 97 | + command) |
| 98 | + _describe -t commands 'devproc command' commands |
| 99 | + _describe -t options 'options' options |
| 100 | + ;; |
| 101 | + args) |
| 102 | + case "\$words[2]" in |
| 103 | + completions) |
| 104 | + _describe -t shells 'shell' shells |
| 105 | + ;; |
| 106 | + *) |
| 107 | + _describe -t options 'options' options |
| 108 | + ;; |
| 109 | + esac |
| 110 | + ;; |
| 111 | + esac |
| 112 | +} |
| 113 | +
|
| 114 | +_devproc "\$@" |
| 115 | +` |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Generate fish completion script |
| 120 | + */ |
| 121 | +export function fishCompletion(): string { |
| 122 | + return `# devproc fish completion |
| 123 | +# Add to ~/.config/fish/config.fish: |
| 124 | +# devproc completions fish | source |
| 125 | +# Or save to a file: |
| 126 | +# devproc completions fish > ~/.config/fish/completions/devproc.fish |
| 127 | +
|
| 128 | +# Disable file completion by default |
| 129 | +complete -c devproc -f |
| 130 | +
|
| 131 | +# Commands |
| 132 | +complete -c devproc -n "__fish_use_subcommand" -a "up" -d "Start all services (default)" |
| 133 | +complete -c devproc -n "__fish_use_subcommand" -a "down" -d "Stop all services" |
| 134 | +complete -c devproc -n "__fish_use_subcommand" -a "restart" -d "Restart all services" |
| 135 | +complete -c devproc -n "__fish_use_subcommand" -a "status" -d "Show service status" |
| 136 | +complete -c devproc -n "__fish_use_subcommand" -a "init" -d "Create a new devproc.yaml config file" |
| 137 | +complete -c devproc -n "__fish_use_subcommand" -a "validate" -d "Validate the config file" |
| 138 | +complete -c devproc -n "__fish_use_subcommand" -a "completions" -d "Generate shell completions" |
| 139 | +
|
| 140 | +# Options |
| 141 | +complete -c devproc -s c -l config -d "Path to config file" -r -F |
| 142 | +complete -c devproc -s w -l watch -d "Watch config file for changes" |
| 143 | +complete -c devproc -s h -l help -d "Show help" |
| 144 | +complete -c devproc -s v -l version -d "Show version" |
| 145 | +
|
| 146 | +# Completions subcommand |
| 147 | +complete -c devproc -n "__fish_seen_subcommand_from completions" -a "bash" -d "Generate bash completions" |
| 148 | +complete -c devproc -n "__fish_seen_subcommand_from completions" -a "zsh" -d "Generate zsh completions" |
| 149 | +complete -c devproc -n "__fish_seen_subcommand_from completions" -a "fish" -d "Generate fish completions" |
| 150 | +` |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Get completion script for a specific shell |
| 155 | + */ |
| 156 | +export function getCompletion(shell: string): string | null { |
| 157 | + switch (shell.toLowerCase()) { |
| 158 | + case "bash": |
| 159 | + return bashCompletion() |
| 160 | + case "zsh": |
| 161 | + return zshCompletion() |
| 162 | + case "fish": |
| 163 | + return fishCompletion() |
| 164 | + default: |
| 165 | + return null |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * List of supported shells |
| 171 | + */ |
| 172 | +export const SUPPORTED_SHELLS = SHELLS |
0 commit comments