Commit | Line | Data |
1716b200 |
1 | use strict; |
2 | use warnings; |
80fa249c |
3 | package Devel::REPL::Plugin::History; |
4 | |
54beb05d |
5 | our $VERSION = '1.003027'; |
6 | |
6a5409bc |
7 | use Devel::REPL::Plugin; |
aa8b7647 |
8 | use namespace::autoclean; |
80fa249c |
9 | |
10 | has 'history' => ( |
b595a818 |
11 | isa => 'ArrayRef', is => 'rw', |
12 | lazy => 1, |
78453011 |
13 | default => sub { [] } |
14 | ); |
15 | |
16 | # lazy so ReadLineHistory Plugin can set this |
17 | has 'have_readline_history' => ( |
b595a818 |
18 | is => 'rw', |
19 | lazy => 1, |
78453011 |
20 | default => sub { 0 } |
80fa249c |
21 | ); |
22 | |
23 | sub push_history { |
78453011 |
24 | my ($self, $line) = @_; |
25 | # Push history is not needed if we have Term::ReadLine |
26 | # support. We put the test inside push_history() in case |
27 | # someone has modified it in their code. |
28 | if ($self->have_readline_history) { |
29 | # update history to keep consistent with Term::ReadLine |
30 | $self->history( [ $self->term->GetHistory ] ); |
31 | } else { |
32 | # not used with Term::ReadLine history support |
33 | push(@{$self->history}, $line); |
34 | } |
80fa249c |
35 | } |
36 | |
37 | around 'read' => sub { |
78453011 |
38 | my $orig = shift; |
39 | my ($self, @args) = @_; |
40 | my $line = $self->$orig(@args); |
41 | if (defined $line) { |
42 | if ($line =~ m/^!(.*)$/) { |
43 | my $call = $1; |
44 | $line = $self->history_call($call); |
45 | if (defined $line) { |
46 | $self->print($line."\n"); |
47 | } else { |
48 | return "'Unable to find ${call} in history'"; |
49 | } |
50 | } |
51 | if ($line =~ m/\S/) { |
52 | $self->push_history($line); |
80fa249c |
53 | } |
78453011 |
54 | } |
55 | return $line; |
80fa249c |
56 | }; |
57 | |
58 | sub history_call { |
78453011 |
59 | my ($self, $call) = @_; |
60 | if ($call =~ m/^(-?\d+)$/) { # handle !1 or !-1 |
61 | my $idx = $1; |
62 | $idx-- if ($idx > 0); # !1 gets history element 0 |
63 | my $line = $self->history->[$idx]; |
64 | return $line; |
65 | } |
66 | my $re = qr/^\Q${call}\E/; |
67 | foreach my $line (reverse @{$self->history}) { |
68 | return $line if ($line =~ $re); |
69 | } |
70 | return; |
80fa249c |
71 | }; |
72 | |
73 | 1; |
cfd1094b |
74 | |
75 | __END__ |
76 | |
77 | =head1 NAME |
78 | |
79 | Devel::REPL::Plugin::History - Keep track of all input, provide shortcuts !1, !-1 |
80 | |
81 | =cut |
82 | |