0

When I run this script it works as wanted as long as I don't redirect STDOUT to a file. When I redirect STDOUT to a file, the output after the ENTER: breaks. Why doesn't the script work any more if I redirect the STDOUT to a file?

#!/usr/bin/env perl
use warnings;
use strict;
use Term::ReadKey;
use Unicode::GCString;
use Term::ANSIScreen qw( :all );

select( *STDERR );
$| = 1;


print YELLOW "YELLOW\n";
print RESET;

print "ENTER:";
my $dummy = <>;

print savepos;
my $str = '';
print_readline( $str );
for my $s ( 33 .. 126 ) {
    $str .= '  ' . chr( $s ) x 5;
    print_readline( $str );
}
print "\n";

sub print_readline {
    my ( $str ) = @_;
    my $gcs = Unicode::GCString->new( $str );
    my $up = int( $gcs->columns() / ( GetTerminalSize )[0] );
    print loadpos;
    if ( $up ) {
        print "\n" x $up, up( $up );
    }
    print cldown, savepos, $str;
}
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • Please describe how it breaks in more detail. What happens, or fails to happen when you redirect STDOUT? – Len Jaffe Jul 17 '14 at 15:14
  • Ever `print` of `$str` gets appended to the previous print. It seems that the restoring of the cursor position doesn't work if STDOUT is redirected. – sid_com Jul 17 '14 at 15:55
  • Well, yes. a file is not a cursor addressable TTY, although a cursor addressable tty might act like a file in some ways. – Len Jaffe Jul 17 '14 at 16:20
  • @Len Jaffe: But I have `select`ed `STDERR` in the script. – sid_com Jul 17 '14 at 16:36

1 Answers1

1

The driver for a TTY might have code to respond to specific character sequences by moving the cursor around the screen, or changing colors, but the default driver for text files has no such code.

If you want to capture the output of a terminal session so that you can watch it in 'instant replay', I know of a command names script which records everything printed to your terminal into a file, which you can then read with an editor, or cat back to the screen to watch it replay.

You may need to make a decision between capturing the output, and using the ANSIScreen module.

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28