File Coverage

blib/lib/App/ansiexpand.pm
Criterion Covered Total %
statement 45 52 86.5
branch 7 8 87.5
condition n/a
subroutine 12 13 92.3
pod 0 2 0.0
total 64 75 85.3


line stmt bran cond sub pod time code
1             package App::ansiexpand;
2             our $VERSION = "1.06";
3              
4 25     25   4309678 use 5.014;
  25         122  
5 25     25   159 use warnings;
  25         36  
  25         1905  
6              
7 25     25   881 use open IO => 'utf8', ':std';
  25         1155  
  25         206  
8 25     25   19617 use Encode;
  25         545167  
  25         2847  
9 25     25   15852 use Pod::Usage;
  25         1598257  
  25         3816  
10 25     25   1009 use Data::Dumper;
  25         10234  
  25         1916  
11 25     25   198 use List::Util qw(max);
  25         51  
  25         1790  
12 25     25   15432 use Text::ANSI::Tabs qw(ansi_expand ansi_unexpand);
  25         1819690  
  25         2990  
13              
14             our $DEFAULT_UNEXPAND;
15              
16 25     25   16789 use Getopt::EX::Hashed 1.05; {
  25         246453  
  25         188  
17              
18             Getopt::EX::Hashed->configure(DEFAULT => [ is => 'rw' ]);
19              
20             has unexpand => ' u ! ' , default => $DEFAULT_UNEXPAND;
21             has all => ' a ' , default => 1;
22             has zap => ' z ' ;
23             has minimum => ' x :1 ' ;
24             has ambiguous => ' =s ' , any => [ qw(wide narrow) ];
25             has tabstop => ' t =i ' , min => 1;
26             has tabhead => ' =s ' ;
27             has tabspace => ' =s ' ;
28             has tabstyle => ' ts :s ' ;
29             has help => ' h ' ;
30             has version => ' v ' ;
31              
32             has [ qw(+minimum +tabstop +tabhead +tabspace +tabstyle) ] => sub {
33             my($name, $val) = ("$_[0]", $_[1]);
34             if ($name eq 'tabstyle' and $val eq '') {
35             list_tabstyle();
36             exit;
37             }
38             $_->$name = $val;
39             Text::ANSI::Tabs->configure($name => $val);
40             };
41              
42             has '+help' => sub {
43             pod2usage
44             -verbose => 99,
45             -sections => [ qw(SYNOPSIS VERSION) ];
46             };
47              
48             has '+version' => sub {
49             print "Version: $VERSION\n";
50             exit;
51             };
52              
53             has ARGV => default => [];
54             has '<>' => sub {
55             if ($_[0] =~ /^-([0-9]+)$/x) {
56             $_->tabstop = $1 or die "$_[0]: invalid tabstop\n";
57             Text::ANSI::Tabs->configure(tabstop => $1);
58             } else {
59             if ($_[0] =~ /^-{1,2}+(.+)/) {
60             warn "Unknown option: $1\n";
61             pod2usage();
62             }
63             push @{$_->ARGV}, $_[0];
64             }
65             };
66              
67 25     25   18924 } no Getopt::EX::Hashed;
  25         64  
  25         148  
68              
69             sub run {
70 21     21 0 34459143 my $app = shift;
71 21 50       619 local @ARGV = map { utf8::is_utf8($_) ? $_ : decode('utf8', $_) } @_;
  36         3066  
72              
73 25     25   17756 use Getopt::EX::Long qw(:DEFAULT ExConfigure Configure);
  25         2831673  
  25         15900  
74 21         2940 ExConfigure BASECLASS => [ __PACKAGE__, 'Getopt::EX' ];
75 21         6420 Configure qw(bundling pass_through);
76 21 100       4079 $app->getopt || pod2usage();
77 15         32719 @ARGV = @{$app->ARGV};
  15         275  
78              
79 15 100       179 my $action = $app->unexpand ? \&ansi_unexpand : \&ansi_expand;
80              
81 15 100       533 local $/ = $app->zap ? undef : $/;
82 15         1879 while (<>) {
83 19         51666 print $action->($_);
84             }
85              
86 15         143680 return 0;
87             }
88              
89             sub list_tabstyle {
90 0     0 0   my %style = %Text::ANSI::Fold::TABSTYLE;
91 0           my $max = max map length, keys %style;
92 0           for my $name (sort keys %style) {
93 0           my($head, $space) = @{$style{$name}};
  0            
94 0           my $tab = $head . $space x 7;
95 0           printf "%*s %s\n", $max, $name, $tab x 3;
96             }
97             }
98              
99             1;
100             __END__