File Coverage

blib/lib/App/optex/xform.pm
Criterion Covered Total %
statement 26 44 59.0
branch 0 6 0.0
condition n/a
subroutine 9 11 81.8
pod 0 2 0.0
total 35 63 55.5


line stmt bran cond sub pod time code
1             package App::optex::xform;
2              
3             our $VERSION = "1.03";
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 column -Mxform --xform-ansi -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-2022 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   721 use v5.14;
  1         3  
66 1     1   5 use warnings;
  1         1  
  1         28  
67 1     1   4 use Carp;
  1         1  
  1         54  
68 1     1   519 use utf8;
  1         13  
  1         5  
69 1     1   457 use open IO => 'utf8', ':std';
  1         1006  
  1         6  
70 1     1   587 use Data::Dumper;
  1         5865  
  1         71  
71              
72 1     1   447 use Text::Conceal;
  1         54193  
  1         43  
73 1     1   7 use Text::VisualWidth::PP qw(vwidth);
  1         2  
  1         101  
74 1     1   407 use Text::ANSI::Fold::Util qw(ansi_width);
  1         1921  
  1         105  
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__