blib/lib/Dist/Zilla/Chrome/Term.pm

filter: Statementstmt 35.7% 39 / 109 Branchbran 8.3% 3 / 36 Conditioncond 6.4% 2 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 42.8% 9 / 21 Podpod 0.0% 0 / 3 totaltotal 24.4% 53 / 217 CC 31 SCAR 35.4
CC
31
Cov
25%
prompt_str
90.0
_str_color
72.0
prompt_yn
30.0
CRAP 34.3
« blib/lib/Dist/Zilla/Role/BuildPL.pm ↑ Summary blib/lib/Dist/Zilla/App/Command/new.pm »
1 package Dist::Zilla::Chrome::Term 6.040;
2 # ABSTRACT: chrome used for terminal-based interaction
3  
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Dist::Zilla::Types qw(OneZero);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Log::Dispatchouli 1.102220;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #pod =head1 OVERVIEW
16 #pod
17 #pod This class provides a L<Dist::Zilla::Chrome> implementation for use in a
18 #pod terminal environment. It's the default chrome used by L<Dist::Zilla::App>.
19 #pod
20 #pod =cut
21  
22 0 ▶sub _str_color {
Subroutine _str_color: not called CC=8 SCAR=42.8
23 0 my ($str) = @_;
24  
25 0 state %color_for;
26  
27 # I know, I know, this is ludicrous, but guess what? It's my Sunday and I
28 # can spend it how I want.
29 0 ▶ state $max = ($ENV{COLORTERM}//'') eq 'truecolor' ? 255 : 5;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'COLORTERM'} // '') eq 'truecolor' ? : 0 0 0
Condition0%0/2
$ENV{"COLORTERM"} // ""
l!l
00
MC/DC0%0/1
$ENV{"COLORTERM"} // ""
$ENV{"COLORTERM"}
Truth table0%0/2
A
A= $ENV{"COLORTERM"}
Aresult
11
00
30 0 ▶ state $min = $max == 255 ? 384 : 5;
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
31 0 ▶ state $inc = $max == 255 ? 16 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
32 0 ▶ state $fmt = $max == 255 ? 'r%ug%ub%u' : 'rgb%u%u%u';
Branch: 1 branch
BranchTrueFalseTotal
$max == 255 ? : 0 0 0
33  
34 0 ▶ return $color_for{$str} //= do {
Condition0%0/3
$color_for{$str} //= do { my(@rgb) = map({$_ % $max;} unpack("CCC", md5($str))); my $i = ($rgb[0] + $rgb[1] + $rgb[2]) % 3; while (1) { last if $rgb[0] + $rgb[1] + $rgb[2] >= $min; my $next = $i++ % 3; $rgb[$next] = abs $max - $rgb[$next]; } ; sprintf $fmt, @rgb }
l!l&&r!l&&!r
000
MC/DC0%0/2
$color_for{$str} //= do { my(@rgb) = map({$_ % $max;} unpack("CCC", md5($str))); my $i = ($rgb[0] + $rgb[1] + $rgb[2]) % 3; while (1) { last if $rgb[0] + $rgb[1] + $rgb[2] >= $min; my $next = $i++ % 3; $rgb[$next] = abs $max - $rgb[$next]; } ; sprintf $fmt, @rgb }
$color_for{$str} do { my(@rgb) = map({$_ % $max;} unpack("CCC", md5($str))); my $i = ($rgb[0] + $rgb[1] + $rgb[2]) % 3; while (1) { last if $rgb[0] + $rgb[1] + $rgb[2] >= $min; my $next = $i++ % 3; $rgb[$next] = abs $max - $rgb[$next]; } ; sprintf $fmt, @rgb }
Truth table0%0/3
A //= B
A= $color_for{$str}B= do { my(@rgb) = map({$_ % $max;} unpack("CCC", md5($str))); my $i = ($rgb[0] + $rgb[1] + $rgb[2]) % 3; while (1) { last if $rgb[0] + $rgb[1] + $rgb[2] >= $min; my $next = $i++ % 3; $rgb[$next] = abs $max - $rgb[$next]; } ; sprintf $fmt, @rgb }
ABresult
1X1
011
000
35 0 my @rgb = map { $_ % $max } unpack 'CCC', md5($str);
36  
37 0 my $i = ($rgb[0] + $rgb[1] + $rgb[2]) % 3;
38 0 while (1) {
39 0 ▶ last if $rgb[0] + $rgb[1] + $rgb[2] >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if $rgb[0] + $rgb[1] + $rgb[2] >= $min 0 0 0
40  
41 0 my $next = $i++ % 3;
42  
43 0 $rgb[$next] = abs($max - $rgb[$next]);
44 }
45  
46 0 sprintf $fmt, @rgb;
47 }
48 }
49  
50 4 has logger => (
51 is => 'ro',
52 isa => 'Log::Dispatchouli',
53 init_arg => undef,
54 writer => '_set_logger',
55 lazy => 1,
56 builder => '_build_logger',
57 );
58  
59 20 ▶sub _build_logger {
Subroutine _build_logger: called CC=6 SCAR=23.9
60 20 my $self = shift;
61 20 my $enc = $self->term_enc;
62  
63 20 ▶ if ($enc && Encode::resolve_alias($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc and &Encode::resolve_alias($enc)) 20 0 20
Condition33%1/3
$enc and &Encode::resolve_alias($enc)
!ll&&!rl&&r
0020
MC/DC0%0/2
$enc and &Encode::resolve_alias($enc)
$enc &Encode::resolve_alias($enc)
Truth table33%1/3
A and B
A= $encB= &Encode::resolve_alias($enc)
ABresult
0X0
100
111
64 20 my $layer = sprintf(":encoding(%s)", $enc);
65 20 binmode( STDOUT, $layer );
66 20 binmode( STDERR, $layer );
67 }
68  
69 my $logger = Log::Dispatchouli->new({
70 ident => 'Dist::Zilla',
71 to_stdout => 1,
72 log_pid => 0,
73 20 ▶ to_self => ($ENV{DZIL_TESTING} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DZIL_TESTING'} ? : 20 0 20
74 quiet_fatal => 'stdout',
75 });
76  
77 20 ▶ my $use_color = $ENV{DZIL_COLOR} // -t *STDOUT;
Condition33%1/3
$ENV{"DZIL_COLOR"} // -t *STDOUT
l!l&&r!l&&!r
0200
MC/DC0%0/2
$ENV{"DZIL_COLOR"} // -t *STDOUT
$ENV{"DZIL_COLOR"} -t *STDOUT
Truth table33%1/3
A // B
A= $ENV{"DZIL_COLOR"}B= -t *STDOUT
ABresult
1X1
011
000
78  
79 20 ▶ if ($use_color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_color) 0 20 20
80 0 my $stdout = $logger->{dispatcher}->output('stdout');
81  
82 0 ▶ $stdout->add_callback(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
83 0 require Term::ANSIColor;
84 0 my $message = {@_}->{message};
85 0 ▶ return $message unless $message =~ s/\A\[([^\]]+)] //;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ s/\A\[([^\]]+)] //u 0 0 0
86 0 my $prefix = $1;
87 0 return sprintf "[%s] %s",
88 Term::ANSIColor::colored([ _str_color($prefix) ], $prefix),
89 $message;
90 0 });
91 }
92  
93 20 return $logger;
94 }
95  
96 has term_ui => (
97 is => 'ro',
98 isa => 'Object',
99 lazy => 1,
100 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 require Term::ReadLine;
102 0 require Term::UI;
103 0 Term::ReadLine->new('dzil')
104 },
105 4 );
106  
107 has term_enc => (
108 is => 'ro',
109 lazy => 1,
110 20 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 20 require Term::Encoding;
112 20 return Term::Encoding::get_encoding();
113 },
114 4 );
115  
116 0 ▶sub prompt_str {
Subroutine prompt_str: not called CC=9 SCAR=45.0
Pod undocumented
117 0 my ($self, $prompt, $arg) = @_;
118 0 ▶ $arg ||= {};
Condition0%0/2
$arg ||= {}
l!l
00
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
Aresult
11
00
119 0 my $default = $arg->{default};
120 0 my $check = $arg->{check};
121  
122 0 require Encode;
123 0 my $term_enc = $self->term_enc;
124  
125 my $encode = $term_enc
126 0 ▶ ? sub { Encode::encode($term_enc, shift, Encode::FB_CROAK()) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 ▶ : sub { shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$term_enc ? : 0 0 0
128 my $decode = $term_enc
129 0 ▶ ? sub { Encode::decode($term_enc, shift, Encode::FB_CROAK()) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ : sub { shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$term_enc ? : 0 0 0
131  
132 0 ▶ if ($arg->{noecho}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"noecho"}) 0 0 0
133 0 require Term::ReadKey;
134 0 Term::ReadKey::ReadMode('noecho');
135 }
136 my $input_bytes = $self->term_ui->get_reply(
137 prompt => $encode->($prompt),
138 0 ▶ allow => $check || sub { length $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 ▶ (defined $default
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 0 0 0
Condition0%0/3
$check || sub { length $_[0]; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$check || sub { length $_[0]; }
$check sub { length $_[0]; }
Truth table0%0/3
A || B
A= $checkB= sub { length $_[0]; }
ABresult
1X1
011
000
140 ? (default => $encode->($default))
141 : ()
142 ),
143 );
144 0 ▶ if ($arg->{noecho}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"noecho"}) 0 0 0
145 0 Term::ReadKey::ReadMode('normal');
146 # The \n ending user input disappears under noecho; this ensures
147 # the next output ends up on the next line.
148 0 print "\n";
149 }
150  
151 0 ▶ my $input = $decode->($input_bytes) // q{};
Condition0%0/2
&$decode($input_bytes) // ""
l!l
00
MC/DC0%0/1
&$decode($input_bytes) // ""
&$decode($input_bytes)
Truth table0%0/2
A
A= &$decode($input_bytes)
Aresult
11
00
152 0 chomp $input;
153  
154 0 return $input;
155 }
156  
157 0 ▶sub prompt_yn {
Subroutine prompt_yn: not called CC=5 SCAR=34.0
Pod undocumented
158 0 my ($self, $prompt, $arg) = @_;
159 0 ▶ $arg ||= {};
Condition0%0/2
$arg ||= {}
l!l
00
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
Aresult
11
00
160 0 my $default = $arg->{default};
161  
162 0 ▶ if (! $self->_isa_tty) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isa_tty) 0 0 0
163 0 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 0 0 0
164 0 return OneZero->coerce($default);
165 }
166  
167 $self->logger->log_fatal(
168 0 "want interactive input, but terminal doesn't appear interactive"
169 );
170 }
171  
172 0 ▶ my $input = $self->term_ui->ask_yn(
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 0 0 0
173 prompt => $prompt,
174 (defined $default ? (default => OneZero->coerce($default)) : ()),
175 );
176  
177 0 return $input;
178 }
179  
180 0 ▶sub _isa_tty {
Subroutine _isa_tty: not called CC=4 SCAR=30.0
181 0 ▶ my $isa_tty = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT));
Condition0%0/9
-t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT))
!ll&&!rl&&r
000
-t STDOUT or not -f STDOUT || -c STDOUT
l!l&&r!l&&!r
000
-f STDOUT || -c STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/4
-t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT))
-t STDIN -t STDOUT -f STDOUT -c STDOUT
Truth table0%0/5
A && B or not(C || D)
A= -t STDINB= -t STDOUTC= -f STDOUTD= -c STDOUT
ABCDresult
0XXX0
101X0
10010
11XX1
10001
182 0 return $isa_tty;
183 }
184  
185 0 ▶sub prompt_any_key {
Subroutine prompt_any_key: not called CC=3 SCAR=24.8
Pod undocumented
186 0 my ($self, $prompt) = @_;
187 0 ▶ $prompt ||= 'press any key to continue';
Condition0%0/2
$prompt ||= "press any key to continue"
l!l
00
MC/DC0%0/1
$prompt ||= "press any key to continue"
$prompt
Truth table0%0/2
A
A= $prompt
Aresult
11
00
188  
189 0 my $isa_tty = $self->_isa_tty;
190  
191 0 ▶ if ($isa_tty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_tty) 0 0 0
192 0 local $| = 1;
193 0 print $prompt;
194  
195 0 require Term::ReadKey;
196 0 Term::ReadKey::ReadMode('cbreak');
197 0 Term::ReadKey::ReadKey(0);
198 0 Term::ReadKey::ReadMode('normal');
199 0 print "\n";
200 }
201 }
202  
203 4 with 'Dist::Zilla::Role::Chrome';
204  
205 4 __PACKAGE__->meta->make_immutable;
206 4 1;
207  
208 __END__
« blib/lib/Dist/Zilla/Role/BuildPL.pm ↑ Summary blib/lib/Dist/Zilla/App/Command/new.pm »