pstray / rename

Rename renames the filenames supplied according to the rule specified as the first argument. The argument is a Perl expression which is expected to modify the $_ string for at least some of the filenames specified.
Other
6 stars 1 forks source link

feat: add zsh completion, fix #6 #7

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 1 year ago

I generate zsh completion from --help. In fact, we can also generate it from pod like this:

open my $in, '<' . <*.pod> or die "Can't find `*.pod'!";
my @lines = ();
my @pods  = ();
my $sep   = $/;
$/ = '=item B<-';
while (<$in>) {
    push @pods, $_;

    # strip \..*
    s/^([^.]*)\..*/$1/s;

    # remove pod mark B<>, I<>
    s/^/B<-/;
    s/B<(-(?:.|-[^>]+))>/$1/g;
    s/I<([^>]*)>/$1/g;

    # convert '-?, --help' to '{-?,--help}'
    s/, -/,-/g;
    s!(-[^= ]+,[^=$sep]+)(=|$sep)!{$1}$2!;

    # convert '--option=value XXXX' to '--option"[XXX]:value"'
    s/=([^$sep]+)$sep$sep(.*)/"[$2]:$1"/s;

    # convert '--option XXXX' to '--option"[XXX]"'
    s/$sep$sep(.*)/"[$1]"/s;

    # change `--help` to `'(- : *)'--help`
    s/(.*and exit)/'(- : *)'$1/;
    s/(:shell)/$1:(zsh)/;
    push @lines, $_;
}
$/ = $sep;
# not $\, zsh only support '\n'
my $options = join "\n  ", @lines[ 1 .. $#lines ];

$code .= <<EOF;
sub print_completion_zsh() {
    print <<_EOF;
#compdef \$MAIN
local options=(
  $options
)
_arguments -S -s \\\$options '*:: :_files'
_EOF
}
pstray commented 1 year ago

Had a nearly finished version before this pull request, so I finished it and pushed it instead. Closes #6.