3

I would like to log actions made in a terminal window, and convert the log to html on fly.

I already tried commands script/screen + ansi2html, but the result is not perfect: the escape sequences for line editing is not handled correctly, so in the resulting html I have a mixture for the old and the new version of the line. So if the output of the terminal is like

$> echo Original text
Original text
$> echo Other
Other

The resulting html is

 $> echo Original text
 Original text
 $> echo Othernal text
 Other

Any idea, how to solve the issue?

1 Answers1

0

The problem is not your Perl script (which should be unnecessary) but ansi2html which is incomplete.

The logfile shows that something printed some text and then

  • repeatedly added the escape sequence for clearing the remainder of the line \E[K
  • alternately adding a backspace.

In ansi2html, the script finds escape sequences, and then ignores all but a few of those which it finds, i.e.,

self.ansi_codes_prog = re.compile('\033\\\[' '(\[\\d;\]*)' '(\[a-zA-z\])')

if command not in 'mMA':

Getting rid of the repeated stuff is simplest in a loop. Here is an alternate script which works around the problems seen in ansi2html for your example:

#!/usr/bin/perl -w

while (<>) {
    my $save = "";
    while ( $save ne $_ ) {
        $save = $_;
        s/[^\010]\010\e\[K//;
    }
    $save = "";
    while ( $save ne $_ ) {
        $save = $_;
        s/[^\010]\010//;
    }
    s/\e\[\?[;0-9]*[\100-\176]//g;
    s/\e\][^\a]*\a//g;
    print;
}

and (calling that filterit), use it like this:

./filterit < 1.log | ansi2html >1.html
Thomas Dickey
  • 76,765