I need some help with an issue related with GUI construction: I need a very simple GUI to show the output of a series of commands. To give you an idea of what I'm after, it's a thing very similar to the GUI I've seen often on many Debian based distros when system updates are performed (if I remember well): a text pane/view which scrolls following whatever gets out from apt/dpkg in real-time.
My researches on CPAN have suggested that there exists something similar which could be reused in the form of Gtk2::Ex::TextView::FollowAppend. I'm posting a relatively simple thing I've came up with, but I've never touched GTK2 and I'm in a bit of a rush, so I thought I could use some help here on how to "send" the output I'm collecting with IPC::Run to the Gtk2::TextBuffer.
I'm reading the API doc/details here http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/TextBuffer.html and here http://gtk2-perl.sourceforge.net/doc/gtk2-perl-study-guide/x2662.html but to repeat I'm not really familiar with the GTK2 API, so I couldn't find the "right" signal to update the Gtk2::Ex::TextView::FollowAppend Gtk2::TextBuffer when something gets "pump-ed" into $out.
I've also tried inspecting the source of the main distribution module (Gtk2::Ex::ErrorTextDialog) to see how the Gtk2::Ex::TextView::FollowAppend is updated but it's pretty convoluted.
I should also add that the update of the buffer should take into account the fact that the commands print some textual "progress bar" which re-set the cursor very often and I fear it could cause problems if the text is just appended to the Gtk2::TextBuffer.
Please ignore the rawness of the GUI, I'm just interested in the mechanics of the update for now. Also, I'm open to suggestion on alternative GUIs (on GNU/Linux) but I feel like that I miss only some pieces here.
Thanks for the attention.
#!/usr/bin/perl use strict; use warnings FATAL => 'all'; use Gtk2 '-init'; use Gtk2::Ex::TextView::FollowAppend; use IPC::Run qw( start pump finish timeout ); use FindBin qw($Bin); my $window = Gtk2::Window->new; $window->set_title('Diabytes meters downloader'); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); $window->set_border_width(3); my $vbox = Gtk2::VBox->new(0, 6); $window->add($vbox); my $frame = Gtk2::Frame->new('Buttons'); $vbox->pack_start($frame, 1, 1, 0); $frame->set_border_width(3); my $hbox = Gtk2::HBox->new(0 ,6); $frame->add($hbox); $hbox->set_border_width(3); my $incButton = Gtk2::Button->new('_Click Me'); $hbox->pack_start($incButton, 0 , 0, 0); my $quitButton = Gtk2::Button->new('_Quit'); $hbox->pack_start($quitButton, 0, 0 , 0); $quitButton->signal_connect( clicked => sub { Gtk2::main_quit(); }); my $scrolled = Gtk2::ScrolledWindow->new; $scrolled->set_policy('never', 'always'); $vbox->pack_start($scrolled, 1,1,0); my $textBuffer = Gtk2::TextBuffer->new(); $incButton->signal_connect( clicked => sub { &makeSomeNoise($textBuffer); }); my $textView = Gtk2::Ex::TextView::FollowAppend->new_with_buffer($text +Buffer); $textView->set(wrap_mode => 'char', editable => 0); $scrolled->add($textView); $window->set_default_size(500, 250); $window->set_position('GTK_WIN_POS_CENTER'); $window->show_all; Gtk2->main; sub makeSomeNoise { my $textBuff = shift; # IPC::Run setup my $file = File::Spec->catfile($Bin, 'somefile.txt'); my $timeFile = File::Spec->catfile($Bin, 'downloadsession.tm'); my $scriptFile = File::Spec->catfile($Bin, 'downloadsession.out') +; my @cat = ('cat', $file); #my @command = qw(scriptreplay -t downloadsession.tm -s downloadse +ssion.out); my @command = ('scriptreplay', '-t', $timeFile, '-s', $scriptFile) +; my ($in, $out, $err, $t); #my $h = start( \@cat, \$in, \$out, $t = timeout( 180 ) ); my $h = start( \@command, \$in, \$out, $t = timeout( 180 ) ); while ($h->pump) { #&refreshTextBuffer($textBuffer, $out); #$textBuff->set_text($out); #print($out); } $h->finish(); }
In reply to GUI to dynamically show program output by markong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |