File: help2ansi

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (60 lines) | stat: -rwxr-xr-x 1,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
use warnings;
use integer;

#Converts text with KBtin-style color codes (~n~) to ANSI.

$lastcol=$col="\e[0m";
my ($fg,$bg,$at) = (7,0,0);
%ct=(0=>0, 1=>4, 2=>2, 3=>6, 4=>1, 5=>5, 6=>3, 7=>7);
sub get256c($)
{
    my $c = $_[0];
    return 16+36*($c/100)+6*($c/10 % 10)+($c % 10);
}

while(<>)
{
    $line="";
    while(/\G(.*?)~(\-1|\d+|\d*:\d*(?::\d*)?)~/gc)
    {
        $line.=$1;
        next if $2 eq "-1" && ($line.=$col=$lastcol);

        $2 =~ /^(\d*)(?::(\d*)(?::(\d*))?)?$/;
        $fg=($1 eq "")?($cn&0xf):$1;
        $bg=(!defined $2)?0:($2 eq "")?(($cn&0x70)>>4):$2;
        $at=(!defined $3)?0:($3 eq "")?($cn>>7):$3;
        if ($1 && $1>16)
        {
            $cn=0;
            $col.="\e[0;38;5;".get256c($1);
            $line.=$col.="m";
            next; # no 256c backgrounds in the help...
        }

        die "Invalid color code in [$_]" if $fg>15&&($bg||$at) || $bg>7;
        $vcn=$cn=$fg+$bg*0x10+$at*0x80;
        $vcn|=7 if ($vcn&0xf)==($vcn&0x70)>>4;
        $vcn&=~7 if ($vcn&0xf)==($vcn&0x70)>>4;
        $col="\e[0";
        if(($vcn&0xf)==8)
        {
            $col.=";2";
        }
        else
        {
            $col.=";3".$ct{$vcn&0x7} if ($vcn&0x7)!=7;
            $col.=";1" if ($vcn&0x8);
        }
        $col.=";4".$ct{($vcn>>4)&0x7} if $vcn&0x70;
        $col.=";5" if $vcn&0x80;
        $col.=";3" if $vcn&0x100;
        $col.=";4" if $vcn&0x200;
        $col.=";9" if $vcn&0x400;
        $line.=$col.="m";
    }
    /\G(.*)/;
    print "$line$1\n";
    $lastcol=$col;
}