SYNOPSIS

\*(C`Term::TtyRec::Plus\*(C' is a module that lets you read ttyrec files. The related module, Term::TtyRec is designed more for simple interactions. \*(C`Term::TtyRec::Plus\*(C' gives you more information and, using a callback, lets you munge the data block and timestamp. It will do all the subtle work of making sure timing is kept consistent, and of rebuilding each frame header.

    use Term::TtyRec::Plus;
    # complete (but simple) ttyrec playback script

    foreach my $file (@ARGV) {
      my $ttyrec = Term::TtyRec::Plus->new(infile => $file, time_threshold => 10);
      while (my $frame_ref = $ttyrec->next_frame()) {
        select undef, undef, undef, $frame_ref->{diff};
        print $frame_ref->{data};
      }
    }

CONSTRUCTOR AND STARTUP

\fInew()\fP

Creates and returns a new \*(C`Term::TtyRec::Plus\*(C' object.

my $ttyrec = Term::TtyRec::Plus->new();

Parameters

Here are the parameters that \*(C`Term::TtyRec::Plus->new()\*(C' recognizes.

infile

The input filename. A value of "-", which is the default, or \*(C`undef\*(C', means \*(C`STDIN\*(C'.

filehandle

The input filehandle. By default this is \*(C`undef\*(C'; if you have already opened the ttyrec then you can pass its filehandle to the constructor. If both filehandle and infile are defined, filehandle is used.

bzip2

Perform bzip2 decompression. By default this is \*(C`undef\*(C', which signals that bzip2 decompression should occur if and only if the filename is available and it ends in \*(L".bz2\*(R". Otherwise, you can force or forbid decompression by setting bzip2 to a true or false value, respectively. After the call to new, this field will be set to either 1 if decompression is enabled or 0 if it is not.

time_threshold

The maximum difference between two frames, in seconds. If \*(C`undef\*(C', which is the default, there is no enforced maximum. The second most common value would be 10, which some ttyrec utilities (such as timettyrec) use.

frame_filter

A callback, run for each frame before returning the frame to the user of \*(C`Term::TtyRec::Plus\*(C'. This callback receives three arguments: the frame text, the timestamp, and the timestamp of the previous frame. All three arguments are passed as scalar references. The previous frame's timestamp is \*(C`undef\*(C' for the first frame. The return value is not currently looked at. If you modify the timestamp, the module will make sure that change is noted and respected in further frame timestamps. Modifications to the previous frame's timestamp are currently ignored. sub halve_frame_time_and_stumblify { my ($data_ref, $time_ref, $prev_ref) = @_; $$time_ref = $$prev_ref + ($$time_ref - $$prev_ref) / 2 if defined $$prev_ref; $$data_ref =~ s/Eidolos/Stumbly/g; }

State

In addition to passing arguments, you can modify \*(C`Term::TtyRec::Plus\*(C''s initial state, if you want to. This could be useful if you are chaining multiple ttyrecs together; you could pass a different initial frame. Support for such chaining might be added in a future version.

frame

The initial frame number. Default 0.

prev_timestamp

The previous frame's timestamp. Default \*(C`undef\*(C'.

accum_diff

The accumulated difference of all frames seen so far; see the section on \*(C`diffed_timestamp\*(C' in \*(C`next_frame()\*(C''s return value. Default 0.

relative_time

The time passed since the first frame. Default 0.

METHODS

\fInext_frame()\fP

\*(C`next_frame()\*(C' reads and processes the next frame in the ttyrec. It accepts no arguments. On \s-1EOF\s0, it will return \*(C`undef\*(C'. On malformed ttyrec input, it will die. If it cannot reconstruct the header of a frame (which might happen if the callback sets the timestamp to -1, for example), it will die. Otherwise, a hash reference is returned with the following fields set.

data

The frame data, filtered through the callback. The original data block is not made available.

orig_timestamp

The frame timestamp, straight out of the file.

diffed_timestamp

The frame timestamp, with the accumulated difference of all of the previous frames applied to it. This is so consistent results are given. For example, if your callback adds three seconds to frame 5's timestamp, then frame 6's diffed timestamp will take into account those three seconds, so frame 6 happens three seconds later as well. So the net effect is frame 5 is extended by three seconds, and no other frames' relatives times are affected.

timestamp

The diffed timestamp, filtered through the callback.

prev_timestamp

The previous frame's timestamp (after diffing and filtering; the originals are not made available).

diff

The difference between the current frame's timestamp and the previous frame's timestamp. Yes, it is equivalent to \*(C`timestamp - prev_timestamp\*(C', but it is provided for convenience. On the first frame it will be 0 (not \*(C`undef\*(C').

orig_header

The 12-byte frame header, straight from the file.

header

The 12-byte frame header, reconstructed from \*(C`data\*(C' and \*(C`timestamp\*(C' (so, after filtering, etc.).

frame

The frame number, using 1-based indexing.

relative_time

The time between the first frame's timestamp and the current frame's timestamp.

\fIgrep()\fP

Returns the next frame that meets the specified criteria. \*(C`grep()\*(C' accepts arguments that are subroutines, regex, or strings; anything else is a fatal error. If you pass multiple arguments to \*(C`grep()\*(C', each one must be true. The subroutines receive the frame reference that is returned by \*(C`next_frame()\*(C'. You can modify the frame, but do so cautiously.

my $next_jump_frame_ref = $t->grep("Where do you want to jump?", sub { $_[0]{data} !~ /Message History/});

\fIrewind()\fP

Rewinds the ttyrec to the first frame and resets state variables to their initial values. Note that if \*(C`filehandle\*(C' is not seekable (such as \s-1STDIN\s0 on some systems, or if bzip2 decompression is used), \*(C`rewind()\*(C' will die.

\fIinfile()\fP

Returns the infile passed to the constructor. If a filehandle was passed, this will be \*(C`undef\*(C'.

\fIfilehandle()\fP

Returns the filehandle passed to the constructor, or if \*(C`infile\*(C' was used, a handle to \*(C`infile\*(C'.

\fIbzip2()\fP

Returns 1 if bzip2 decompression has taken place, 0 if it has not.

\fItime_threshold()\fP

Returns the time threshold passed to the constructor. By default it is \*(C`undef\*(C'.

\fIframe_filter()\fP

Returns the frame filter callback passed to the constructor. By default it is \*(C`sub { @_ }\*(C'.

\fIframe()\fP

Returns the frame number of the most recently returned frame.

\fIprev_timestamp()\fP

Returns the timestamp of the most recently returned frame.

\fIrelative_time()\fP

Returns the time so far since the first frame.

\fIaccum_diff()\fP

Returns the total time difference between timestamps and filtered timestamps. \*(C`accum_diff\*(C' is added to each frame's timestamp before they are passed to the \*(C`frame_filter\*(C' callback.

AUTHOR

Shawn M Moore, \*(C`[email protected]\*(C'

CAVEATS

  • Ttyrecs are frame-based. If you are trying to modify a string that is broken across multiple frames, it will not work. Say you have a ttyrec that prints \*(L"foo\*(R" in frame one and \*(L"bar\*(R" in frame two, both with the same timestamp. In a ttyrec player, it might look like these are one frame (with data \*(L"foobar\*(R"), but it's not. There is no easy, complete way to add arbitrary substitutions; you would have to write (or reuse) a terminal emulator.

  • If you modify the data block, weird things could happen. This is especially true of escape-code-littered ttyrecs (such as those of NetHack). For best results, pretend the data block is an executable file; changes are \s-1OK\s0 as long as you do not change the length of the file. It really depends on the ttyrec though.

  • If you modify the timestamp of a frame so that it is not in sequence with other frames, the behavior is undefined (it is up to the client program). \*(C`Term::TtyRec::Plus\*(C' will not reorder the frames for you.

  • bzip2 support is transparent, mostly. Unfortunately IO::Uncompress::Bunzip2 is rather slow. I took a lengthy (~4 hours), bzipped ttyrec and ran a simple script on it, depending on the built-in bzip2 decompression. This took nearly four minutes. Using bunzip2 then the same script took about four seconds. So when you can, do explicit bzip2 decompression. Or better yet, help out the guys working on IO::Uncompress::Bunzip2. :)

COPYRIGHT & LICENSE

Copyright 2006-2009 Shawn M Moore, all rights reserved.

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