Ei

 view release on metacpan or  search on metacpan

bin/ei  view on Meta::CPAN

        't|format=s' => \$t,
    ) or usage;
    my @items_to_export = @items;
    if ($l) {
        @items_to_export = grep { ($_->{location} // '') eq $l } @items_to_export;
    }
    foreach (@items_to_export) {
        print_item($_);
    }
}

sub print_item {
    my ($item, $format) = @_;
    my $formatter = __PACKAGE__->can('as_'.lc($format // $shell{'format'} // 'native'))
        or fatal "unknown format: $format";
    my %item = %$item;
    $item{'id'}   ||= delete $item{'#'};
    $item{'file'} ||= delete $item{'/'};
    $item{'line'} ||= delete $item{'.'};
    print $formatter->(\%item);
}

sub cmd_help {
    #@ help :: show helpful information
    my $pfx = %shell ? '' : 'ei [-c CONFIGFILE] ';
    print STDERR <<'EOS';
usage: ${pfx}COMMAND [ARG...]
commands:
EOS
    my $w = 0;
    my ($commands, $aliases) = commands();
    my $table = Text::Table->new(\'  ', 'command', \'  ', 'description');
    foreach (sort { $a->{'name'} cmp $b->{'name'} } @$commands) {
        my ($name, $args, $descrip) = @$_{qw(name args description)};
        $table->add(join(' ', $name, @{ $args || [] }), $descrip || '');
    }
    print $table->body;
    $table = Text::Table->new(\'  ', 'alias', \' = ', 'command');
    print "aliases:\n";
    foreach (sort keys %$aliases) {
        $table->add($_, $aliases->{$_});
    }
    print $table->body;
}

sub cmd_shell {
    init_shell();
    compile_prompt($shell{'prompt'});
    print STDERR $shell{'prompter'}->();
    while (<STDIN>) {
        next if /^\s*(?:#.*)?$/;  # Skip blank lines and comments
        chomp;
        if (s/^!\s*//) {
            system($ENV{'SHELL'} || 'sh', '-c', $_);
            next;
        }
        elsif (/^{(.+)}$/) {
            eval $1;
            next;
        }
        local @ARGV = shellwords($_);
        my $cmd = shift @ARGV;
        $cmd =~ tr/-/_/;
        if ($shell{'aliases'}{$cmd}) {
            $cmd = $shell{'aliases'}{$cmd};
            if (ref $cmd) {
                ($cmd, @ARGV) = (@$cmd, @ARGV);
            }
        }
        my $sub = __PACKAGE__->can("shellcmd_$cmd")
               || __PACKAGE__->can("cmd_$cmd");
        if (!$sub) {
            print "unrecognized command: $cmd\nenter 'help' for a list\n";
            next;
        }
        eval {
            $shell{'cmd'} = $cmd;
            $sub->();
        };
        last if !$shell{'running'};
    }
    continue {
        print STDERR $shell{'prompter'}->();
    }
}

# --- Shell commands

sub shellcmd_goto {
    usage if @ARGV != 1;
    my ($loc) = @ARGV;
    print STDERR "empty location: $loc\n" if !exists $shell{'locations'}{$loc};
    $shell{'location'} = $loc;
}

sub shellcmd_quit {
    CORE::exit;
}

sub shellcmd_proto {
    usage if @ARGV != 1;
    $shell{'prototype'} = shift @ARGV;
}

sub shellcmd_view {
    usage if @ARGV > 1;
    my $id = @ARGV ? shift @ARGV : $shell{'item'};
    fatal "no current item" if !$id;
    my ($item) = grep { $_->{'#'} eq $id } @items;
    fatal "no such item: $id" if !$item;
    $shell{'item'} = $id;
    print_item($item);
}

sub shellcmd_format {
    if (@ARGV != 1) {
        print $shell{'format'} ||= 'native', "\n";
    }
    else {
        my $format = shift @ARGV;
        usage if !__PACKAGE__->can('as_'.$format);



( run in 2.908 seconds using v1.01-cache-2.11-cpan-364913b4093 )