"... really does what I need but ... doesn't show correctly ... probably due to ... printing "\r" char ..."

Well, I obviously had to make an initial guess about how the textual progressbar was implemented; however, you'll be glad to know that can be very easily fixed. :-)

I rewrote the "Fill X" callback so that it gives a rough approximation of what your doProgress() is printing. I left in the all-important \r but didn't bother adding the percentage calculation.

$fill_x->configure(-command => sub { $_->configure(-state => 'disabled') for $fill_x, $fill_y; my $ms = 50 * (1 + int rand 10); my $iters = 1 + int rand 100; my $iter = 0; my $id; my $label = 'LABEL'; $id = $mw->repeat($ms, sub { if (++$iter <= $iters) { text_show($text, sprintf "\r%s%4d/%4d [%s]", $label, $iter, $iters, '=' x $iter ); } else { text_show($text, "\nDone!\n"); $id->cancel; $_->configure(-state => 'normal') for $fill_x, $fill_y; } }); });

I then added some code to text_show() to handle the \r.

{ my ($line, $char); BEGIN { ($line, $char) = (1, 0) } sub text_show { my ($text, $str) = @_; if (0 == index $str, "\r") { $str = substr $str, 1; $text->delete("$line.0", "$line.$char"); $char = 0; } $text->insert("$line.$char" => $str); if (-1 < index $str, "\n") { $line += $str =~ y/\n//; $char = length(substr $str, rindex $str, "\n") - 1; } else { $char += length $str; } $text->see("$line.$char"); } }

You may notice that I also moved the $text->see("$line.$char") statement. It logically makes more sense for this to occur after $line and $char have been recalculated; although, visually I couldn't see any difference. It probably depends on the size of the GUI window, how you've implemented scrollbars, and the value of -wrap. I'll leave you to play around with that part.

I didn't change anything else in the original code I posted. So, just change those two blocks of code; give it a whirl; and see how you go.

Minor update: With the numbers being displayed, I noticed an off-by-one error in the "Fill X" callback: I changed ++$iter < $iters to ++$iter <= $iters. The same error exists in the "Fill Y" callback: fix it if you want; it would only be noticeable if $iters randomised to 1, in which case you'd get no output.

— Ken


In reply to Re^3: GUI to dynamically show program output by kcott
in thread GUI to dynamically show program output by markong

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.