File Coverage

blib/lib/Devel/REPL/Plugin/ReadLineHistory.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             # First cut at using the readline history directly rather than reimplementing
2             # it. It does save history but it's a little crappy; still playing with it ;)
3             #
4             # epitaph, 22nd April 2007
5              
6 2     2   1840 use strict;
  2         5  
  2         57  
7 2     2   10 use warnings;
  2         3  
  2         98  
8             # ABSTRACT: Integrate history with the facilities provided by L<Term::ReadLine>
9              
10             our $VERSION = '1.003029';
11              
12             use Devel::REPL::Plugin;
13 2     2   11 use File::Spec;
  2         4  
  2         10  
14 2     2   8044 use namespace::autoclean;
  2         6  
  2         42  
15 2     2   9  
  2         6  
  2         13  
16             my $hist_file = $ENV{PERLREPL_HISTFILE} ||
17             File::Spec->catfile(($^O eq 'MSWin32' && "$]" < 5.016 ? $ENV{HOME} || $ENV{USERPROFILE} : (<~>)[0]), '.perlreplhist');
18              
19             # HISTLEN should probably be in a config file to stop people accidentally
20             # truncating their history if they start the program and forget to set
21             # PERLREPL_HISTLEN
22             my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
23              
24             around 'run' => sub {
25             my $orig=shift;
26             my ($self, @args)=@_;
27             if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
28             $self->term->stifle_history($hist_len);
29             }
30             if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
31             $self->term->Attribs->{MaxHistorySize} = $hist_len;
32             }
33             if (-f($hist_file)) {
34             if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
35             $self->term->ReadHistory($hist_file);
36             }
37             if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
38             open HIST, $hist_file or die "ReadLineHistory: could not open $hist_file: $!\n";
39             while (my $line = <HIST>) {
40             chomp $line;
41             $self->term->addhistory($line);
42             }
43             close HIST;
44             }
45             }
46              
47             $self->term->Attribs->{do_expand}=1; # for Term::ReadLine::Gnu
48             $self->term->MinLine(2); # don't save one letter commands
49              
50             # let History plugin know we have Term::ReadLine support
51             $self->have_readline_history(1) if $self->can('have_readline_history');
52              
53              
54             $self->$orig(@args);
55              
56             if ($self->term->ReadLine eq 'Term::ReadLine::Gnu') {
57             $self->term->WriteHistory($hist_file) ||
58             $self->print("warning: failed to write history file $hist_file");
59             }
60             if ($self->term->ReadLine eq 'Term::ReadLine::Perl') {
61             my @lines = $self->term->GetHistory() if $self->term->can('GetHistory');
62             if( open HIST, ">$hist_file" ) {
63             print HIST join("\n",@lines);
64             close HIST;
65             } else {
66             $self->print("warning: unable to WriteHistory to $hist_file");
67             }
68             }
69             };
70              
71             1;
72              
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Devel::REPL::Plugin::ReadLineHistory - Integrate history with the facilities provided by L<Term::ReadLine>
81              
82             =head1 VERSION
83              
84             version 1.003029
85              
86             =head1 DESCRIPTION
87              
88             This plugin enables loading and saving command line history from
89             a file as well has history expansion of previous commands using
90             the !-syntax a la bash.
91              
92             By default, history expansion is enabled with this plugin when
93             using L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>. That means that
94             "loose" '!' characters will be treated as history events which
95             may not be what you wish.
96              
97             To avoid this, you need to quote the '!' with '\':
98              
99             my $var = "foo\!";
100              
101             or place the arguments in single quotes---but enable the
102             C<Term::ReadLine> attribute C<history_quotes_inhibit_expansion>:
103              
104             $_REPL->term->Attribs->{history_quotes_inhibit_expansion} = 1;
105             my $var = 'foo!';
106              
107             and to disable history expansion from GNU readline/history do
108              
109             $_REPL->term->Attribs->{do_expand} = 0;
110              
111             =head1 CONFLICTS
112              
113             Note that L<Term::ReadLine::Perl> does not support a history
114             expansion method. In that case, you may wish to use the
115             L<Devel::REPL History plugin|Devel::REPL::Plugin::History> which provides similar functions.
116             Work is underway to make use of either L<History|Devel::REPL::Plugin::History> or
117             L<ReadLineHistory|Devel::REPL::Plugin::ReadHistory>> consistent for expansion with either the
118             L<Term::ReadLine::Gnu> support or L<Term::ReadLine::Perl>.
119              
120             =head1 SUPPORT
121              
122             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
123             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
124              
125             There is also an irc channel available for users of this distribution, at
126             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
127              
128             =head1 AUTHOR
129              
130             Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
131              
132             =head1 COPYRIGHT AND LICENCE
133              
134             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut