#!perl

our $DATE = '2016-11-04'; # DATE
our $VERSION = '0.002'; # VERSION

# FRAGMENT id=shcompgen-hint completer=1 for=meta

use strict;
use warnings;

use Shell::Completer;

declare_completer(
    options => {
        'help' => undef,
        'whitespace|ws' => undef,
        'version' => undef,
        'themes' => undef,
        'remote' => undef,
        'check' => undef,
        'category=s' => sub {
            my %args = @_;
            my $word = $args{word};

            my $theme = $args{words}[0];
            return [] unless defined($theme);
            return [] unless $theme =~ /\A\w+(\::\w+)*\z/;
            my $pkg = "Acme::MetaSyntactic::$theme";
            (my $pkg_pm = "$pkg.pm") =~ s!::!/!g;
            eval { require $pkg_pm; 1 };
            return [] if $@;

            require Complete::Util;
            return Complete::Util::complete_array_elem(
                word => $word,
                array => [$pkg->categories],
            );
        },
        'sources' => undef,
        '<>' => sub {
            my %args = @_;
            my $word = $args{word};

            if ($args{type} eq 'arg') {
                if ($args{argpos} == 0) {
                    #require Complete::Module;
                    #return Complete::Module::complete_module(
                    #    word => $word,
                    #    ns_prefix => 'Acme::MetaSyntactic',
                    #    path_sep => '::',
                    #);

                    # this is better
                    chomp(my @ary = `meta --themes`);
                    require Complete::Util;
                    return Complete::Util::complete_array_elem(
                        word => $word,
                        array => \@ary,
                    );
                } elsif ($args{argpos} == 1) {
                    require Complete::Number;
                    return Complete::Number::complete_int(
                        word => $word,
                        min => 1,
                    );
                }
                undef;
            }
            undef;
        },
    },
);

# ABSTRACT: Shell completer for meta (Acme::MetaSyntactic CLI)
# PODNAME: _meta

__END__

=pod

=encoding UTF-8

=head1 NAME

_meta - Shell completer for meta (Acme::MetaSyntactic CLI)

=head1 VERSION

This document describes version 0.002 of _meta (from Perl distribution App-ShellCompleter-meta), released on 2016-11-04.

=head1 SYNOPSIS

To install, install this module and then in your bash (and/or bash startup
file):

 complete -C _meta meta

or, you can use L<shcompgen> to do that for you automatically.

Now L<meta> has bash completion:

 % meta -<tab>                 ;# complete list of options
 % meta <tab>                  ;# complete theme
 % meta christmas --cat <tab>  ;# complete category

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-ShellCompleter-meta>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-ShellCompleter-meta>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-ShellCompleter-meta>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
