App-manwrap-pm

 view release on metacpan or  search on metacpan

script/manwrap-pm  view on Meta::CPAN

use Complete::Util qw(complete_array_elem complete_comma_sep);
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2023-01-17'; # DATE
our $DIST = 'App-manwrap-pm'; # DIST
our $VERSION = '0.006'; # VERSION

my $opts = {
};
my $noop = sub {};
my $has_arg;

my $comp_man_or_mod = sub {
    require Complete::Man;
    require Complete::Module;
    require Complete::Util;

    my %args = @_;
    my $word = $args{word};

    # XXX for more proper
    my $has_arg = 0;
    for (0..$#{ $args{words} }) {
        if ($_ ne $args{argpos} && /^[^-]/) {
            $has_arg++;
            last;
        }
    }

    my @answers;
    if ($word =~ m!/!) {
        push @answers, Complete::Module::complete_module(word=>$word);
    } elsif ($word !~ /^-/ && !$has_arg) {
        push @answers, Complete::Man::complete_manpage(word=>$word, use_mandb=>0);
    }

    Complete::Util::combine_answers(@answers);
};

my @orig_argv = @ARGV;

# this is taken from App::cpanminus::script and should be updated from time to
# time.
GetOptionsWithCompletion(
    sub {
        my %args  = @_;
        my $type      = $args{type};
        my $word      = $args{word};
        if ($word eq '') {
            return {message=>"Gathering list of all modules and manpages will take at least a few seconds. Please type something first."};
        } elsif ($type eq 'arg') {
            log_trace("[manwrap-pm] Completing arg");
            return $comp_man_or_mod->(%args);
        } elsif ($type eq 'optval') {
            my $ospec = $args{ospec};
            my $opt   = $args{opt};
            log_trace("[manwrap-pm] Completing optval (opt=$opt, ospec=$ospec)");
            if ($ospec eq 'config-file|C=s') {
                return complete_file(filter=>'f', word=>$word);
            } elsif ($ospec eq 'locale|L=s') {
                require Complete::Locale;
                return Complete::Locale::complete_locale(word => $word);
            } elsif ($ospec eq 'manpath|M=s') {
                return complete_file(filter=>'d', word=>$word);
            } elsif ($ospec eq 'sections|S|s=s') {
                require Complete::Man;
                my $ss = Complete::Man::complete_manpage_section(word=>'');
                my $ss_elems = [];
                my $ss_summaries = [];
                for (@$ss) {
                    if (ref $_ eq 'HASH') {
                        push @$ss_elems, $_->{word};
                        push @$ss_summaries, $_->{summary};
                    } else {
                        push @$ss_elems, $_;
                        push @$ss_summaries, '';
                    }
                }
                log_trace "elems: %s", $ss_elems;
                log_trace "summaries: %s", $ss_summaries;

                return complete_comma_sep(word=>$word, elems=>$ss_elems, summaries=>$ss_summaries, uniq=>1);
            } elsif ($ospec eq 'pager|P=s') {
                require Complete::Program;
                return Complete::Program::complete_program(word=>$word);
            }
        }
        return [];
    },
    'config-file|C=s' => $noop,
    'debug|d' => $noop,
    'default|D' => $noop,
    'warnings=s' => $noop,
    'whatis|f' => $noop,
    'apropos|k' => $noop,
    'global-apropos|K' => $noop,
    'local-file|l' => $noop,
    'where|path|location|w' => $noop,
    'where-cat|location-cat|W' => $noop,
    'catman|c' => $noop,
    'recode|R=s' => $noop,
    'locale|L=s' => $noop,
    'systems|m=s' => $noop,
    'manpath|M=s' => $noop,
    'sections|S|s=s' => $noop,
    'extension|e=s' => $noop,
    'ignore-case|i' => $noop,
    'match-case|I' => $noop,
    'regex' => $noop,
    'wildcard' => $noop,
    'names-only' => $noop,
    'all|a' => $noop,
    'update|u' => $noop,
    'no-subpages' => $noop,
    'pager|P=s' => $noop,
    'prompt|r=s' => $noop,
    'ascii|7' => $noop,
    'encoding|E=s' => $noop,
    'no-hyphenation|nh' => $noop,
    'no-justification|nj' => $noop,
    'preprocessor|p=s' => $noop,
    'troff|t' => $noop,
    'troff-device|T:s' => $noop,
    'html|H:s' => $noop,
    'gxditview|X:s' => $noop,
    'ditroff|Z' => $noop,
    'help|?' => $noop,
    'usage' => $noop,
    'version|V' => $noop,

    '<>' => sub {
        my $arg = shift;
        # check if this is a module name in the form of Foo/Bar? if yes, we
        # convert it to Foo::Bar
        if ($arg =~ m!/!) {
            for (@orig_argv) {
                next unless $_ eq $arg;
                s!/!::!g;
            }
        }
    },
);

#use DD;
#dd @ARGV;
#dd \@orig_argv;

exec "man", @orig_argv;

# ABSTRACT: Wrapper and completer for man (extra Perl stuffs)
# PODNAME: manwrap-pm

__END__

=pod

=encoding UTF-8

=head1 NAME

manwrap-pm - Wrapper and completer for man (extra Perl stuffs)



( run in 2.426 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )