Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Catching INT in Term::ReadLine::Gnu->readline()

by gnosti (Hermit)
on Jul 03, 2019 at 18:45 UTC ( #11102373=note: print w/replies, xml ) Need Help??


in reply to Catching INT in Term::ReadLine::Gnu->readline()

Hi,

I use Terminal::ReadLine::Gnu with the AnyEvent asynchronous framework in an application and manage to immediately catch Ctrl-C.

Edit

I am replacing previous code with a working example

use Term::ReadLine; use AnyEvent; use Event; use strict; use warnings; my $events = {}; my $text = {}; my $term = Term::ReadLine->new("MyApp"); ($text->{screen_lines}, $text->{screen_columns}) = $term->get_screen_s +ize(); # create a STDIN watcher $events->{stdin} = AE::io(*STDIN, 0, sub { &{$term->Attribs->{'callback_read_char'}}(); }); $term->callback_handler_install(prompt(), \&process_line); # handle Control-C from terminal $events->{sigint} = AE::signal('INT', \&cleanup_exit); $term->stuff_char(10); # necessary to respond to Ctrl-C at first promp +t &{$term->Attribs->{'callback_read_char'}}(); print prompt(); $Event::DIED = sub { my ($event, $errmsg) = @_; print($errmsg); $term->Attribs->{line_buffer} = q(); if($term){ $term->clear_message(); $term->rl_reset_line_state(); } }; Event::loop(); sub cleanup_exit { print "\n caught $SIG{INT}",@_,"\n"; $term->rl_deprep_terminal(); exit; } sub prompt {"Enter command : "} sub process_line { my ($input) = @_; print "got some text: $input\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11102373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-02-21 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (95 votes). Check out past polls.

    Notices?