Commit | Line | Data |
54d0f9a0 |
1 | package Devel::REPL::Plugin::Interrupt; |
2 | |
6a5409bc |
3 | use Devel::REPL::Plugin; |
54fea9b0 |
4 | use Sys::SigAction qw(set_sig_handler); |
54d0f9a0 |
5 | use namespace::clean -except => [ 'meta' ]; |
6 | |
54fea9b0 |
7 | around 'run' => sub { |
8 | my ($orig, $self) = (shift, shift); |
54d0f9a0 |
9 | |
54fea9b0 |
10 | local $SIG{INT} = 'IGNORE'; |
11 | |
12 | return $self->$orig(@_); |
13 | }; |
14 | |
15 | around 'run_once' => sub { |
16 | my ($orig, $self) = (shift, shift); |
17 | |
18 | # We have to use Sys::SigAction: Perl 5.8+ has safe signal handling by |
19 | # default, and Term::ReadLine::Gnu restarts the interrupted system calls. |
20 | # The result is that the signal handler is not fired until you hit Enter. |
21 | my $sig_action = set_sig_handler INT => sub { |
54d0f9a0 |
22 | die "Interrupted.\n"; |
23 | }; |
24 | |
54fea9b0 |
25 | return $self->$orig(@_); |
26 | }; |
27 | |
28 | around 'read' => sub { |
29 | my ($orig, $self) = (shift, shift); |
30 | |
31 | # here SIGINT is caught and only kills the line being edited |
32 | while (1) { |
33 | my $line = eval { $self->$orig(@_) }; |
34 | return $line unless $@; |
35 | |
36 | die unless $@ =~ /^Interrupted\./; |
37 | |
38 | # (Term::ReadLine::Gnu kills the line by default, but needs a LF - |
39 | # maybe I missed something?) |
40 | print "\n"; |
41 | } |
54d0f9a0 |
42 | }; |
43 | |
44 | 1; |
45 | |
cfd1094b |
46 | __END__ |
47 | |
48 | =head1 NAME |
49 | |
50 | Devel::REPL::Plugin::Interrupt - traps SIGINT to kill long-running lines |
51 | |
54fea9b0 |
52 | =head1 DESCRIPTION |
53 | |
54 | By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this |
55 | module, SIGINT will be trapped and used to kill long-running commands |
56 | (statements) and also to kill the line being edited (like eg. BASH do). (You |
57 | can still use Ctrl-D to exit.) |
58 | |
30b459d4 |
59 | =head1 AUTHOR |
60 | |
61 | Shawn M Moore, C<< <sartak at gmail dot com> >> |
62 | |
54fea9b0 |
63 | =cut |