blib/lib/App/tldr.pm

filter: Statementstmt 29.3% 42 / 143 Branchbran 0.0% 0 / 56 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.0% 13 / 25 Podpod 0.0% 0 / 3 totaltotal 22.4% 56 / 250 CC 39 SCAR 42.7
CC
39
Cov
20%
_render
210.0
parse_options
110.0
_guess_pager
30.0
CRAP 71.7
↑ Summary
1 package App::tldr 0.21;
2 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Pod::Usage ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Text::Fold ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use constant DEBUG => !!$ENV{TLDR_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant REPOSITORY => $ENV{TLDR_REPOSITORY} // '';
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"TLDR_REPOSITORY"} // ""
l!l
01
MC/DC0%0/1
$ENV{"TLDR_REPOSITORY"} // ""
$ENV{"TLDR_REPOSITORY"}
Truth table50%1/2
A
A= $ENV{"TLDR_REPOSITORY"}
Aresult
11
00
17  
18 1 my $URL = "https://raw.githubusercontent.com/tldr-pages/tldr/main/pages%s/%s/%s.md";
19  
20 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my ($class, %option) = @_;
22 0 my $http = HTTP::Tiny->new(verify_SSL => 1);
23 0 bless { http => $http, %option }, $class;
24 }
25  
26 0 ▶sub parse_options {
Subroutine parse_options: not called CC=10 SCAR=47.0
Pod undocumented
27 0 my ($self, @argv) = @_;
28 0 $self->{platform} = [];
29  
30 0 ▶ $self->{unicode} = ($ENV{LANG} || "") =~ /UTF-8/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'LANG'} || '') =~ /UTF-8/iu ? : 0 0 0
Condition0%0/2
$ENV{'LANG'} || ''
l!l
00
MC/DC0%0/1
$ENV{'LANG'} || ''
$ENV{'LANG'}
Truth table0%0/2
A
A= $ENV{'LANG'}
Aresult
11
00
31 0 my $parser = Getopt::Long::Parser->new(
32 config => [qw(no_auto_abbrev no_ignore_case)],
33 );
34 $parser->getoptionsfromarray(
35 \@argv,
36 0 ▶ "h|help" => sub { print $self->_help; exit },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 "l|language=s" => \my $language,
38 "o|os=s@" => \($self->{platform}),
39 0 ▶ "v|version" => sub { printf "%s %s\n", ref $self, $self->VERSION; exit },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 "pager=s" => \my $pager,
41 "no-pager" => \my $no_pager,
42 "unicode!" => \$self->{unicode},
43 0 ▶ ) or exit(2);
Branch: 1 branch
BranchTrueFalseTotal
$parser->getoptionsfromarray(\@argv, "h|help", sub { print $self->_help; exit; } , "l|language=s", \my $language, "o|os=s\@", \$self->{"platform"}, "v|version", sub { printf "%s %s\n", ref $self, $self->VERSION; exit; } , "pager=s", \my $pager, "no-pager", \my $no_pager, "unicode!", \$self->{"unicode"}) or exit 2 0 0 0
44 0 $self->{argv} = \@argv;
45 0 ▶ if ($language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language) 0 0 0
46 0 ▶ $language = $language =~ /^\./ ? $language : ".$language";
Branch: 1 branch
BranchTrueFalseTotal
$language =~ /^\./u ? : 0 0 0
47 }
48 0 ▶ $self->{language} = $language || '';
Condition0%0/2
$language || ''
l!l
00
MC/DC0%0/1
$language || ''
$language
Truth table0%0/2
A
A= $language
Aresult
11
00
49 0 ▶ if (!$no_pager and -t STDOUT and my $guess = $self->_guess_pager($pager)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_pager and -t STDOUT and my $guess = $self->_guess_pager($pager)) 0 0 0
Condition0%0/6
not $no_pager and -t STDOUT and my $guess = $self->_guess_pager($pager)
!ll&&!rl&&r
000
not $no_pager and -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/3
not $no_pager and -t STDOUT and my $guess = $self->_guess_pager($pager)
not $no_pager -t STDOUT my $guess = $self->_guess_pager($pager)
Truth table0%0/4
not(A) and B and C
A= not $no_pagerB= -t STDOUTC= my $guess = $self->_guess_pager($pager)
ABCresult
0XX0
10X0
1100
1111
50 0 $self->{pager} = $guess;
51 }
52 0 push @{$self->{platform}}, $self->_guess_platform, "common";
53 0 $self;
54 }
55  
56 0 ▶sub _guess_pager {
Subroutine _guess_pager: not called CC=5 SCAR=34.0
57 0 my $self = shift;
58  
59 0 my $cmd;
60 0 for my $try (grep $_, @_, $ENV{PAGER}, "less", "more") {
61 0 ▶ if (my $found = File::Which::which($try)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $found = File::Which::which($try)) 0 0 0
62 0 $cmd = $found, last;
63 }
64 }
65 0 ▶ return if !$cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 0 0
66 0 ▶ [$cmd, $cmd =~ /\bless$/ ? "-R" : ()];
Branch: 1 branch
BranchTrueFalseTotal
$cmd =~ /\bless$/u ? : 0 0 0
67 }
68  
69 0 ▶sub _help {
Subroutine _help: not called CC=1 SCAR=6.9
70 0 my ($self, $exit) = @_;
71 0 open my $fh, '>', \my $out;
72 0 Pod::Usage::pod2usage
73 exitval => 'noexit',
74 input => $0,
75 output => $fh,
76 sections => 'SYNOPSIS',
77 verbose => 99,
78 ;
79 0 $out =~ s/^Usage:\n//;
80 0 $out =~ s/^[ ]{6}//mg;
81 0 $out =~ s/\n$//;
82 0 $out;
83 }
84  
85  
86 # XXX
87 0 ▶sub _guess_platform {
Subroutine _guess_platform: not called CC=4 SCAR=30.0
88 0 ▶ $^O =~ /darwin/i ? "osx" :
Branch: 3 branches
BranchTrueFalseTotal
$^O =~ /darwin/iu ? : 0 0 0
$^O =~ /linux/iu ? : 0 0 0
$^O =~ /sunos/iu ? : 0 0 0
89 $^O =~ /linux/i ? "linux" :
90 $^O =~ /sunos/i ? "sunos" : ();
91 }
92  
93 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
94 0 my $self = shift;
95 0 if (REPOSITORY) {
96 $self->_local_get(@_);
97 } else {
98 0 $self->_http_get(@_);
99 }
100 }
101  
102 0 ▶sub _http_get {
Subroutine _http_get: not called CC=3 SCAR=24.8
103 0 my ($self, $query, $platform) = @_;
104 0 my $url = sprintf $URL, $self->{language}, $platform, $query;
105 0 my $res = $self->{http}->get($url);
106 0 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 0 0 0
107 0 (Encode::decode_utf8($res->{content}), undef);
108 } else {
109 0 my $err = "$res->{status} $res->{reason}";
110 0 ▶ if ($res->{status} == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} == 599) 0 0 0
111 0 $err .= ", $res->{content}";
112 }
113 0 (undef, "$url: $err");
114 }
115 }
116  
117 0 ▶sub _local_get {
Subroutine _local_get: not called CC=3 SCAR=24.8
118 0 my ($self, $query, $platform) = @_;
119 0 my $file = File::Spec->catfile(REPOSITORY, "pages$self->{language}", $platform, "$query.md");
120 0 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
121 0 ▶ open my $fh, "<:utf8", $file or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $file or die "${file}: $!" 0 0 0
122 0 local $/;
123 0 (<$fh>, undef);
124 } else {
125 0 (undef, "Missing $file");
126 }
127 }
128  
129 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
130 0 my $self = shift;
131 0 ▶ my $arg = shift @{$self->{argv}} or die $self->_help(1);
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift @{$$self{"argv"};} or die $self->_help(1) 0 0 0
132 0 my $content;
133 0 for my $platform (@{ $self->{platform} }) {
134 0 ($content, my $err) = $self->_get($arg, $platform);
135 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) { } 0 0 0
136 0 last;
137 } elsif (DEBUG) {
138 warn "-> $err\n";
139 }
140 }
141 0 ▶ die "Couldn't find tldr for '$arg'\n" unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
142 0 $self->_render($content, $arg);
143 }
144  
145 1 my $CHECK = "\N{U+2713}";
146 1 my $SUSHI = "\N{U+1F363}";
147  
148 0 ▶sub _render {
Subroutine _render: not called CC=14 SCAR=53.5
149 0 my ($self, $content, $query) = @_;
150  
151 0 ▶ my ($check, $prompt) = $self->{unicode} ? ($CHECK, $SUSHI) : ('*', '$');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'unicode'} ? : 0 0 0
152  
153 0 ▶ my $width = $ENV{COLUMNS} || (Term::ReadKey::GetTerminalSize())[0];
Condition0%0/3
$ENV{'COLUMNS'} || (Term::ReadKey::GetTerminalSize())[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'COLUMNS'} || (Term::ReadKey::GetTerminalSize())[0]
$ENV{'COLUMNS'} (Term::ReadKey::GetTerminalSize())[0]
Truth table0%0/3
A || B
A= $ENV{'COLUMNS'}B= (Term::ReadKey::GetTerminalSize())[0]
ABresult
1X1
011
000
154 0 $width -= 4;
155  
156 0 my @line = split /\n/, $content;
157  
158 0 my $out;
159 0 ▶ if ($self->{pager}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pager'}) { } 0 0 0
160 0 ▶ open $out, "|-", @{$self->{pager}} or die "failed to exec @{$self->{pager}}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $out, "|-", @{$self->{"pager"};} or die qq[failed to exec @{$self->{"pager"};}: $!] 0 0 0
161 } else {
162 0 $out = \*STDOUT;
163 }
164 0 binmode $out, ":utf8";
165  
166 0 ▶ while (defined(my $line = shift @line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = shift @line)) 0 0 0
167 0 ▶ if ($line =~ /^#/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^#/u) { } 0 0 0
elsif ($line =~ s/^\>\s*//u) { } 0 0 0
elsif ($line =~ s/^[*-]\s*//u) { } 0 0 0
elsif ($line =~ /`([^`]+)`/u) { } 0 0 0
168 # skip
169 } elsif ($line =~ s/^\>\s*//) {
170 0 my $description = $line;
171 0 while (1) {
172 0 my $next = shift @line;
173 0 ▶ if ($next eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($next eq '') { } 0 0 0
elsif ($next =~ s/^\>\s*//u) { } 0 0 0
174 0 next;
175 } elsif ($next =~ s/^\>\s*//) {
176 0 $description .= "\n$next";
177 } else {
178 0 unshift @line, $next;
179 0 last;
180 }
181 }
182 0 my $fold = Text::Fold::fold_text($description, $width);
183 0 $out->print("\n");
184 0 $out->print(" \e[32m$_\e[m\n") for split /\n/, $fold;
185 0 $out->print("\n");
186 } elsif ($line =~ s/^[*-]\s*//) {
187 0 my $fold = Text::Fold::fold_text($line, $width - 2);
188 0 my ($first, @rest) = split /\n/, $fold;
189 0 $out->print(" \e[1m$check \e[4m$first\e[m\n");
190 0 $out->print(" \e[1m\e[4m$_\e[m\n") for @rest;
191 0 $out->print("\n");
192 } elsif ($line =~ /`([^`]+)`/) {
193 0 my $code = $1;
194 0 $code =~ s/\b$query\b/
195 0 "\e[32m$query\e[m"
196 /eg;
197 0 $out->print(" $prompt $code\n\n");
198 }
199 }
200 }
201  
202 1 1;
203 __END__
↑ Summary