mzagrabe has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I am learning Curses::UI and am trying to mix printing to stdout with the curses elements. Here is a code snippet:

#!/usr/bin/perl use strict; use warnings; use Curses::UI; my $cui = new Curses::UI( -color_support => 1, ); my $window = $cui->add( "main_window", "Window", -width => 50, -height => 4, ); my $text_editor = $window->add( "description_text", "TextEditor", -text => "", -title => "Enter Description", -border => 1, -singleline => 1, -width => 40, -bfg => "red", -titlereverse => 0, -showlines => 1, -x => 0, -y => 0, ); my $okay_button = $window->add( "buttons", "Buttonbox", -border => 1, -width => 6, -bfg => "blue", -x => 40, -y => 0, -buttons => [ { -label => "Okay", -value => 1, -shortcut => "o", -onpress => sub { $cui-> +mainloopExit; }, }, ], ); $text_editor->focus; $cui->mainloop; print "outside of mainloop!\n";
However, I never see the "outside of mainloop!" printed to stdout.

Any ideas?

Thanks for the help,

-mz

Replies are listed 'Best First'.
Re: mixing curses::ui with stdout program
by Khen1950fx (Canon) on Mar 28, 2012 at 19:25 UTC
    Try the leave_curses() method.
    #!/usr/bin/perl use strict; use warnings; use Curses::UI; my $cui = new Curses::UI( -color_support => 1, ); my $window = $cui->add( "main_window", "Window", -width => 50, -height => 4, ); my $text_editor = $window->add( "description_text", "TextEditor", -text => "", -title => "Enter Description", -border => 1, -singleline => 1, -width => 40, -bfg => "red", -titlereverse => 0, -showlines => 1, -x => 0, -y => 0, ); my $okay_button = $window->add( "buttons", "Buttonbox", -border => 1, -width => 6, -bfg => "blue", -x => 40, -y => 0, -buttons => [ { -label => "Okay", -value => 1, -shortcut => "o", -onpress => sub { $cui->mainloopExit; }, }, ], ); $text_editor->focus; $cui->leave_curses; print "Outside of mainloop!\n"; $cui->mainloop;
Re: mixing curses::ui with stdout program
by bms (Monk) on Mar 28, 2012 at 16:55 UTC

    If your running on a Linux box and executing that script through a terminal, it should work. However:

    print STDOUT "Try this\n";
      Hi bms!

      Thanks for your reply. I am running this on a GNU/Linux box. Unfortunately, your suggestion does not improve the result:

      ---{/tmp/curses_ui_test}--- ... $text_editor->focus; $cui->mainloop; print STDOUT "outside of mainloop!\n"; ---{/tmp/curses_ui_test}---
      mzagrabe@achilles:/tmp$ ./curses_ui_test mzagrabe@achilles:/tmp$
      I would expect to see something between the two prompts above.

      Any other ideas? :)

      Thanks!

      -mz
Re: mixing curses::ui with stdout program
by bms (Monk) on Mar 28, 2012 at 20:14 UTC

    Alrighty, try this:

    #!/usr/bin/env perl use strict; use warnings; use Curses::UI; my $cui = new Curses::UI( -color_support => 1, ); my $window = $cui->add( "main_window", "Window", -width => 70, -height => 4, ); my $text_editor = $window->add( "description_text", "TextEditor", -text => "", -title => "Enter Description", -border => 1, -singleline => 1, -width => 40, -bfg => "red", -titlereverse => 0, -showlines => 1, -x => 0, -y => 0, ); my $okay_button = $window->add( "buttons", "Buttonbox", -border => 1, -width => 6, -bfg => "blue", -x => 40, -y => 0, -buttons => [ { -label => "Okay", -value => 1, -shortcut => "o", -onpress => sub { $cui->le +ave_curses(); print "O +utside of mainloop!\n"; sleep(2) +; $cui->re +set_curses(); }, }, ], ); my $exit_button = $window->add( "exit", "Buttonbox", -border => 1, -width => 6, -bfg => "blue", -x => 46, -y => 0, -buttons => [ { -label => "Exit", -value => 1, -shortcut => "x", -onpress => sub { $cui-> +mainloopExit; }, }, ], ); $text_editor->focus; $cui->mainloop;

    Now, using the Okay button flashes you to the terminal for a couple seconds to see the output of print and then goes back to Curses.

    Hope this helps