Re: problem with Gtk2::Helper & STDOUT




On Sep 10, 2009, at 7:16 AM, Landry Breuil wrote:

i'm asking here after turning around a strange problem for days...
i can't get to make Gtk2::Helper watch the STDOUT of the current program (not from a forked child or whatever). The idea is to do smth like 'tail' but
in a Gtk Widget or VTE Widget, but showing current process STDOUT.

As Matthew already mentioned, i don't think this is really a gtk+ issue, but the fact that STDOUT is a write-only file handle always pointing at the end.

You may get more mileage out of a minor modification of your program, making evil use of some very perlish things:


#!/usr/bin/perl -w

use strict;
use Glib 1.040, qw(TRUE FALSE);
use Gtk2;
use Data::Dumper;
use Gtk2::Helper;

Gtk2->init();

my $window = Gtk2::Window->new;
$window->signal_connect (delete_event => sub {exit});
$window->set_default_size (400, 300);
my $vbox = Gtk2::VBox->new;
$window->add ($vbox);
my $scroller = Gtk2::ScrolledWindow->new;
$vbox->add ($scroller);
my $textview = Gtk2::TextView->new;
my $buffer = $textview->get_buffer;
$buffer->create_mark ('end', $buffer->get_end_iter, FALSE);
$buffer->signal_connect (insert_text => sub { $textview- >scroll_to_mark ($buffer->get_mark ('end'), 0.0, TRUE, 0, 0.5);});
$scroller->add ($textview);
my $button = Gtk2::Button->new ('go');
$button->signal_connect (clicked => \&button_handler);
$vbox->pack_start ($button, FALSE, FALSE, 0);
$window->show_all;
Gtk2->main;
exit;

sub button_handler {
        $button->set_sensitive (FALSE);

        # create a new tied handle with a callback that will
        # copy what ever we print to the window.
        tie *STDOUTANDWINDOW, 'LineCopy', *STDOUT, sub {
                        $buffer->insert ($buffer->get_end_iter, $_[0])
                });
        select STDOUTANDWINDOW;

        print STDERR "FOO1\n";
        print "FOO2\n";

        # sleep 1 is evil, use a timeout, instead.
        Glib::Timeout->add (1000, sub { Gtk2->main_quit; FALSE });
        Gtk2->main;

        print STDOUTANDWINDOW "FOO3\n";
        $buffer->insert ($buffer->get_end_iter, "done");
        $button->set_sensitive (TRUE);

        # undo what we did.
        untie *STDOUTANDWINDOW;
        select STDOUT;
};



package LineCopy;

sub TIEHANDLE {
        my ($class, $handle, $callback) = @_;
return bless { buffer => '', handle => $handle, callback => $callback };
}

sub PRINT {
        my $self = shift;
        foreach (@_) {
               $self->{callback} ($_) if $self->{callback};
               print { $self->{handle} } $_;
        }
}



--
The trees on the bus go "pyoo pyoo pyoo..."
  -- Yvonne, singing, um, something




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]