Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

External event loop for Curses::UI

by cavac (Curate)
on Feb 27, 2019 at 12:02 UTC ( #1230611=perlmeditation: print w/replies, xml ) Need Help??

Since i've been stumped by this before, and so have probably many others, i want to show how to do external event loops (main loops) when using Curses::UI.

By external i mean using your own, not the one provided by Curses::UI.

This is somewhat loosely based on some of the tutorial code included in the package to make the window as well as some quickly hacked together code. Sorry about that. Control-X opens the menu, Control-Q quits the application.

First of all, we need to make sure we got a read timeout for the input. So, make sure this is set correctly right after getting a new UI instance:

use Curses::UI; my $cui = new Curses::UI( -color_support => 1); $cui->{-read_timeout} = 0;

In our (rather simple) main loop, whenever we update the screen, we call draw() and do_one_event(). In every loop, we also call do_one_event() after our sleeping period to handle any user input:

my $last = time; while(1) { $cui->do_one_event(); my $now = time; if($last != $now) { $last = $now; foreach my $sensor (@sensors) { my $newtext = int(rand(1000) * 100) / 100; $sensor->text($newtext); } $cui->draw(); $cui->do_one_event(); } else { sleep(0.01); } }

Just for reference, here is the complete, runable example, in all its indent-mismatched-copy-and-paste glory:

#!/usr/bin/perl -w use strict; use warnings; use Time::HiRes qw[sleep]; use Curses::UI; my $cui = new Curses::UI( -color_support => 1); $cui->{-read_timeout} = 0; my @menu = ( { -label => 'File', -submenu => [ { -label => 'Exit ^Q', -value => \&exit_dialog } ] }, ); my $menu = $cui->add( 'menu','Menubar', -menu => \@menu, -fg => "white", -bg => "blue", ); my $win1 = $cui->add( 'win1', 'Window', -border => 1, -y => 1, -bfg => 'red', ); sub exit_dialog() { my $return = $cui->dialog( -message => "Do you really want to quit?", -title => "Are you sure???", -buttons => ['yes', 'no'], ); exit(0) if $return; } my @labels; my @sensors; for(my $i = 1; $i <= 3; $i++) { push @labels, $win1->add('label' . $i, 'Label', -width => 14, -height => 1, -text => 'Sensor ' . $i . ' :', -x => 4, -y => 3 + $i, ); push @sensors, $win1->add('sensordata' . $i, 'Label', -width => 10, -height => 1, -text => '----------', -x => 19, -y => 3 + $i, ); } $cui->set_binding(sub {$menu->focus()}, "\cX"); $cui->set_binding( \&exit_dialog , "\cQ"); my $last = time; while(1) { $cui->do_one_event(); my $now = time; if($last != $now) { $last = $now; foreach my $sensor (@sensors) { my $newtext = int(rand(1000) * 100) / 100; $sensor->text($newtext); } $cui->draw(); $cui->do_one_event(); } else { sleep(0.01); } }
perl -e 'use MIME::Base64; print decode_base64("4pmsIE5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCiAgTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duLi4uIOKZqwo=");'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1230611]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2021-02-27 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?