Permalink
Browse files

Keep history between shell invocations.

Keep the history inside 'histfile' between shell invocations, so the
comands will be remembered. I used the CPAN.pm code as reference, but
the code here is original.

TODO : add a way to configure how many lines to remember.
  • Loading branch information...
1 parent 8cfd0f1 commit e5f027449894c4fdca7036cfff82559b2dabf8b2 @shlomif committed Apr 5, 2012
Showing with 75 additions and 0 deletions.
  1. +3 −0 ChangeLog
  2. +9 −0 lib/CPANPLUS/Config.pm
  3. +32 −0 lib/CPANPLUS/Shell/Classic.pm
  4. +31 −0 lib/CPANPLUS/Shell/Default.pm
View
3 ChangeLog
@@ -1,3 +1,6 @@
+* Save the history between invocations of the
+ shell.
+
Changes for 0.9123 Fri Mar 30 16:46:52 2012
================================================
* Added support for adding blib/script to PATH
View
9 lib/CPANPLUS/Config.pm
@@ -282,6 +282,15 @@ etc. Defaults to 'false'.
$Conf->{'conf'}->{'force'} = 0;
+=item histfile
+
+A string containing the history filename of the CPANPLUS readline instance.
+
+=cut
+
+ $Conf->{'conf'}->{'histfile'} = File::Spec->catdir(
+ __PACKAGE__->_home_dir, DOT_CPANPLUS, 'history' );
+
=item lib
An array ref holding directories to be added to C<@INC> when CPANPLUS
View
32 lib/CPANPLUS/Shell/Classic.pm
@@ -97,6 +97,20 @@ sub new {
code => \&__ask_about_test_report,
);
+ if (my $histfile = $self->configure_object->get_conf( 'histfile' )) {
+ my $term = $self->term;
+ if ($term->can('AddHistory')) {
+ if (open my $fh, '<', $histfile) {
+ local $/ = "\n";
+ while (my $line = <$fh>) {
+ chomp($line);
+ $term->AddHistory($line);
+ }
+ close($fh);
+ }
+ }
+ }
+
return $self;
}
@@ -194,6 +208,24 @@ sub _dispatch_on_input {
### displays quit message
sub _quit {
+ my $self = shift;
+ my $term = $self->term;
+
+ if ($term->can('GetHistory')) {
+ my @history = $term->GetHistory;
+
+ my $histfile = $self->configure_object->get_conf('histfile');
+
+ if (open my $fh, '>', $histfile) {
+ foreach my $line (@history) {
+ print {$fh} "$line\n";
+ }
+ close($fh);
+ }
+ else {
+ warn "Cannot open history file '$histfile' - $!";
+ }
+ }
### well, that's what CPAN.pm says...
print "Lockfile removed\n";
View
31 lib/CPANPLUS/Shell/Default.pm
@@ -213,6 +213,20 @@ sub new {
### load all the plugins
$self->_plugins_init;
+ if (my $histfile = $cb->configure_object->get_conf( 'histfile' )) {
+ my $term = $self->term;
+ if ($term->can('AddHistory')) {
+ if (open my $fh, '<', $histfile) {
+ local $/ = "\n";
+ while (my $line = <$fh>) {
+ chomp($line);
+ $term->AddHistory($line);
+ }
+ close($fh);
+ }
+ }
+ }
+
return $self;
}
@@ -511,10 +525,27 @@ sub __display_results {
sub _quit {
my $self = shift;
+ my $term = $self->term;
$self->dispatch_on_input( input => $rc->{'logout'} )
if defined $rc->{'logout'};
+ if ($term->can('GetHistory')) {
+ my @history = $term->GetHistory;
+
+ my $histfile = $self->backend->configure_object->get_conf('histfile');
+
+ if (open my $fh, '>', $histfile) {
+ foreach my $line (@history) {
+ print {$fh} "$line\n";
+ }
+ close($fh);
+ }
+ else {
+ warn "Cannot open history file '$histfile' - $!";
+ }
+ }
+
$self->__print( loc("Exiting CPANPLUS shell"), "\n" );
return 1;

0 comments on commit e5f0274

Please sign in to comment.