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