| 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
| |||||||||||||||||||||||||||||
| 44 | # --help and --version do not exit, as a function must not | ||||||||||||||||||||||||||||
| 45 | 29 | ▶ | if ($app->help) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | 1 | return 0; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 28 | ▶ | if ($app->version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 2 | say "ansipic $VERSION"; | |||||||||||||||||||||||||||
| 54 | 2 | return 0; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 26 | ▶ | @ARGV or return usage(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 5 | ▶ | warn "ansipic: $file: " . (-e $file ? 'not shown' : 'not found') . "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
$app->$name // (next)
$app->$name
Truth table100%2/2
A
A= $app->$name
| |||||||||||||||||||||||||||||
| 91 | 10 | $val =~ s/^=//; # -w=2/, as ansifold takes it | |||||||||||||||||||||||||||
| 92 | 10 | ▶ | if ($val !~ /\D/) { $app->$name = $val; next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 6 | ▶ | defined $init or die "--$name $val: the size of the terminal is not known\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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")
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)
| |||||||||||||||||||||||||||||
| 95 | 5 | $app->$name = int $n; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 23 | my @types; | |||||||||||||||||||||||||||
| 98 | 23 | ▶ | if (defined $app->types) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 5 | @types = split /,/, $app->types; | |||||||||||||||||||||||||||
| 100 | 5 | for my $t (@types) { | |||||||||||||||||||||||||||
| 101 | 6 | ▶ | grep { $_ eq $t } @Text::ANSI::Image::TYPES | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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")
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"))
| |||||||||||||||||||||||||||||
| 110 | ) // die "no method for this terminal: give one with --method\n"; | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 23 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||