File Coverage

blib/lib/Devel/REPL/Plugin/History.pm
Criterion Covered Total %
statement 12 28 42.8
branch 0 8 0.0
condition n/a
subroutine 4 6 66.6
pod 0 2 0.0
total 16 44 36.3


line stmt bran cond sub pod time code
1 2     2   2646 use strict;
  2         2  
  2         58  
2 2     2   7 use warnings;
  2         2  
  2         103  
3             package Devel::REPL::Plugin::History;
4             # ABSTRACT: Keep track of all input, provide shortcuts !1, !-1
5              
6             our $VERSION = '1.003028';
7              
8 2     2   8 use Devel::REPL::Plugin;
  2         1  
  2         13  
9 2     2   6219 use namespace::autoclean;
  2         3  
  2         15  
10              
11             has 'history' => (
12             isa => 'ArrayRef', is => 'rw',
13             lazy => 1,
14             default => sub { [] }
15             );
16              
17             # lazy so ReadLineHistory Plugin can set this
18             has 'have_readline_history' => (
19             is => 'rw',
20             lazy => 1,
21             default => sub { 0 }
22             );
23              
24             sub push_history {
25 0     0 0   my ($self, $line) = @_;
26             # Push history is not needed if we have Term::ReadLine
27             # support. We put the test inside push_history() in case
28             # someone has modified it in their code.
29 0 0         if ($self->have_readline_history) {
30             # update history to keep consistent with Term::ReadLine
31 0           $self->history( [ $self->term->GetHistory ] );
32             } else {
33             # not used with Term::ReadLine history support
34 0           push(@{$self->history}, $line);
  0            
35             }
36             }
37              
38             around 'read' => sub {
39             my $orig = shift;
40             my ($self, @args) = @_;
41             my $line = $self->$orig(@args);
42             if (defined $line) {
43             if ($line =~ m/^!(.*)$/) {
44             my $call = $1;
45             $line = $self->history_call($call);
46             if (defined $line) {
47             $self->print($line."\n");
48             } else {
49             return "'Unable to find ${call} in history'";
50             }
51             }
52             if ($line =~ m/\S/) {
53             $self->push_history($line);
54             }
55             }
56             return $line;
57             };
58              
59             sub history_call {
60 0     0 0   my ($self, $call) = @_;
61 0 0         if ($call =~ m/^(-?\d+)$/) { # handle !1 or !-1
62 0           my $idx = $1;
63 0 0         $idx-- if ($idx > 0); # !1 gets history element 0
64 0           my $line = $self->history->[$idx];
65 0           return $line;
66             }
67 0           my $re = qr/^\Q${call}\E/;
68 0           foreach my $line (reverse @{$self->history}) {
  0            
69 0 0         return $line if ($line =~ $re);
70             }
71 0           return;
72             };
73              
74             1;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             Devel::REPL::Plugin::History - Keep track of all input, provide shortcuts !1, !-1
85              
86             =head1 VERSION
87              
88             version 1.003028
89              
90             =head1 SUPPORT
91              
92             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
93             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
94              
95             There is also an irc channel available for users of this distribution, at
96             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
97              
98             =head1 AUTHOR
99              
100             Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
101              
102             =head1 COPYRIGHT AND LICENCE
103              
104             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
105              
106             This is free software; you can redistribute it and/or modify it under
107             the same terms as the Perl 5 programming language system itself.
108              
109             =cut