Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- @ARGV
- or die "Supply an ANSI Escaped file name parameter";
- foreach my $fname (@ARGV) {
- open O, '>', "$fname.uncontrol"
- or die;
- open I, $fname or die;
- my $c;
- while (read I, $c, 1) {
- if (ord $c == 0x1B) {
- print O "\nE";
- } elsif (ord $c < 32) {
- printf O '.', ord($c);
- } else {
- print O $c;
- }
- }
- close I;
- close O
- or die "Closing O failed";
- open O, '>', "$fname.u"
- or die;
- open I, "$fname.uncontrol"
- or die;
- # Don't print two newlines in a row
- my $lastWasNewline;
- while (<I>) {
- # Remove the most common escape sequences for readability
- # See e.g. these for more info:
- # http://digitalpbk.blogspot.com/2007/05/ansi-escape-sequences.html
- # http://en.wikipedia.org/wiki/ANSI_escape_code
- s/^E\[\d+;\d+H//;
- s/^E\[0(;\d+)?m//;
- # Remove trailing whitespace
- s/\s+$/\n/;
- if (/^$/) {
- unless ($lastWasNewline) {
- print O "\n";
- $lastWasNewline = 1;
- }
- } else {
- print O $_;
- $lastWasNewline = undef;
- }
- };
- close I;
- close O
- or die "Closing O failed";
- unlink "$fname.uncontrol";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement