diff --git a/pshell.pl b/pshell.pl index ba27f74..0b84f17 100755 --- a/pshell.pl +++ b/pshell.pl @@ -21,13 +21,39 @@ my $env = Environment::SQLite->new( # Signal handling setup $SIG{INT} = sub { print "\n"; }; $SIG{CHLD} = 'IGNORE'; -$SIG{WINCH} = sub { $term->resize_terminal if defined $term }; print "Perl Shell (pshell) - Type 'exit' to quit\n"; my $term = Term::ReadLine->new('pshell'); $term->ornaments(0); +# Workaround for Ctrl-R binding with Term::ReadLine::Gnu +$term->add_defun('history-search', sub { + my ($count, $key) = @_; + my $search_term = $term->readline('Search history: '); + return unless defined $search_term && length $search_term; + + my @matches = grep { $_ =~ /\Q$search_term\E/i } $history->get_all; + if (@matches) { + print "\nMatching commands:\n"; + for my $i (0..$#matches) { + print sprintf("%2d: %s\n", $i+1, $matches[$i]); + } + + my $choice = $term->readline('\nSelect command to run (number or Enter to cancel): '); + if ($choice =~ /^\d+$/ && $choice > 0 && $choice <= @matches) { + $term->addhistory($matches[$choice-1]); + $term->replace_line($matches[$choice-1]); + $term->redisplay; + } + } else { + print "\nNo matching commands found\n"; + } + + return 0; +}); +$term->parse_and_bind('"\C-r": history-search'); + # Load previous history my @history = $history->get_all; $term->addhistory($_) for @history; @@ -70,7 +96,6 @@ while (1) { } # Command not found - #print STDERR "DEBUG: Main program detected command not found\n"; print "Command not found: $command\n"; next; }