| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::optex::xform; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = "1.02"; | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =encoding utf-8 | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 NAME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | xform - data transform filter module for optex | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | optex -Mxform | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | B is a filter module for B command which transform STDIN | 
| 18 |  |  |  |  |  |  | into different form to make it convenient to manipulate, and recover | 
| 19 |  |  |  |  |  |  | to the original form after the process. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Transformed data have to be appear in exactly same order as original | 
| 22 |  |  |  |  |  |  | data. | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 OPTION | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =over 7 | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =item B<--xform-ansi> | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Transform ANSI terminal sequence into printable string, and recover. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =item B<--xform-utf8> | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | Transform multibyte Non-ASCII chracters into single-byte sequene, and | 
| 35 |  |  |  |  |  |  | recover. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =back | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head1 EXAMPLE | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | $ jot 100 | egrep --color=always .+ | optex -Mxform --xform-ansi column -x | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | L, L, | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | L, L, | 
| 48 |  |  |  |  |  |  | L | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | L | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head1 AUTHOR | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Kazumasa Utashiro | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =head1 LICENSE | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | Copyright 2020 Kazumasa Utashiro. | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 61 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =cut | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 1 |  |  | 1 |  | 726 | use v5.14; | 
|  | 1 |  |  |  |  | 4 |  | 
| 66 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 67 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 68 | 1 |  |  | 1 |  | 611 | use utf8; | 
|  | 1 |  |  |  |  | 14 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 69 | 1 |  |  | 1 |  | 584 | use open IO => 'utf8', ':std'; | 
|  | 1 |  |  |  |  | 1250 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 70 | 1 |  |  | 1 |  | 685 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 6691 |  | 
|  | 1 |  |  |  |  | 63 |  | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 1 |  |  | 1 |  | 504 | use Text::Conceal; | 
|  | 1 |  |  |  |  | 34014 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 73 | 1 |  |  | 1 |  | 7 | use Text::VisualWidth::PP qw(vwidth); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 74 | 1 |  |  | 1 |  | 478 | use Text::ANSI::Fold::Util qw(ansi_width); | 
|  | 1 |  |  |  |  | 865 |  | 
|  | 1 |  |  |  |  | 123 |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | my @concealer; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | my %param = ( | 
| 79 |  |  |  |  |  |  | ansi => { | 
| 80 |  |  |  |  |  |  | length => \&ansi_width, | 
| 81 |  |  |  |  |  |  | match  => qr/\e\[.*?(?:\e\[0*m)+(?:\e\[0*K)*/, | 
| 82 |  |  |  |  |  |  | visible => 2, | 
| 83 |  |  |  |  |  |  | }, | 
| 84 |  |  |  |  |  |  | utf8 => { | 
| 85 |  |  |  |  |  |  | length => \&vwidth, | 
| 86 |  |  |  |  |  |  | match  => qr/\P{ASCII}+/, | 
| 87 |  |  |  |  |  |  | visible => 2, | 
| 88 |  |  |  |  |  |  | }, | 
| 89 |  |  |  |  |  |  | ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub encode { | 
| 92 | 0 |  |  | 0 | 0 |  | my %arg = @_; | 
| 93 | 0 | 0 |  |  |  |  | my $param = $param{$arg{mode}} or die "$arg{mode}: unkown mode"; | 
| 94 | 0 |  |  |  |  |  | my $conceal = Text::Conceal->new(%$param); | 
| 95 | 0 |  |  |  |  |  | local $_ = do { local $/; <> }; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 96 | 0 | 0 |  |  |  |  | if ($conceal) { | 
| 97 | 0 |  |  |  |  |  | $conceal->encode($_); | 
| 98 | 0 |  |  |  |  |  | push @concealer, $conceal; | 
| 99 |  |  |  |  |  |  | } | 
| 100 | 0 |  |  |  |  |  | return $_; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub decode { | 
| 104 | 0 |  |  | 0 | 0 |  | my %arg = @_; | 
| 105 | 0 |  |  |  |  |  | local $_ = do { local $/; <> }; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 106 | 0 | 0 |  |  |  |  | if (my $conceal = pop @concealer) { | 
| 107 | 0 |  |  |  |  |  | $conceal->decode($_); | 
| 108 |  |  |  |  |  |  | } else { | 
| 109 | 0 |  |  |  |  |  | die "Not encoded.\n"; | 
| 110 |  |  |  |  |  |  | } | 
| 111 | 0 |  |  |  |  |  | print $_; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | 1; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | __DATA__ |