#!/usr/bin/perl
# Test-only: replays what a program wrote to a terminal of COLS x ROWS and
# prints the screen, one row per line, trailing spaces dropped. With --alt,
# the alternate screen as it was just before it was last left (fullscreen's
# final frame). It models what the checks need and no more: cursor moves
# (CUP, CUU, CUD, CHA, CR, LF), erases (EL 0/2, ED 0/2), the alternate
# screen, and xterm's deferred wrap: a character written in the last column
# leaves the cursor there, so an EL that follows erases that character.
# Escape sequences it does not model are skipped; every character is one
# column wide.
# Usage: screen.pl [--alt] COLS ROWS < TRANSCRIPT
use strict;
use warnings;
my $alt_wanted = 0;
if (@ARGV && $ARGV[0] eq '--alt') { $alt_wanted = 1; shift @ARGV; }
my ($cols, $rows) = @ARGV;
die "usage: screen.pl [--alt] COLS ROWS\n" unless $cols && $rows;
local $/;
binmode STDIN, ':raw';
my $in = <STDIN>;
utf8::decode($in);
sub blank { return [ (' ') x $cols ]; }
my (@main, @alt);
push @main, blank() for 1 .. $rows;
push @alt, blank() for 1 .. $rows;
my $scr = \@main;
my ($x, $y, $pending) = (0, 0, 0);
my $last_alt;
sub scroll_up { shift @$scr; push @$scr, blank(); }
sub lf { if ($y == $rows - 1) { scroll_up(); } else { $y++; } }
sub dump_screen {
my ($s) = @_;
my $out = '';
for my $r (@$s) { my $l = join('', @$r); $l =~ s/ +$//; $out .= "$l\n"; }
return $out;
}
my $i = 0;
my $n = length $in;
while ($i < $n) {
my $c = substr($in, $i, 1);
if ($c eq "\e") {
my $next = substr($in, $i + 1, 1);
if ($next eq '[') {
my $j = $i + 2;
$j++ while $j < $n && substr($in, $j, 1) !~ /[\x40-\x7e]/;
my $params = substr($in, $i + 2, $j - $i - 2);
my $fin = substr($in, $j, 1);
$i = $j + 1;
my $priv = $params =~ s/^\?//;
my @p = map { $_ eq '' ? 0 : $_ + 0 } split /;/, $params, -1;
if ($priv) {
if ($params eq '1049' && ($fin eq 'h' || $fin eq 'l')) {
if ($fin eq 'h') {
@alt = ();
push @alt, blank() for 1 .. $rows;
$scr = \@alt;
} else {
$last_alt = dump_screen(\@alt) if $scr == \@alt;
$scr = \@main;
}
$pending = 0;
}
next;
}
if ($fin eq 'H' || $fin eq 'f') {
$y = ($p[0] || 1) - 1;
$x = ($p[1] || 1) - 1;
$y = $rows - 1 if $y >= $rows;
$x = $cols - 1 if $x >= $cols;
$pending = 0;
} elsif ($fin eq 'A') {
$y -= $p[0] || 1; $y = 0 if $y < 0; $pending = 0;
} elsif ($fin eq 'B') {
$y += $p[0] || 1; $y = $rows - 1 if $y >= $rows; $pending = 0;
} elsif ($fin eq 'G') {
$x = ($p[0] || 1) - 1; $x = $cols - 1 if $x >= $cols; $pending = 0;
} elsif ($fin eq 'K') {
my $m = $p[0] || 0;
my ($from, $to) = $m == 2 ? (0, $cols - 1) : $m == 1 ? (0, $x) : ($x, $cols - 1);
$scr->[$y][$_] = ' ' for $from .. $to;
} elsif ($fin eq 'J') {
my $m = $p[0] || 0;
if ($m == 2 || $m == 3) {
$scr->[$_] = blank() for 0 .. $rows - 1;
} elsif ($m == 0) {
$scr->[$y][$_] = ' ' for $x .. $cols - 1;
$scr->[$_] = blank() for $y + 1 .. $rows - 1;
}
}
next;
} elsif ($next eq ']') {
my $j = $i + 2;
$j++ while $j < $n && substr($in, $j, 1) ne "\a" && substr($in, $j, 2) ne "\e\\";
$i = $j < $n && substr($in, $j, 1) eq "\a" ? $j + 1 : $j + 2;
next;
}
$i += 2;
next;
}
$i++;
if ($c eq "\r") { $x = 0; $pending = 0; next; }
if ($c eq "\n") { lf(); $pending = 0; next; }
next if ord($c) < 0x20;
if ($pending) { $x = 0; lf(); $pending = 0; }
$scr->[$y][$x] = $c;
if ($x == $cols - 1) { $pending = 1; } else { $x++; }
}
my $out = $alt_wanted ? ($last_alt // dump_screen(\@alt)) : dump_screen($scr);
binmode STDOUT, ':utf8';
print $out;