our
$AUTHORITY
=
'cpan:ARODLAND'
;
our
$VERSION
=
'0.02'
;
my
%irccolors
= (
black
=> 1,
red
=> 5,
green
=> 3,
yellow
=> 7,
blue
=> 2,
magenta
=> 6,
cyan
=> 10,
white
=> 14,
bright_black
=> 15,
bright_red
=> 4,
bright_green
=> 9,
bright_yellow
=> 8,
bright_blue
=> 12,
bright_magenta
=> 13,
bright_cyan
=> 11,
bright_white
=> 0,
);
sub
convert {
my
(
$text
) =
@_
;
my
$ret
=
""
;
my
$ansi
= Parse::ANSIColor::Tiny->new;
my
$data
=
$ansi
->parse(
$text
);
my
(
%foregrounds
,
%backgrounds
);
$foregrounds
{
$_
} = 1
for
$ansi
->foreground_colors;
$backgrounds
{
$_
} = 1
for
$ansi
->background_colors;
my
(
$foreground
,
$background
,
$underline
,
$bold
) = (
undef
,
undef
, 0, 0);
for
my
$chunk
(
@$data
) {
my
(
$attrs
,
$text
) =
@$chunk
;
my
(
$fg
) =
grep
$foregrounds
{
$_
},
@$attrs
;
my
(
$bg
) =
grep
$backgrounds
{
$_
},
@$attrs
;
my
$bb
= (
grep
$_
eq
'bold'
,
@$attrs
) ? 1 : 0;
my
$u
= (
grep
$_
eq
'underline'
,
@$attrs
) ? 1 : 0;
my
$set_color
;
if
(
$fg
) {
$foreground
= (
$b
?
'bright_'
:
''
) .
$fg
;
$set_color
=
"\cC$irccolors{$foreground}"
;
$bb
= 0;
}
if
(
$bg
) {
$background
=
$bg
;
$set_color
=
"\cC"
.
$irccolors
{
$foreground
||
"black"
} .
",$irccolors{$background}"
;
}
if
(!
$fg
&& !
$bg
&& (
$foreground
||
$background
)) {
undef
$foreground
;
undef
$background
;
if
(
$text
=~ /^\d/) {
$set_color
=
"\cO"
;
undef
$underline
;
undef
$bold
;
}
else
{
$set_color
=
"\cC"
;
}
}
$ret
.=
$set_color
if
length
$set_color
;
if
(
$bb
^
$bold
) {
$bold
=
$bb
;
$ret
.=
"\cB"
;
}
if
(
$u
^
$underline
) {
$underline
=
$u
;
$ret
.=
"\c_"
;
}
if
(
$ret
=~ /\D\d$/ &&
$text
=~ /^\d/) {
substr
(
$ret
, -1, 0,
'0'
);
}
$ret
.=
$text
;
}
return
$ret
;
}
1;