I am pretty new to Perl, and writing my first program in Gtk2. I have a program that executes a string of shell commands when a button is pressed. I am trying to get the STDOUT/STDERR from the console and into a TextView box that I have. I have it somewhat working, with the following code:
# Code to open the filehandle
my $fh = FileHandle->new();
open ($fh, "tail -f /usr/rip/cdimport.log |");
# Code to draw the actual textview
$frame = Gtk2::Frame->new('Console Window');
my $ctextBox = Gtk2::TextView->new;
my $cscroll = Gtk2::ScrolledWindow->new;
my $cevent_box = Gtk2::EventBox->new;
$color = Gtk2::Gdk::Color->parse ("black");
$cevent_box->modify_bg ('normal', $color);
my $calign = Gtk2::Alignment->new (0.5, 0.5, 1.0, 1.0);
$calign->set_border_width (1);
$cevent_box->add ($calign);
$calign->add ($ctextBox);
$cscroll->add($cevent_box);
my $cbuffer = $ctextBox->get_buffer;
my $cend_mark = $cbuffer->create_mark( 'end', $cbuffer->get_end_iter,
+FALSE );
$cbuffer->signal_connect(
insert_text => sub {
$ctextBox->scroll_to_mark( $cend_mark, 0.0, TRUE, 0.0, 0.0 );
}
);
$frame->add($cscroll);
$box1->pack_end($frame, TRUE, TRUE, 0);
$ctextBox->set_buffer($cbuffer);
$ctextBox->set_editable(FALSE);
$cbuffer->set_text("Console window");
# Code to set up the watch on the opened filehandle
my $ctag;
$ctag = Gtk2::Helper->add_watch ( $fh->fileno, 'in', sub {
watch_console ($ctag, $fh, $cbuffer) } );
sub watch_console {
my ($fd, $fh, $buffer) = @_;
my $sysbuffer;
if ( not sysread($fh, $sysbuffer, 4096) ) {
# obviously the connected pipe was closed
Gtk2::Helper->remove_watch ($fd)
or die "couldn't remove watcher";
close($fh);
return 1;
}
$buffer->set_text($sysbuffer);
return 1;
}
Then I would run the system commands with the following:
system("find /tmp/$id/ -iname \"*.wav\" -print0 | xargs -0 -n
+1 -I '{}' ffmpeg -i '{}' -f mp3 '{}'.mp3 2>> /usr/rip/cdimport.log")
This works, to an extent. The refresh of the textview is slow and fragmented. Any suggestions on a better implementation?BTW, I know that the scrollview doesn't work in the above piece of code - I'll figure that one out next.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.