Term::TtyRec::Player - playbacks ttyrec data


Term-TtyRec documentation Contained in the Term-TtyRec distribution.

Index


Code Index:

NAME

Top

Term::TtyRec::Player - playbacks ttyrec data

SYNOPSIS

Top

  use Term::TtyRec::Player;
  use FileHandle;

  # $handle is any IO::* object
  my $handle = FileHandle->new('file.tty');
  my $player = Term::TtyRec::Player->new($handle);

  # options can be set as hashref
  my $player = Term::TtyRec::Player->new($handle, {
      speed => 1, nowait => undef,
  });

DESCRIPTION

Top

Term::TtyRec::Player playbacks ttyrec recorded data. See pttyplay and Term::TtyRec for details about ttyrec.

METHODS

Top

new
  $player = Term::TtyRec::Player->new($handle, \%attr);

constructs new Term::TtyRec::Player instance.

play
  $player->play();

Plays recorded data on your terminal.

peek
  $player->peek();

Plays live-recoded data to your terminal. This method implements ttyplay -p option.

AUTHOR

Top

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

Term::TtyRec, pttyplay, http://namazu.org/~satoru/ttyrec/


Term-TtyRec documentation Contained in the Term-TtyRec distribution.

package Term::TtyRec::Player;

use strict;
use vars qw($VERSION);
$VERSION = '0.03';

use Time::HiRes qw(sleep);
use Term::TtyRec;

sub new {
    my($proto, $io, $attr) = @_;
    my $class = ref $proto || $proto;
    my %attr = ( speed => 1, nowait => undef, %{$attr || {}} ); # default

    unless ($io->can('read')) {
	require Carp;
	Carp::croak('Usage: Term::TtyRec::Player->new($io)');
    }

    bless { ttyrec => Term::TtyRec->new($io), %attr }, $class;
}

sub play {
    my $self = shift;
    local $| = 1;

    my $prev;
    while (my($sec, $text) = $self->ttyrec->read_next) {
	my $wait = ($sec - $prev) / $self->speed if defined $prev;
	unless ($self->nowait or ! $prev) {
	    sleep $wait;
	}
	print $text;
	$prev = $sec;
    }
}

sub skip_all {
    my $self = shift;
    1 while $self->ttyrec->read_next;
}

sub peek {
    my $self = shift;
    $self->skip_all;

    while (1) {
	$self->play;
	sleep 0.25;
    }
}

### accessor
for my $attr (qw(ttyrec speed nowait)) {
    no strict 'refs';
    *{__PACKAGE__. "::$attr"} = sub {
	my $self = shift;
	$self->{$attr} = shift if @_;
	$self->{$attr};
    };
}


1;
__END__