Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by gnosti (Chaplain)
on Jul 03, 2019 at 18:45 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-25 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found