Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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=");'

In reply to External event loop for Curses::UI by cavac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found