rejig to provide $REPL instead of $self, fix namespace::clean usage to not nuke meta
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / History.pm
CommitLineData
80fa249c 1package Devel::REPL::Plugin::History;
2
3use Moose::Role;
48ddfeae 4use namespace::clean -except => [ 'meta' ];
80fa249c 5
6has 'history' => (
7 isa => 'ArrayRef', is => 'rw', required => 1, lazy => 1,
8 default => sub { [] }
9);
10
11sub push_history {
12 my ($self, $line) = @_;
13 push(@{$self->history}, $line);
14}
15
16around 'read' => sub {
17 my $orig = shift;
18 my ($self, @args) = @_;
19 my $line = $self->$orig(@args);
20 if (defined $line) {
21 if ($line =~ m/^!(.*)$/) {
22 my $call = $1;
23 $line = $self->history_call($call);
24 if (defined $line) {
25 $self->print($line."\n");
26 } else {
27 return "'Unable to find ${call} in history'";
28 }
29 }
30 if ($line =~ m/\S/) {
31 $self->push_history($line);
32 }
33 }
34 return $line;
35};
36
37sub history_call {
38 my ($self, $call) = @_;
39 if ($call =~ m/^(-?\d+)$/) { # handle !1 or !-1
40 my $idx = $1;
41 $idx-- if ($idx > 0); # !1 gets history element 0
42 my $line = $self->history->[$idx];
43 return $line;
44 }
45 my $re = qr/^\Q${call}\E/;
46 foreach my $line (reverse @{$self->history}) {
47 return $line if ($line =~ $re);
48 }
49 return;
50};
51
521;