Term::...::Plus(3pm)
β¨ NAME β¨
Term::TtyRec::Plus β read a ttyrec π
(and yes bestie, the manual page itself is filed under Term::...::Plus(3pm) because man page naming is a whole vibe π
)
πΈ SYNOPSIS πΈ
okay so Term::TtyRec::Plus is the module that lets you read ttyrec files. Its sibling, Term::TtyRec, is more about simple interactions β this one is the upgrade arc π. Term::TtyRec::Plus hands you way more info, and with a callback you can munge the data block and the timestamp however you want. It does all the subtle background work of keeping the timing consistent and rebuilding each frame header for you. no cap.
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 π¦
new()
Creates and returns a brand-new Term::TtyRec::Plus object, like a lil baby ttyrec reader πΌβ¨
my $ttyrec = Term::TtyRec::Plus->new();
π Parameters
These are the parameters that Term::TtyRec::Plus->new() recognizes. Periodt.
infileβ the input filename. The value-(which is the default) orundefboth meanSTDIN. iykyk.filehandleβ the input filehandle. Default isundef. If you've already opened the ttyrec yourself, you can pass its filehandle right into the constructor. If bothfilehandleandinfileare defined,filehandlewins. πbzip2β do the bzip2 decompression thing. Default isundef, which means: decompress if and only if the filename is available and it ends in.bz2. Otherwise you can force it or forbid it by settingbzip2to a true or false value respectively. After the call tonew, this field gets set to1if decompression is enabled, or0if it isn't. slay, self-documenting.time_thresholdβ the maximum difference between two frames, in seconds. If it'sundef(the default) there's no enforced maximum. The second most common value would be10, which some ttyrec utilities (such astimettyrec) use. πframe_filterβ a callback, run for each frame before the frame gets returned to the user ofTerm::TtyRec::Plus. This callback receives three arguments: the frame text, the timestamp, and the timestamp of the previous frame. All three come in as scalar references. The previous frame's timestamp isundeffor the first frame. The return value is not currently looked at, okurrr. 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
Besides passing arguments, you can also modify Term::TtyRec::Plus's initial state if you want to. This could be useful if you're chaining multiple ttyrecs together β you could pass a different initial frame. Support for such chaining might be added in a future version (manifesting it β¨).
frameβ the initial frame number. Default0.prev_timestampβ the previous frame's timestamp. Defaultundef.accum_diffβ the accumulated difference of all frames seen so far; peep the section ondiffed_timestampinnext_frame()'s return value. Default0.relative_timeβ the time passed since the first frame. Default0.
π₯ METHODS π₯
next_frame()
next_frame() reads and processes the next frame in the ttyrec. It takes no arguments. On EOF, it returns undef. 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, you get back a hash reference with these fields all filled in:
dataβ the frame data, filtered through the callback. The original data block is not made available. sorry bestie.orig_timestampβ the frame timestamp, straight out of the file.diffed_timestampβ the frame timestamp with the accumulated difference of all the previous frames applied to it. This exists so you get consistent results. For example, if your callback adds three seconds to frame 5's timestamp, then frame 6's diffed timestamp will take those three seconds into account, so frame 6 happens three seconds later as well. The net effect: frame 5 is extended by three seconds, and no other frames' relative times are affected. iconic behaviour, honestly.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 totimestamp - prev_timestamp, but it's provided for convenience. On the first frame it will be0(notundef). πorig_headerβ the 12-byte frame header, straight from the file.headerβ the 12-byte frame header, reconstructed fromdataandtimestamp(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.
grep()
Returns the next frame that meets the specified criteria. grep() accepts arguments that are subroutines, regex, or strings; anything else is a fatal error. If you pass multiple arguments to grep(), every single one of them must be true. The subroutines receive the frame reference that is returned by next_frame(). You can modify the frame β but do it cautiously, bestie. β οΈ
my $next_jump_frame_ref = $t->grep("Where do you want to jump?", sub { $_[0]{data} !~ /Message History/});
rewind()
Rewinds the ttyrec to the first frame and resets the state variables to their initial values. Heads up: if filehandle is not seekable (such as STDIN on some systems, or if bzip2 decompression is used), rewind() will die. π
infile()
Returns the infile passed to the constructor. If a filehandle was passed instead, this will be undef.
filehandle()
Returns the filehandle passed to the constructor, or, if infile was used, a handle to infile.
bzip2()
Returns 1 if bzip2 decompression has taken place, 0 if it has not.
time_threshold()
Returns the time threshold passed to the constructor. By default it's undef.
frame_filter()
Returns the frame filter callback passed to the constructor. By default it is sub { @_ }.
frame()
Returns the frame number of the most recently returned frame.
prev_timestamp()
Returns the timestamp of the most recently returned frame.
relative_time()
Returns the time so far since the first frame.
accum_diff()
Returns the total time difference between timestamps and filtered timestamps. accum_diff is added to each frame's timestamp before they get passed to the frame_filter callback. π
π AUTHOR π
Shawn M Moore, sartak@gmail.com π«Ά
π CAVEATS π
- Ttyrecs are frame-based. If you're trying to modify a string that's broken across multiple frames, it will not work. Say you have a ttyrec that prints
fooin frame one andbarin frame two, both with the same timestamp. In a ttyrec player, it might look like these are one frame (with datafoobar), but it's not. There is no easy, complete way to add arbitrary substitutions; you would have to write (or reuse) a terminal emulator. π that's just how it is. - 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 OK 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's not in sequence with other frames, the behavior is undefined (it is up to the client program).
Term::TtyRec::Pluswill not reorder the frames for you. π you're on your own for that one. - bzip2 support is transparent, mostly. Unfortunately
IO::Uncompress::Bunzip2is 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. Usingbunzip2then the same script took about four seconds. So when you can, do explicit bzip2 decompression. Or better yet, help out the guys working onIO::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.
and that's the whole page, bestie π this one ships as perl v5.32.0, dated 2021-01-07, and it lives in the manual as Term::...::Plus(3pm) β go forth and read those ttyrecs, you absolute icon π
π¦ where this page comes from
- debian bookworm: libterm-ttyrec-plus-perl 0.09-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libterm-ttyrec-plus-perl 0.09-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libterm-ttyrec-plus-perl 0.09-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libterm-ttyrec-plus-perl 0.09-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms