| 1 | # ABSTRACT: Generate shell completion scripts for bash, zsh, and fish | ||
| 2 | |||
| 3 | package App::karr::Cmd::Completion; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo; | ||
| 6 | use MooX::Cmd; | ||
| 7 | use MooX::Options ( | ||
| 8 | usage_string => 'USAGE: karr completion bash|zsh|fish', | ||
| 9 | ); | ||
| 10 | use App::karr; | ||
| 11 | use App::karr::Config; | ||
| 12 | use App::karr::Role::CliArgs; | ||
| 13 | use App::karr::Role::ExitCodes; | ||
| 14 | use Module::Runtime qw( use_module ); | ||
| 15 | |||
| 16 | with 'App::karr::Role::CliArgs', 'App::karr::Role::ExitCodes'; | ||
| 17 | |||
| 18 | |||
| 19 | sub execute { | ||
| 20 | my ( $self, $args_ref, $chain_ref ) = @_; | ||
| 21 | |||
| 22 | $self->check_positional_args( $args_ref, 1 ); | ||
| 23 | my ($shell) = $self->positional_args($args_ref); | ||
| 24 | die "Usage: karr completion bash|zsh|fish\n" | ||
| 25 | unless defined $shell && length $shell; | ||
| 26 | $self->usage_error("unknown shell '$shell' (use bash, zsh, or fish)") | ||
| 27 | unless $shell =~ /\A(?:bash|zsh|fish)\z/; | ||
| 28 | |||
| 29 | my @command_data = $self->_command_data; | ||
| 30 | my $commands = \@command_data; | ||
| 31 | my $script = | ||
| 32 | $shell eq 'bash' ? $self->_bash_script($commands) | ||
| 33 | : $shell eq 'zsh' ? $self->_zsh_script($commands) | ||
| 34 | : $self->_fish_script($commands); | ||
| 35 | print $script; | ||
| 36 | } | ||
| 37 | |||
| 38 | # The command table as hashes: name, description, and the class that implements | ||
| 39 | # it. Aliases are included (agentname, getrefs, setrefs), each carrying the | ||
| 40 | # description of the spelling it aliases, so completion offers every spelling | ||
| 41 | # the CLI accepts. | ||
| 42 | sub _command_data { | ||
| 43 | my ($self) = @_; | ||
| 44 | |||
| 45 | my $classes = App::karr->_build_command_commands( {} ); | ||
| 46 | my @out; | ||
| 47 | for my $row ( App::karr->command_table ) { | ||
| 48 | my ( $name, $desc ) = @$row; | ||
| 49 | my $class = $classes->{$name} or next; | ||
| 50 | push @out, { name => $name, desc => $desc, class => $class }; | ||
| 51 | } | ||
| 52 | return sort { $a->{name} cmp $b->{name} } @out; | ||
| 53 | } | ||
| 54 | |||
| 55 | # The options of one command class as hashes: dashed name, --long spelling, | ||
| 56 | # short spelling, value format, and doc. --help is added by hand: MooX::Options | ||
| 57 | # declares it outside _options_data (describe_options adds it), and every | ||
| 58 | # command accepts it, so completion must offer it even though the option table | ||
| 59 | # does not. A negatable option (--color) also gets its --no- spelling. | ||
| 60 | sub _options_for { | ||
| 61 | my ( $self, $class ) = @_; | ||
| 62 | |||
| 63 | use_module($class); | ||
| 64 | my %data = $class->_options_data; | ||
| 65 | my @opts; | ||
| 66 | for my $name ( sort keys %data ) { | ||
| 67 | my $d = $data{$name}; | ||
| 68 | push @opts, { | ||
| 69 | name => $name, | ||
| 70 | long => '--' . $name, | ||
| 71 | short => defined $d->{short} ? '-' . $d->{short} : undef, | ||
| 72 | format => $d->{format}, | ||
| 73 | doc => $d->{doc}, | ||
| 74 | }; | ||
| 75 | if ( $d->{negatable} ) { | ||
| 76 | push @opts, { | ||
| 77 | name => "no-$name", | ||
| 78 | long => "--no-$name", | ||
| 79 | short => undef, | ||
| 80 | format => undef, | ||
| 81 | doc => "Negate --$name", | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | push @opts, { | ||
| 86 | name => 'help', | ||
| 87 | long => '--help', | ||
| 88 | short => '-h', | ||
| 89 | format => undef, | ||
| 90 | doc => 'Show help', | ||
| 91 | }; | ||
| 92 | return @opts; | ||
| 93 | } | ||
| 94 | |||
| 95 | # The fixed value list for an option, if it has one. Everything here is cheap | ||
| 96 | # and static: the --sort fields and the default priorities. Board-specific | ||
| 97 | # values are deliberately absent (see DESCRIPTION). | ||
| 98 | sub _value_list { | ||
| 99 | my ( $self, $cmd, $opt ) = @_; | ||
| 100 | |||
| 101 | return [qw( id title status priority created updated due )] if $opt eq 'sort'; | ||
| 102 | return [ @{ App::karr::Config->default_config->{priorities} } ] | ||
| 103 | if $opt eq 'priority'; | ||
| 104 | return undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | # --- bash --------------------------------------------------------------- | ||
| 108 | |||
| 109 | sub _bash_script { | ||
| 110 | my ( $self, $commands ) = @_; | ||
| 111 | |||
| 112 | my $commands_list = join ' ', map { $_->{name} } @$commands; | ||
| 113 | my @root_opts = $self->_options_for('App::karr'); | ||
| 114 | my @global = map { $_->{long} } @root_opts; | ||
| 115 | push @global, map { $_->{short} } grep { $_->{short} } @root_opts; | ||
| 116 | my $global_opts = join ' ', @global; | ||
| 117 | |||
| 118 | my %by_class; | ||
| 119 | for my $cmd (@$commands) { | ||
| 120 | push @{ $by_class{ $cmd->{class} } }, $cmd->{name}; | ||
| 121 | } | ||
| 122 | |||
| 123 | my $out = ''; | ||
| 124 | $out .= "# bash completion for karr\n"; | ||
| 125 | $out .= "# Generated by `karr completion bash` -- static, never calls karr.\n"; | ||
| 126 | $out .= "\n"; | ||
| 127 | $out .= "_karr() {\n"; | ||
| 128 | $out .= " local cur prev cmd\n"; | ||
| 129 | $out .= " COMPREPLY=()\n"; | ||
| 130 | $out .= " cur=\"\${COMP_WORDS[COMP_CWORD]}\"\n"; | ||
| 131 | $out .= " prev=\"\${COMP_WORDS[COMP_CWORD-1]}\"\n"; | ||
| 132 | $out .= "\n"; | ||
| 133 | $out .= " # The subcommand, if any: the first word that names one. Option\n"; | ||
| 134 | $out .= " # values are skipped because they are not command names.\n"; | ||
| 135 | $out .= " local commands=\"$commands_list\"\n"; | ||
| 136 | $out .= " local i word\n"; | ||
| 137 | $out .= " for (( i = 1; i < COMP_CWORD; i++ )); do\n"; | ||
| 138 | $out .= " word=\"\${COMP_WORDS[i]}\"\n"; | ||
| 139 | $out .= " case \" \$commands \" in\n"; | ||
| 140 | $out .= " *\" \$word \"*) cmd=\"\$word\"; break ;;\n"; | ||
| 141 | $out .= " esac\n"; | ||
| 142 | $out .= " done\n"; | ||
| 143 | $out .= "\n"; | ||
| 144 | $out .= " # Value completion for options with a fixed value list.\n"; | ||
| 145 | $out .= " case \"\$prev\" in\n"; | ||
| 146 | my %seen_values; | ||
| 147 | for my $cmd (@$commands) { | ||
| 148 | for my $opt ( $self->_options_for( $cmd->{class} ) ) { | ||
| 149 | my $values = $self->_value_list( $cmd->{name}, $opt->{name} ); | ||
| 150 | next unless $values; | ||
| 151 | next if $seen_values{ $opt->{long} }++; | ||
| 152 | $out .= " $opt->{long})\n"; | ||
| 153 | $out .= " COMPREPLY=( \$(compgen -W \"@$values\" -- \"\$cur\") )\n"; | ||
| 154 | $out .= " return 0\n"; | ||
| 155 | $out .= " ;;\n"; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | $out .= " esac\n"; | ||
| 159 | $out .= "\n"; | ||
| 160 | $out .= " if [[ -z \"\$cmd\" ]]; then\n"; | ||
| 161 | $out .= " COMPREPLY=( \$(compgen -W \"\$commands $global_opts\" -- \"\$cur\") )\n"; | ||
| 162 | $out .= " return 0\n"; | ||
| 163 | $out .= " fi\n"; | ||
| 164 | $out .= "\n"; | ||
| 165 | $out .= " case \"\$cmd\" in\n"; | ||
| 166 | for my $class ( sort keys %by_class ) { | ||
| 167 | my @opts = $self->_options_for($class); | ||
| 168 | my @words = map { $_->{long} } @opts; | ||
| 169 | push @words, map { $_->{short} } grep { $_->{short} } @opts; | ||
| 170 | $out .= " " . join( '|', @{ $by_class{$class} } ) . ")\n"; | ||
| 171 | $out .= " COMPREPLY=( \$(compgen -W \"@words\" -- \"\$cur\") )\n"; | ||
| 172 | $out .= " ;;\n"; | ||
| 173 | } | ||
| 174 | $out .= " esac\n"; | ||
| 175 | $out .= "}\n"; | ||
| 176 | $out .= "\n"; | ||
| 177 | $out .= "complete -F _karr karr\n"; | ||
| 178 | return $out; | ||
| 179 | } | ||
| 180 | |||
| 181 | # --- zsh ---------------------------------------------------------------- | ||
| 182 | |||
| 183 | sub _zsh_script { | ||
| 184 | my ( $self, $commands ) = @_; | ||
| 185 | |||
| 186 | my %by_class; | ||
| 187 | for my $cmd (@$commands) { | ||
| 188 | push @{ $by_class{ $cmd->{class} } }, $cmd->{name}; | ||
| 189 | } | ||
| 190 | |||
| 191 | my $out = ''; | ||
| 192 | $out .= "#compdef karr\n"; | ||
| 193 | $out .= "\n"; | ||
| 194 | $out .= "# zsh completion for karr\n"; | ||
| 195 | $out .= "# Generated by `karr completion zsh` -- static, never calls karr.\n"; | ||
| 196 | $out .= "\n"; | ||
| 197 | $out .= "_karr() {\n"; | ||
| 198 | $out .= " local -a commands\n"; | ||
| 199 | $out .= " commands=(\n"; | ||
| 200 | for my $cmd (@$commands) { | ||
| 201 | $out .= " " . $self->_zsh_quote( $cmd->{name} . ':' . $cmd->{desc} ) . "\n"; | ||
| 202 | } | ||
| 203 | $out .= " )\n"; | ||
| 204 | $out .= " local -a names\n"; | ||
| 205 | $out .= " names=( " . join( ' ', map { $_->{name} } @$commands ) . " )\n"; | ||
| 206 | $out .= "\n"; | ||
| 207 | |||
| 208 | # The root context (no subcommand yet) offers both the command names and the | ||
| 209 | # root's own options, the same pair bash and fish embed there. _describe adds | ||
| 210 | # the commands; _arguments adds the options. | ||
| 211 | my @root_specs = map { $self->_zsh_spec( $_, '' ) } $self->_options_for('App::karr'); | ||
| 212 | my $root_body = " _describe -t commands 'karr command' commands\n"; | ||
| 213 | $root_body .= " _arguments \\\n"; | ||
| 214 | for my $i ( 0 .. $#root_specs ) { | ||
| 215 | my $comma = $i == $#root_specs ? '' : ' \\'; | ||
| 216 | $root_body .= " $root_specs[$i]$comma\n"; | ||
| 217 | } | ||
| 218 | $root_body .= " return\n"; | ||
| 219 | |||
| 220 | $out .= " if (( CURRENT == 2 )); then\n"; | ||
| 221 | $out .= $root_body; | ||
| 222 | $out .= " fi\n"; | ||
| 223 | $out .= "\n"; | ||
| 224 | $out .= " # The subcommand, if any: the first word that names one.\n"; | ||
| 225 | $out .= " local cmd\n"; | ||
| 226 | $out .= " local i word\n"; | ||
| 227 | $out .= " for (( i = 2; i < CURRENT; i++ )); do\n"; | ||
| 228 | $out .= " word=\"\${words[i]}\"\n"; | ||
| 229 | $out .= " case \" \${names[*]} \" in\n"; | ||
| 230 | $out .= " *\" \$word \"*) cmd=\"\$word\"; break ;;\n"; | ||
| 231 | $out .= " esac\n"; | ||
| 232 | $out .= " done\n"; | ||
| 233 | $out .= "\n"; | ||
| 234 | $out .= " if [[ -z \"\$cmd\" ]]; then\n"; | ||
| 235 | $out .= $root_body; | ||
| 236 | $out .= " fi\n"; | ||
| 237 | $out .= "\n"; | ||
| 238 | $out .= " case \"\$cmd\" in\n"; | ||
| 239 | for my $class ( sort keys %by_class ) { | ||
| 240 | $out .= " " . join( '|', @{ $by_class{$class} } ) . ")\n"; | ||
| 241 | $out .= " _arguments \\\n"; | ||
| 242 | my @specs = map { $self->_zsh_spec( $_, $by_class{$class}[0] ) } | ||
| 243 | $self->_options_for($class); | ||
| 244 | for my $i ( 0 .. $#specs ) { | ||
| 245 | my $comma = $i == $#specs ? '' : ' \\'; | ||
| 246 | $out .= " $specs[$i]$comma\n"; | ||
| 247 | } | ||
| 248 | $out .= " ;;\n"; | ||
| 249 | } | ||
| 250 | $out .= " esac\n"; | ||
| 251 | $out .= "}\n"; | ||
| 252 | $out .= "\n"; | ||
| 253 | $out .= "compdef _karr karr\n"; | ||
| 254 | return $out; | ||
| 255 | } | ||
| 256 | |||
| 257 | # One _arguments spec for one option. A boolean is '--opt[doc]'; a value-taking | ||
| 258 | # option appends ':name:' and, when the option has a fixed value list, the list | ||
| 259 | # in parentheses. The short spelling is its own spec. | ||
| 260 | sub _zsh_spec { | ||
| 261 | my ( $self, $opt, $cmd ) = @_; | ||
| 262 | |||
| 263 | my $values = $self->_value_list( $cmd, $opt->{name} ); | ||
| 264 | my $action = $values ? '(' . join( ' ', @$values ) . ')' : ''; | ||
| 265 | my $spec = $self->_zsh_quote( '--' . $opt->{name} . '[' . ( $opt->{doc} // '' ) . ']' ); | ||
| 266 | if ( defined $opt->{format} ) { | ||
| 267 | $spec = $self->_zsh_quote( | ||
| 268 | '--' . $opt->{name} . '[' . ( $opt->{doc} // '' ) . ']:' . $opt->{name} . ':' . $action ); | ||
| 269 | } | ||
| 270 | if ( defined $opt->{short} ) { | ||
| 271 | my $short = $self->_zsh_quote( $opt->{short} . '[' . ( $opt->{doc} // '' ) . ']' ); | ||
| 272 | if ( defined $opt->{format} ) { | ||
| 273 | $short = $self->_zsh_quote( | ||
| 274 | $opt->{short} . '[' . ( $opt->{doc} // '' ) . ']:' . $opt->{name} . ':' . $action ); | ||
| 275 | } | ||
| 276 | $spec .= ' ' . $short; | ||
| 277 | } | ||
| 278 | return $spec; | ||
| 279 | } | ||
| 280 | |||
| 281 | # Single-quote for zsh, escaping embedded single quotes the way bash does. | ||
| 282 | sub _zsh_quote { | ||
| 283 | my ( $self, $s ) = @_; | ||
| 284 | $s =~ s/'/'"'"'/g; | ||
| 285 | return "'$s'"; | ||
| 286 | } | ||
| 287 | |||
| 288 | # --- fish --------------------------------------------------------------- | ||
| 289 | |||
| 290 | sub _fish_script { | ||
| 291 | my ( $self, $commands ) = @_; | ||
| 292 | |||
| 293 | my %by_class; | ||
| 294 | for my $cmd (@$commands) { | ||
| 295 | push @{ $by_class{ $cmd->{class} } }, $cmd->{name}; | ||
| 296 | } | ||
| 297 | |||
| 298 | my %root_names = map { $_->{name} => 1 } $self->_options_for('App::karr'); | ||
| 299 | |||
| 300 | my $out = ''; | ||
| 301 | $out .= "# fish completion for karr\n"; | ||
| 302 | $out .= "# Generated by `karr completion fish` -- static, never calls karr.\n"; | ||
| 303 | $out .= "\n"; | ||
| 304 | $out .= "complete -c karr -f\n"; | ||
| 305 | for my $opt ( $self->_options_for('App::karr') ) { | ||
| 306 | $out .= $self->_fish_opt_spec( $opt, undef ); | ||
| 307 | } | ||
| 308 | for my $cmd (@$commands) { | ||
| 309 | $out .= "complete -c karr -n '__fish_use_subcommand' -a " | ||
| 310 | . $self->_fish_quote($cmd->{name}) . ' -d ' | ||
| 311 | . $self->_fish_quote($cmd->{desc}) . "\n"; | ||
| 312 | } | ||
| 313 | for my $class ( sort keys %by_class ) { | ||
| 314 | my $cond = '__fish_seen_subcommand_from ' . join( ' ', @{ $by_class{$class} } ); | ||
| 315 | for my $opt ( $self->_options_for($class) ) { | ||
| 316 | next if $root_names{ $opt->{name} }; | ||
| 317 | $out .= $self->_fish_opt_spec( $opt, $cond ); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | return $out; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub _fish_opt_spec { | ||
| 324 | my ( $self, $opt, $cond ) = @_; | ||
| 325 | |||
| 326 | my $out = 'complete -c karr'; | ||
| 327 | $out .= " -n '$cond'" if defined $cond; | ||
| 328 | $out .= ' -l ' . $opt->{name}; | ||
| 329 | $out .= ' -s ' . $opt->{short} if defined $opt->{short}; | ||
| 330 | my $values = $self->_value_list( undef, $opt->{name} ); | ||
| 331 | $out .= ' -a "' . join( ' ', @$values ) . '"' if $values; | ||
| 332 | $out .= ' -d ' . $self->_fish_quote( $opt->{doc} // '' ); | ||
| 333 | return $out . "\n"; | ||
| 334 | } | ||
| 335 | |||
| 336 | # Single-quote for fish, escaping embedded single quotes with a backslash. | ||
| 337 | sub _fish_quote { | ||
| 338 | my ( $self, $s ) = @_; | ||
| 339 | $s =~ s/'/\\'/g; | ||
| 340 | return "'$s'"; | ||
| 341 | } | ||
| 342 | |||
| 343 | 1; | ||
| 344 | |||
| 345 | __END__ |