Advertisement
Guest User

Beginnings of ANSI Escape displayer

a guest
Oct 22nd, 2010
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.37 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. @ARGV
  5.     or die "Supply an ANSI Escaped file name parameter";
  6.  
  7. foreach my $fname (@ARGV) {
  8.  
  9.     open O, '>', "$fname.uncontrol"
  10.         or die;
  11.  
  12.     open I, $fname or die;
  13.     my $c;
  14.  
  15.     while (read I, $c, 1) {
  16.         if (ord $c == 0x1B) {
  17.             print O "\nE";
  18.         } elsif (ord $c < 32) {
  19.             printf O '.', ord($c);
  20.         } else {
  21.             print O $c;
  22.         }
  23.     }
  24.  
  25.     close I;
  26.     close O
  27.         or die "Closing O failed";
  28.  
  29.     open O, '>', "$fname.u"
  30.         or die;
  31.     open I, "$fname.uncontrol"
  32.         or die;
  33.  
  34.     # Don't print two newlines in a row
  35.     my $lastWasNewline;
  36.     while (<I>) {
  37.         # Remove the most common escape sequences for readability
  38.         # See e.g. these for more info:
  39.         # http://digitalpbk.blogspot.com/2007/05/ansi-escape-sequences.html
  40.         # http://en.wikipedia.org/wiki/ANSI_escape_code
  41.         s/^E\[\d+;\d+H//;
  42.         s/^E\[0(;\d+)?m//;
  43.         # Remove trailing whitespace
  44.         s/\s+$/\n/;
  45.         if (/^$/) {
  46.             unless ($lastWasNewline) {
  47.                 print O "\n";
  48.                 $lastWasNewline = 1;
  49.             }
  50.         } else {
  51.             print O $_;
  52.             $lastWasNewline = undef;
  53.         }
  54.     };
  55.  
  56.     close I;
  57.     close O
  58.         or die "Closing O failed";
  59.  
  60.     unlink "$fname.uncontrol";
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement