From: Peder Stray Date: Sun, 22 Aug 2021 11:36:53 +0000 (+0200) Subject: Fix some long standing bugs in log2ansi X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=e05dfc5c22ef8267c1299aa6a1f5617dd5efecc5;p=irssi-scripts.irssi.org.git Fix some long standing bugs in log2ansi Also make script behave in testing --- diff --git a/scripts/log2ansi.pl b/scripts/log2ansi.pl index b29f69a..c40b468 100755 --- a/scripts/log2ansi.pl +++ b/scripts/log2ansi.pl @@ -28,7 +28,9 @@ use vars qw{$VERSION %IRSSI}; if (__PACKAGE__ =~ /^Irssi/) { # we are within irssi... die! Irssi::print("%RWarning:%n log2ansi should not run from within irssi"); - die "Suicide to prevent loading\n"; +} +else { + do_convert(); } my $opt_clear = 0; @@ -36,56 +38,6 @@ my $opt_html = 0; my $opt_utf8 = 0; my $opt_help = 0; -GetOptions( - 'c|clear!' => \$opt_clear, - 'h|html!' => \$opt_html, - 'u|utf8!' => \$opt_utf8, - 'help' => sub { $opt_help = 1 }, - 'full-help' => sub { $opt_help = 2 }, - ) or pod2usage(2); - -# show some help if stdin is a tty and no files -$opt_help = 1 if !$opt_help && -t 0 && !@ARGV; - -pod2usage(-verbose => $opt_help, - -exitval => 0, - ) if $opt_help; - -for (@ARGV) { - if (/\.xz$/) { - $_ = "unxz < '$_' |"; - } - elsif (/\.bz2$/) { - $_ = "bunzip2 < '$_' |"; - } - elsif (/\.gz$/) { - $_ = "gunzip < '$_' |"; - } - elsif (/\.lzma$/) { - $_ = "unlzma < '$_' |"; - } -} - -my($n) = 0; -%ansi = map { $_ => $n++ } split //, 'krgybmcw'; - -@bols = qw(bold underline blink reverse fgh bgh); -@nums = qw(fgc bgc); - -@base{@bols} = qw(1 4 5 7 1 5); -@base{@nums} = qw(30 40); - -@mirc = split //, 'WkbgRrmyYGcCBMKw'; -@irssi = split //, 'kbgcrmywKBGCRMYW'; - -@mc = map {$ansi{lc $_}} @mirc; -@mh = map {$_ eq uc $_} @mirc; - -@ic = map {$ansi{lc $_}} @irssi; -@ih = map {$_ eq uc $_} @irssi; - -@cn = qw(black dr dg dy db dm dc lgray dgray lr lg ly lb lm lc white); - sub defc { my($attr) = shift || \%attr; $attr->{fgc} = $attr->{bgc} = -1; @@ -176,171 +128,226 @@ sub emit { setold; } -if ($opt_html) { - print qq{
\n}; -} +sub do_convert { -if ($opt_utf8) { - binmode STDIN, ':bytes'; #encoding(cp1252)'; - binmode STDOUT, ':utf8'; -} + GetOptions( + 'c|clear!' => \$opt_clear, + 'h|html!' => \$opt_html, + 'u|utf8!' => \$opt_utf8, + 'help' => sub { $opt_help = 1 }, + 'full-help' => sub { $opt_help = 2 }, + ) or pod2usage(2); -while (<>) { - if ($opt_utf8) { - my $line; - while (length) { - $line .= decode("utf8", $_, Encode::FB_QUIET); - $line .= substr $_, 0, 1, ""; + # show some help if stdin is a tty and no files + $opt_help = 1 if !$opt_help && -t 0 && !@ARGV; + + pod2usage(-verbose => $opt_help, + -exitval => 0, + ) if $opt_help; + + for (@ARGV) { + if (/\.xz$/) { + $_ = "unxz < '$_' |"; + } + elsif (/\.bz2$/) { + $_ = "bunzip2 < '$_' |"; + } + elsif (/\.gz$/) { + $_ = "gunzip < '$_' |"; + } + elsif (/\.lzma$/) { + $_ = "unlzma < '$_' |"; } - $_ = $line; } - chomp; + my($n) = 0; + %ansi = map { $_ => $n++ } split //, 'krgybmcw'; + + @bols = qw(bold underline blink reverse fgh bgh); + @nums = qw(fgc bgc); + + @base{@bols} = qw(1 4 5 7 1 5); + @base{@nums} = qw(30 40); + + @mirc = split //, 'WkbgRrmyYGcCBMKw'; + @irssi = split //, 'kbgcrmywKBGCRMYW'; + + @mc = map {$ansi{lc $_}} @mirc; + @mh = map {$_ eq uc $_} @mirc; + + @ic = map {$ansi{lc $_}} @irssi; + @ih = map {$_ eq uc $_} @irssi; + + @cn = qw(black dr dg dy db dm dc lgray dgray lr lg ly lb lm lc white); - def; - setold; if ($opt_html) { - printf qq{
}; + print qq{
\n}; } - while (length) { - if (s/^\cB//) { - # toggle bold - $attr{bold} = !$attr{bold}; + if ($opt_utf8) { + binmode STDIN, ':bytes'; #encoding(cp1252)'; + binmode STDOUT, ':encoding((UTF-8)'; + } + + while (<>) { + if ($opt_utf8) { + my $line; + while (length) { + $line .= decode("utf8", $_, Encode::FB_QUIET); + $line .= substr $_, 0, 1, ""; + } + $_ = $line; + } - } elsif (s/^\cC//) { - # mirc colors + chomp; - if (/^[^\d,]/) { - defc; - } else { + def; + setold; - if (s/^(\d\d?)//) { - $attr{fgc} = $mc[$1 % 16]; - $attr{fgh} = $mh[$1 % 16]; - } + if ($opt_html) { + printf qq{
}; + } + + while (length) { + if (s/^\cB//) { + # toggle bold + $attr{bold} = !$attr{bold}; + + } elsif (s/^\cC//) { + # mirc colors + + if (/^[^\d,]/) { + defc; + } else { - if (s/^,//) { if (s/^(\d\d?)//) { - $attr{bgc} = $mc[$1 % 16]; - $attr{bgh} = $mh[$1 % 16]; - } else { - $attr{bgc} = -1; - $attr{bgh} = 0; + $attr{fgc} = $mc[$1 % 16]; + $attr{fgh} = $mh[$1 % 16]; + } + + if (s/^,//) { + if (s/^(\d\d?)//) { + $attr{bgc} = $mc[$1 % 16]; + $attr{bgh} = $mh[$1 % 16]; + } else { + $attr{bgc} = -1; + $attr{bgh} = 0; + } } } - } - } elsif (s/^\cD//) { - # irssi format + } elsif (s/^\cD//) { + # irssi format + + if (s/^a//) { + $attr{blink} = !$attr{blink}; + } elsif (s/^b//) { + $attr{underline} = !$attr{underline}; + } elsif (s/^c//) { + $attr{bold} = !$attr{bold}; + } elsif (s/^d//) { + $attr{reverse} = !$attr{reverse}; + } elsif (s/^e//) { + # indent + } elsif (s/^f([^,]*),//) { + # indent_func + } elsif (s/^g//) { + def; + } elsif (s/^h//) { + # cleol + } elsif (s/^i//) { + # monospace + } else { + s/^(.)(.)//; + my($f,$b) = map { ord($_)-ord('0') } $1, $2; + if ($f<0) { + # $attr{fgc} = -1; $attr{fgh} = 0; + } else { + # c>7 => bold, c -= 8 if c>8 + $attr{fgc} = $ic[$f]; + $attr{fgh} = $ih[$f]; + } + if ($b<0) { + # $attr{bgc} = -1; $attr{bgh} = 0; + } else { + # c>7 => blink, c -= 8 + $attr{bgc} = $ic[$b]; + $attr{bgh} = $ih[$b]; + } + } - if (s/^a//) { + } elsif (s/^\cF//) { + # blink $attr{blink} = !$attr{blink}; - } elsif (s/^b//) { - $attr{underline} = !$attr{underline}; - } elsif (s/^c//) { - $attr{bold} = !$attr{bold}; - } elsif (s/^d//) { - $attr{reverse} = !$attr{reverse}; - } elsif (s/^e//) { - # indent - } elsif (s/^f([^,]*),//) { - # indent_func - } elsif (s/^g//) { + + } elsif (s/^\cO//) { def; - } elsif (s/^h//) { - # cleol - } elsif (s/^i//) { - # monospace - } else { - s/^(.)(.)//; - my($f,$b) = map { ord($_)-ord('0') } $1, $2; - if ($f<0) { -# $attr{fgc} = -1; $attr{fgh} = 0; - } else { - # c>7 => bold, c -= 8 if c>8 - $attr{fgc} = $ic[$f]; - $attr{fgh} = $ih[$f]; + + } elsif (s/^\cV//) { + $attr{reverse} = !$attr{reverse}; + + } elsif (s/^\c[\[([^m]*)m//) { + my(@ansi) = split ";", $1; + my(%a); + + push @ansi, 0 unless @ansi; + + for my $code (@ansi) { + if ($code == 0) { + def(\%a); + } elsif ($code == $base{bold}) { + $a{bold} = 1; + } elsif ($code == $base{underline}) { + $a{underline} = 1; + } elsif ($code == $base{blink}) { + $a{underline} = 1; + } elsif ($code == $base{reverse}) { + $a{reverse} = 1; + } elsif ($code >= 30 && $code <= 37) { + $a{fgc} = $code - 30; + } elsif ($code >= 40 && $code <= 47) { + $a{bgc} = $code - 40; + } else { + $a{$code} = 1; + } } - if ($b<0) { -# $attr{bgc} = -1; $attr{bgh} = 0; - } else { - # c>7 => blink, c -= 8 - $attr{bgc} = $ic[$b]; - $attr{bgh} = $ih[$b]; + + if ($a{fgc} >= 0 && $a{bold}) { + $a{fgh} = 1; + $a{bold} = 0; } - } - } elsif (s/^\cF//) { - # blink - $attr{blink} = !$attr{blink}; - - } elsif (s/^\cO//) { - def; - - } elsif (s/^\cV//) { - $attr{reverse} = !$attr{reverse}; - - } elsif (s/^\c[\[([^m]*)m//) { - my(@ansi) = split ";", $1; - my(%a); - - push @ansi, 0 unless @ansi; - - for my $code (@ansi) { - if ($code == 0) { - def(\%a); - } elsif ($code == $base{bold}) { - $a{bold} = 1; - } elsif ($code == $base{underline}) { - $a{underline} = 1; - } elsif ($code == $base{blink}) { - $a{underline} = 1; - } elsif ($code == $base{reverse}) { - $a{reverse} = 1; - } elsif ($code => 30 && $code <= 37) { - $a{fgc} = $code - 30; - } elsif ($code => 40 && $code <= 47) { - $a{bgc} = $code - 40; - } else { - $a{$code} = 1; + if ($a{bgc} >= 0 && $a{blink}) { + $a{bgh} = 1; + $a{blink} = 0; } - } - if ($a{fgc} >= 0 && $a{bold}) { - $a{fgh} = 1; - $a{bold} = 0; - } + for my $key (keys %a) { + $attr{$key} = $a{$key}; + } - if ($a{bgc} >= 0 && $a{blink}) { - $a{bgh} = 1; - $a{blink} = 0; - } + } elsif (s/^\c_//) { + $attr{underline} = !$attr{underline}; - for my $key (keys %a) { - $attr{$key} = $a{$key}; + } else { + s/^(.[^\cB\cC\cD\cF\cO\cV\c[\c_]*)//; + emit $1; } + } - } elsif (s/^\c_//) { - $attr{underline} = !$attr{underline}; - - } else { - s/^(.[^\cB\cC\cD\cF\cO\cV\c[\c_]*)//; - emit $1; + def; + emit ""; + if ($opt_html) { + print "
"; } + print "\n"; } - def; - emit ""; if ($opt_html) { - print "
"; + print "
\n"; } - print "\n"; -} -if ($opt_html) { - print "
\n"; } __END__