make ReadLineHistory history file location portable
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / ReadLineHistory.pm
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 package Devel::REPL::Plugin::ReadLineHistory;
7
8 use Devel::REPL::Plugin;
9 use File::HomeDir;
10 use File::Spec;
11
12 my $hist_file = $ENV{PERLREPL_HISTFILE} ||
13     File::Spec->catfile(File::HomeDir->my_home, '.perlreplhist');
14
15 # HISTLEN should probably be in a config file to stop people accidentally
16 # truncating their history if they start the program and forget to set
17 # PERLREPL_HISTLEN
18 my $hist_len=$ENV{PERLREPL_HISTLEN} || 100;
19
20 around 'run' => sub {
21   my $orig=shift;
22   my ($self, @args)=@_;
23   $self->term->stifle_history($hist_len);
24   -f($hist_file) && $self->term->ReadHistory($hist_file);
25   $self->term->Attribs->{do_expand}=1;
26   $self->$orig(@args);
27   $self->term->WriteHistory($hist_file) ||
28     $self->print("warning: failed to write history file $hist_file");
29 };
30
31 1;
32
33 __END__
34
35 =head1 NAME
36
37 Devel::REPL::Plugin::ReadLineHistory - Integrate history with the facilities provided by L<Term::ReadLine>
38
39 =cut
40