Experienced Monks!

I have a command-line app that parses commands and prints the output to the terminal. In search of new features, I'm migrating the terminal library from readline to tickit.

To simplify this changeover, my plan is to use open and select to direct the default output filehandle to a $variable, and then periodically dump the contents of $variable to a tickit widget.

I'm close, but my naive implementation loses every other line of output. Seems like I need to away to ensure that writes to $command_output are held up while printing and then deleting the contained text. Some kind of first-in-last-out buffer with synchronization.

Will be grateful for any hints on an easy way to accomplish this. Here is sample code demonstrating my conundrum:

#!/usr/bin/env perl use v5.36; =comment vbox - root scrollbox vbox static static ... entry =cut use Tickit::Async; use Tickit::Widgets qw(ScrollBox Static VBox Entry); use IO::Async::Loop; use IO::Async::Timer::Periodic; use IO::Async::Timer::Countdown; use IO::Async::Loop::Select; STDOUT->autoflush; my $text = {}; $text->{loop} = IO::Async::Loop->new; my $root = Tickit::Widget::VBox->new; my $vbox = Tickit::Widget::VBox->new; # contains multiple item +s to scroll through my $scrollbox = Tickit::Widget::ScrollBox->new->set_child( $vbox ); for (1..100){ my $a = 100 - $_; $vbox->add( Tickit::Widget::Static->new( text => "a hundred bottles +minus $_ is $a \n" )) } my $tickit = Tickit::Async->new( root => $root); my $term = $tickit->term; my $lines = $term->lines; $root->add($scrollbox, force_size => $lines - 1); # , expand => 1); my $entry = Tickit::Widget::Entry->new( text => "enter command > ", on_enter => sub { my ( $self, $line ) = @_; print_to_terminal($line); $scrollbox->scroll_to(1e5); $line =~ s/^.+?>\s*//; $self->set_text(''); my $prompt = 'enter command > '; $self->set_text($prompt); $self->set_position(99); } ); my $prompt = 'enter command > '; $entry->set_text($prompt); $entry->set_position(99); $root->add($entry); my $i; redirect_stdout(); timer(0.5,0.5, sub{ ++$i; say(join'',$i,'-','X'x40)}); #timer(2,1, sub{ print( join '',1..40) }); $tickit->run; sub prompt { my $prompt = 'enter command > '; $entry->set_text($prompt); $entry->set_position(99); } our ($command_output, $output_fh, $old_output_fh); sub redirect_stdout { open($output_fh, '>', \$command_output) or die; $output_fh->autoflush; $old_output_fh = select $output_fh; my $j; timer(1, 1, sub{ return unless $command_output; print_to_terminal($command_output); $command_output = "" }); } sub restore_stdout { select $old_output_fh; close $output_fh; } sub print_to_terminal ($txt) { $vbox->add( Tickit::Widget::Static->new( text => $txt )); $scrollbox->scroll_to(1e5); } sub timer ($delay, $interval, $coderef ) { my $timer; if ($interval == 0){ $timer = IO::Async::Timer::Countdown->new( delay => $delay, on_expire => $coderef, ); } else { $timer = IO::Async::Timer::Periodic->new( interval => $interval, on_tick => $coderef, ); } $timer->start; $text->{loop}->add($timer); $timer }

In reply to Race when redirecting output. by gnosti

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.