# Made by Juerd use strict; sub colorcode ($) { (my $str = $_[0]) =~ s/\e\[.*?[a-zA-Z]//g; # Immediately strip ANSI colors if ($str =~ /(
)/) { $str =~ s//\cO$1/g; } else { $str .= "\cO"; } $str =~ s/\cV(.*?)\cV/\cC0,1$1\cO/g; $str =~ s/\cV/\cC0,1/; my @chars = split //, $str; my $ret = ''; my ($CCfc, $CCbc, $CCnc); my $CCb = 0; my $CCu = 0; while (@chars) { my $char = shift @chars; if ($char eq "\cB") { $ret .= ($CCb ? '' : ''); $CCb = !$CCb; } elsif ($char eq "\c_") { $ret .= ($CCu ? '' : ''); $CCu = !$CCu; } elsif ($char eq "\cO") { if ($CCb) { $CCb = 0; $ret .= ''; } if ($CCu) { $CCu = 0; $ret .= ''; } if ($CCnc){ $CCbc= 0; $ret .= '' x $CCnc; $CCfc= 0; $CCnc = 0; } } elsif ($char eq "\cC") { my $CCcs = ''; while (($char = shift @chars) =~ /[\d,]/) { $CCcs .= $char; } if ($char =~ /[^\d,]/) { unshift @chars, $char; } if ($CCcs eq '') { if ($CCbc) { $ret .= ''; $CCnc--; } if ($CCfc) { $ret .= ''; $CCnc--; } $CCbc = undef; $CCfc = undef; } else { my @k = split /,/, $CCcs; if (defined($k[0]) && $k[0] ne '') { $CCfc = $k[0]; $ret .= ""; $CCnc++; } if (defined($k[1]) && $k[1] ne '') { $CCbc = $k[1]; $ret .= ""; $CCnc++; } } } else { $ret .= $char; } } return $ret; } sub colorstyles () { # Call this sub before you call colorcode() print qq{'; } 1;