blib/lib/App/ansipic.pm

filter: Statementstmt 100% 93 / 93 Branchbran 92.8% 26 / 28 Conditioncond 100% 9 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 4 totaltotal 94.7% 145 / 153 CC 23 SCAR 21.3
CC
23
Cov
98%
image
13.0
perform
11.0
BEGIN
1.0
CRAP 8.4
« blib/lib/Text/ANSI/Image.pm ↑ Summary blib/lib/Text/ANSI/Image/kitty.pm »
1 package App::ansipic;
2  
3 23 our $VERSION = "0.01";
4  
5 23 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use open IO => 'utf8', ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Getopt::EX::Long qw(:DEFAULT Configure ExConfigure);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Getopt::EX::RPN qw(rpn_calc);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Text::ANSI::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 23 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 our @EXPORT_OK = qw(ansipic);
15  
16 # The command as a function: it takes the arguments, and returns the
17 # status, 0, 1 when a file is not shown, or 2 for an error. It does
18 # not exit, so that Command::Run can call it without a fork.
19 30 ▶sub ansipic {
Subroutine ansipic: called CC=1 SCAR=0.0
Pod undocumented
20 # in a variable, the object is gone when it returns: Getopt::EX::Hashed
21 # dies for a new object while another is there
22 30 my $app = __PACKAGE__->new;
23 30 $app->perform(@_);
24 }
25  
26 23 ▶use Getopt::EX::Hashed 'has'; {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 23 Getopt::EX::Hashed->configure(DEFAULT => [ is => 'rw' ]);
28 23 has method => ' m =s ' , default => 'auto';
29 23 has width => ' w =s ' ;
30 23 has height => ' H =s ' ;
31 23 has types => ' t =s ' ;
32 23 has term => ' =s ' ;
33 23 has tty => ' =s ' ;
34 23 has help => ' h ' ;
35 23 has version => ' v ' ;
36 23 ▶} no Getopt::EX::Hashed;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 30 ▶sub perform {
Subroutine perform: called CC=11 SCAR=24.0
Pod undocumented
39 30 my $app = shift;
40 30 local @ARGV = @_;
41 30 ExConfigure BASECLASS => [ __PACKAGE__, 'Getopt::EX' ];
42 30 Configure qw(bundling no_ignore_case);
43 30 ▶ $app->getopt or return usage();
Branch: 1 branch
BranchTrueFalseTotal
$app->getopt or return usage() 29 1 30
44 # --help and --version do not exit, as a function must not
45 29 ▶ if ($app->help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->help) 1 28 29
46 # the manual is in the script; called as a function, $0 may not be
47 # a file, and the module has a manual too
48 pod2usage(-exitval => 'NOEXIT', -verbose => 99, -sections => [ qw(SYNOPSIS VERSION) ],
49 1 ▶ -input => -f $0 ? $0 : $INC{'App/ansipic.pm'});
Branch: 1 branch
BranchTrueFalseTotal
-f $0 ? : 1 0 1
50 1 return 0;
51 }
52 28 ▶ if ($app->version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->version) 2 26 28
53 2 say "ansipic $VERSION";
54 2 return 0;
55 }
56 26 ▶ @ARGV or return usage();
Branch: 1 branch
BranchTrueFalseTotal
@ARGV or return usage() 24 2 26
57 24 ▶ my $img = eval { $app->image } // do {
Condition100%3/3
eval { do { $app->image } } // do { warn "ansipic: ", $@ =~ s/ at .* line \d+.*//rsu, "\n"; return 2 }
l!l&&r!l&&!r
1824
MC/DC0%0/2
eval { do { $app->image } } // do { warn "ansipic: ", $@ =~ s/ at .* line \d+.*//rsu, "\n"; return 2 }
eval { do { $app->image } } do { warn "ansipic: ", $@ =~ s/ at .* line \d+.*//rsu, "\n"; return 2 }
Truth table66%2/3
A // B
A= eval { do { $app->image } }B= do { warn "ansipic: ", $@ =~ s/ at .* line \d+.*//rsu, "\n"; return 2 }
ABresult
1X1
011
000
58 6 warn "ansipic: ", $@ =~ s/ at .* line \d+.*//sr, "\n";
59 6 return 2;
60 };
61 18 my $status = 0;
62 18 for my $file (@ARGV) {
63 21 my @rows = $img->rows($file);
64 21 ▶ unless (@rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rows) 5 16 21
65 5 ▶ warn "ansipic: $file: " . (-e $file ? 'not shown' : 'not found') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 2 3 5
66 5 $status = 1;
67 5 next;
68 }
69 # the image goes to the terminal before the cells which show it
70 16 $img->flush;
71 16 print "$_\n" for @rows;
72 }
73 18 $status;
74 }
75  
76 3 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
77 3 warn "Usage: ansipic [options] file ...\n";
78 3 2;
79 }
80  
81 # The object of Text::ANSI::Image for the options. It dies for an
82 # error.
83 24 ▶sub image {
Subroutine image: called CC=13 SCAR=25.7
Pod undocumented
84 24 my $app = shift;
85 # --width and --height: a number, or an RPN expression which starts
86 # with the width or the height of the terminal, as 2/ for the half
87 24 my($tcols, $trows) = Text::ANSI::Image->terminal($app->term);
88 24 for ([ width => $tcols ], [ height => $trows ]) {
89 47 my($name, $init) = @$_;
90 47 ▶ my $val = $app->$name // next;
Condition100%2/2
$app->$name // (next)
l!l
1037
MC/DC100%1/1
$app->$name // (next)
$app->$name
Truth table100%2/2
A
A= $app->$name
Aresult
11
00
91 10 $val =~ s/^=//; # -w=2/, as ansifold takes it
92 10 ▶ if ($val !~ /\D/) { $app->$name = $val; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /\D/u) 4 6 10
93 6 ▶ defined $init or die "--$name $val: the size of the terminal is not known\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $init or die "--$name ${val}: the size of the terminal is not known\n" 6 0 6
94 6 ▶ my $n = rpn_calc($init, $val) // die "--$name $val: invalid expression\n";
Condition100%2/2
rpn_calc($init, $val) // die("--$name ${val}: invalid expression\n")
l!l
51
MC/DC100%1/1
rpn_calc($init, $val) // die("--$name ${val}: invalid expression\n")
rpn_calc($init, $val)
Truth table100%2/2
A
A= rpn_calc($init, $val)
Aresult
11
00
95 5 $app->$name = int $n;
96 }
97 23 my @types;
98 23 ▶ if (defined $app->types) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $app->types) 5 18 23
99 5 @types = split /,/, $app->types;
100 5 for my $t (@types) {
101 6 ▶ grep { $_ eq $t } @Text::ANSI::Image::TYPES
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $t;} @Text::ANSI::Image::TYPES or die qq[unknown type: $t (@{[join(", ", @Text::ANSI::Image::TYPES)];})\n] 3 3 6
102 3 or die "unknown type: $t (@{[ join ', ', @Text::ANSI::Image::TYPES ]})\n";
103 }
104 }
105 Text::ANSI::Image->new(
106 method => $app->method,
107 @types ? (types => \@types) : (),
108 defined $app->height ? (rows => $app->height) : (),
109 20 ▶ map { defined $app->$_ ? ($_ => $app->$_) : () } qw(width term tty),
Branch: 3 branches
BranchTrueFalseTotal
@types ? : 2 18 20
defined $app->height ? : 2 18 20
defined $app->$_ ? : 44 16 60
Condition100%2/2
"Text::ANSI::Image"->new("method", $app->method, @types ? ("types", \@types) : (), defined $app->height ? ("rows", $app->height) : (), map({defined $app->$_ ? ($_, $app->$_) : ();} "width", "term", "tty")) // die("no method for this terminal: give one with --method\n")
l!l
181
MC/DC100%1/1
"Text::ANSI::Image"->new("method", $app->method, @types ? ("types", \@types) : (), defined $app->height ? ("rows", $app->height) : (), map({defined $app->$_ ? ($_, $app->$_) : ();} "width", "term", "tty")) // die("no method for this terminal: give one with --method\n")
"Text::ANSI::Image"->new("method", $app->method, @types ? ("types", \@types) : (), defined $app->height ? ("rows", $app->height) : (), map({defined $app->$_ ? ($_, $app->$_) : ();} "width", "term", "tty"))
Truth table100%2/2
A
A= "Text::ANSI::Image"->new("method", $app->method, @types ? ("types", \@types) : (), defined $app->height ? ("rows", $app->height) : (), map({defined $app->$_ ? ($_, $app->$_) : ();} "width", "term", "tty"))
Aresult
11
00
110 ) // die "no method for this terminal: give one with --method\n";
111 }
112  
113 23 1;
114  
115 __END__
« blib/lib/Text/ANSI/Image.pm ↑ Summary blib/lib/Text/ANSI/Image/kitty.pm »