Add Ctrl-R history search and remove WINCH signal handler and debug output
This commit is contained in:
parent
9204a8f519
commit
088c61e19b
1 changed files with 27 additions and 2 deletions
29
pshell.pl
29
pshell.pl
|
@ -21,13 +21,39 @@ my $env = Environment::SQLite->new(
|
||||||
# Signal handling setup
|
# Signal handling setup
|
||||||
$SIG{INT} = sub { print "\n"; };
|
$SIG{INT} = sub { print "\n"; };
|
||||||
$SIG{CHLD} = 'IGNORE';
|
$SIG{CHLD} = 'IGNORE';
|
||||||
$SIG{WINCH} = sub { $term->resize_terminal if defined $term };
|
|
||||||
|
|
||||||
print "Perl Shell (pshell) - Type 'exit' to quit\n";
|
print "Perl Shell (pshell) - Type 'exit' to quit\n";
|
||||||
|
|
||||||
my $term = Term::ReadLine->new('pshell');
|
my $term = Term::ReadLine->new('pshell');
|
||||||
$term->ornaments(0);
|
$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
|
# Load previous history
|
||||||
my @history = $history->get_all;
|
my @history = $history->get_all;
|
||||||
$term->addhistory($_) for @history;
|
$term->addhistory($_) for @history;
|
||||||
|
@ -70,7 +96,6 @@ while (1) {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Command not found
|
# Command not found
|
||||||
#print STDERR "DEBUG: Main program detected command not found\n";
|
|
||||||
print "Command not found: $command\n";
|
print "Command not found: $command\n";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue