#!/usr/bin/perl
# Test-only: checks how fullscreen frames were written, from what a program
# wrote to a terminal of COLS x ROWS (the alternate screen, from where it is
# entered to where it is last left). Fails, naming the frame, when
# - the screen is cleared (ED 2) after the first frame;
# - anything that draws (text, a cursor move) is written outside a
# synchronized update (DECSET 2026);
# - a row is written whose cells (character and style) come out as they were,
# in a frame that did not clear the screen.
# Cells are modelled as screen.pl does: cursor moves (CUP, CR), EL, ED 2,
# autowrap on and off, every character one column wide; a cell's style is the
# SGR parameters since the last reset.
# Usage: frames.pl COLS ROWS < TRANSCRIPT
use strict;
use warnings;
my ($cols, $rows) = @ARGV;
die "usage: frames.pl COLS ROWS\n" unless $cols && $rows;
local $/;
binmode STDIN, ':raw';
my $in = <STDIN>;
utf8::decode($in);
my $on = index($in, "\e[?1049h");
my $off = rindex($in, "\e[?1049l");
die "frames: no alternate screen\n" if $on < 0;
$in = substr($in, $on + 8, ($off > $on ? $off : length $in) - $on - 8);
my @scr = map { [ map { [' ', ''] } 1 .. $cols ] } 1 .. $rows;
my ($x, $y, $style, $wrap, $pending) = (0, 0, '', 0, 0);
my ($frame, $in_sync, $first_done, $repaint) = (0, 0, 0, 0);
my (%touched, %before);
my @bad;
sub snap { my ($r) = @_; return join("\x00", map { $_->[0] . "\x01" . $_->[1] } @{ $scr[$r] }); }
sub touch { my ($r) = @_; $before{$r} = snap($r) unless exists $before{$r}; }
sub settle {
return unless %before;
my $changed = 0;
for my $r (keys %touched) { $changed = 1 if snap($r) ne $before{$r}; }
push @bad, "frame $frame: row " . join(',', map { $_ + 1 } sort { $a <=> $b } keys %touched) . " written unchanged"
if %touched && !$changed && $first_done && !$repaint; # a frame that cleared the screen writes every row
%touched = ();
%before = ();
}
while (length $in) {
if ($in =~ s/^\e\[\?2026h//) {
$in_sync = 1;
} elsif ($in =~ s/^\e\[\?2026l//) {
settle();
$in_sync = 0;
$repaint = 0;
$frame++;
$first_done = 1;
} elsif ($in =~ s/^\e\[\?7([hl])//) {
$wrap = $1 eq 'h';
} elsif ($in =~ s/^\e\[(\d*)(?:;(\d*))?H//) {
settle();
push @bad, "frame $frame: a cursor move outside a synchronized update" if $first_done && !$in_sync;
($y, $x, $pending) = (($1 || 1) - 1, (defined $2 && $2 ne '' ? $2 : 1) - 1, 0);
} elsif ($in =~ s/^\e\[([0-2]?)K//) {
my $from = $1 eq '2' ? 0 : $x;
$from = $cols - 1 if $pending && $1 ne '2';
touch($y);
$touched{$y} = 1;
$scr[$y][$_] = [' ', ''] for $from .. $cols - 1;
} elsif ($in =~ s/^\e\[2J//) {
push @bad, "frame $frame: the screen was cleared after the first frame" if $first_done;
$repaint = 1;
@scr = map { [ map { [' ', ''] } 1 .. $cols ] } 1 .. $rows;
} elsif ($in =~ s/^\e\[([0-9;]*)m//) {
my $p = $1;
$style = ($p eq '' || $p =~ /^0(;|$)/) ? substr($p, $p eq '' ? 0 : 1) : "$style;$p";
$style =~ s/^;//;
} elsif ($in =~ s/^\e\[[0-9;?<>=!\$ ]*[\@-~]// || $in =~ s/^\e\][^\a\e]*(?:\a|\e\\)// || $in =~ s/^\e[^\[\]]//) {
# not modelled
} elsif ($in =~ s/^\r//) {
($x, $pending) = (0, 0);
} elsif ($in =~ s/^\n//) {
$y++ if $y < $rows - 1;
} elsif ($in =~ s/^([^\e\r\n])//) {
push @bad, "frame $frame: text outside a synchronized update" if $first_done && !$in_sync;
if ($pending && $wrap) { $x = 0; $y++ if $y < $rows - 1; $pending = 0; }
touch($y);
$touched{$y} = 1;
$scr[$y][$x] = [$1, $style] if $y < $rows && $x < $cols;
if ($x == $cols - 1) { $pending = 1; } else { $x++; }
} else {
substr($in, 0, 1) = '';
}
}
settle();
if (@bad) {
my %seen;
print "$_\n" for grep { !$seen{$_}++ } @bad[0 .. ($#bad < 9 ? $#bad : 9)];
exit 1;
}
print "frames: $frame frames, every one synchronized, no clear, no unchanged row\n";