blib/lib/Pinto/Chrome/Term.pm

filter: Statementstmt 76.9% 87 / 113 Branchbran 45.1% 28 / 62 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 82.6% 19 / 23 Podpod 0.0% 0 / 9 totaltotal 63.8% 141 / 221 CC 37 SCAR 22.8
CC
37
Cov
65%
_build_stdout
42.0
edit
20.0
show_progress
12.0
CRAP 9.8
« blib/lib/Pinto/Action/Verify.pm ↑ Summary blib/lib/Pinto/Editor.pm »
1 # ABSTRACT: Interface for terminal-based interaction
2  
3 package Pinto::Chrome::Term;
4  
5 63 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 63 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use MooseX::Types::Moose qw(Bool ArrayRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use MooseX::MarkAsMethods ( autoclean => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 63 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 63 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 63 ▶use Pinto::Editor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 63 ▶use Pinto::Types qw(Io ANSIColorPalette);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 63 ▶use Pinto::Util qw(user_palette itis throw is_interactive);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #-----------------------------------------------------------------------------
18  
19 63 our $VERSION = '0.14'; # VERSION
20  
21 #-----------------------------------------------------------------------------
22  
23 63 extends qw( Pinto::Chrome );
24  
25 #-----------------------------------------------------------------------------
26  
27 has color => (
28 is => 'ro',
29 isa => Bool,
30 226 ▶ default => sub { !$ENV{PINTO_NO_COLOR} },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 63 );
32  
33 has palette => (
34 is => 'ro',
35 isa => ANSIColorPalette,
36 6 ▶ default => sub { user_palette() },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 63 lazy => 1,
38 );
39  
40 63 has stdout => (
41 is => 'ro',
42 isa => Io,
43 builder => '_build_stdout',
44 coerce => 1,
45 lazy => 1,
46 );
47  
48 has stderr => (
49 is => 'ro',
50 isa => Io,
51 0 ▶ default => sub { [ fileno(*STDERR), '>' ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 63 coerce => 1,
53 lazy => 1,
54 );
55  
56 63 has has_made_progress => (
57 is => 'rw',
58 isa => Bool,
59 default => 0,
60 );
61  
62 #-----------------------------------------------------------------------------
63  
64 0 ▶sub _build_stdout {
Subroutine _build_stdout: not called CC=6 SCAR=37.4
65 0 my ($self) = @_;
66  
67 0 ▶ my $pager = $ENV{PINTO_PAGER} || $ENV{PAGER};
Condition0%0/3
$ENV{'PINTO_PAGER'} || $ENV{'PAGER'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PINTO_PAGER'} || $ENV{'PAGER'}
$ENV{'PINTO_PAGER'} $ENV{'PAGER'}
Truth table0%0/3
A || B
A= $ENV{'PINTO_PAGER'}B= $ENV{'PAGER'}
ABresult
1X1
011
000
68 0 my $stdout = [ fileno(*STDOUT), '>' ];
69  
70 0 ▶ return $stdout if not -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless -t STDOUT 0 0 0
71 0 ▶ return $stdout if not $pager;
Branch: 1 branch
BranchTrueFalseTotal
unless $pager 0 0 0
72  
73 my @pager_options = $ENV{PINTO_PAGER_OPTIONS} ?
74 0 ▶ ( $ENV{PINTO_PAGER_OPTIONS} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PINTO_PAGER_OPTIONS'} ? : 0 0 0
75  
76 0 ▶ open my $pager_fh, q<|->, $pager, @pager_options
Branch: 1 branch
BranchTrueFalseTotal
open my $pager_fh, "|-", $pager, @pager_options or throw("Failed to open pipe to pager ${pager}: $!") 0 0 0
77 or throw "Failed to open pipe to pager $pager: $!";
78  
79  
80 0 my $io = bless $pager_fh, 'IO::Handle'; # HACK!
81 0 $io->autoflush(1);
82  
83 0 return $io;
84 }
85  
86 #------------------------------------------------------------------------------
87  
88 8 ▶sub show {
Subroutine show: called CC=4 SCAR=13.9
Pod undocumented
89 8 my ( $self, $msg, $opts ) = @_;
90  
91 8 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
44
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
92  
93 8 $msg = $self->colorize( $msg, $opts->{color} );
94  
95 8 ▶ $msg .= "\n" unless $opts->{no_newline};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"no_newline"} 7 1 8
96  
97 8 ▶ print { $self->stdout } $msg or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->stdout;} $msg or $!->croak 8 0 8
98  
99 8 return $self;
100 }
101  
102 #-----------------------------------------------------------------------------
103  
104 236 ▶sub diag {
Subroutine diag: called CC=8 SCAR=21.4
Pod undocumented
105 236 my ( $self, $msg, $opts ) = @_;
106  
107 236 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
84152
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
108  
109 236 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 236 236
110  
111 236 ▶ $msg = $msg->() if ref $msg eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg eq "CODE" 0 236 236
112  
113 236 ▶ if ( itis( $msg, 'Pinto::Exception' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (itis($msg, "Pinto::Exception")) 74 162 236
114  
115 # Show full stack trace if we are debugging
116 74 ▶ $msg = $ENV{PINTO_DEBUG} ? $msg->as_string : $msg->message;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PINTO_DEBUG'} ? : 0 74 74
117 }
118  
119 236 chomp $msg;
120 236 $msg = $self->colorize( $msg, $opts->{color} );
121 236 ▶ $msg .= "\n" unless $opts->{no_newline};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"no_newline"} 236 0 236
122  
123 236 ▶ print { $self->stderr } $msg or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->stderr;} $msg or $!->croak 236 0 236
124 }
125  
126 #-----------------------------------------------------------------------------
127  
128 0 ▶sub show_progress {
Subroutine show_progress: not called CC=3 SCAR=24.8
Pod undocumented
129 0 my ($self) = @_;
130  
131 0 ▶ return if not $self->should_render_progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_render_progress 0 0 0
132  
133 0 $self->stderr->autoflush; # Make sure pipes are hot
134  
135 0 ▶ print { $self->stderr } '.' or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->stderr;} "." or $!->croak 0 0 0
136  
137 0 $self->has_made_progress(1);
138 }
139  
140 #-----------------------------------------------------------------------------
141  
142 59 ▶sub progress_done {
Subroutine progress_done: called CC=4 SCAR=23.2
Pod undocumented
143 59 my ($self) = @_;
144  
145 59 ▶ return unless $self->has_made_progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_made_progress 59 0 59
146 0 ▶ return unless $self->should_render_progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->should_render_progress 0 0 0
147  
148 0 ▶ print { $self->stderr } "\n" or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$self->stderr;} "\n" or $!->croak 0 0 0
149 }
150  
151 #-----------------------------------------------------------------------------
152  
153 3 ▶sub should_render_progress {
Subroutine should_render_progress: called CC=4 SCAR=14.1
Pod undocumented
154 3 my ($self) = @_;
155  
156 3 ▶ return 0 if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 2 3
157 2 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 1 1 2
158 1 ▶ return 0 if not is_interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless is_interactive() 1 0 1
159 0 return 1;
160 }
161  
162 #-----------------------------------------------------------------------------
163  
164 0 ▶sub edit {
Subroutine edit: not called CC=4 SCAR=30.0
Pod undocumented
165 0 my ( $self, $document ) = @_;
166  
167 0 ▶ local $ENV{VISUAL} = $self->find_editor
Branch: 1 branch
BranchTrueFalseTotal
local $ENV{"VISUAL"} = $self->find_editor or throw("Unable to find an editor. Please set PINTO_EDITOR") 0 0 0
168 or throw 'Unable to find an editor. Please set PINTO_EDITOR';
169  
170 # If this command is reading input from a pipe or file, then
171 # STDIN will not be connected to a terminal. This causes vim
172 # and emacs to behave oddly (or even segfault). After searching
173 # the internets, this seems to a portable way to reconnect STDIN
174 # to the actual terminal. I haven't actually tried it on Windows.
175 # I'm not sure if/how I should be localizing STDIN here.
176  
177 0 ▶ my $term = ( $^O eq 'MSWin32' ) ? 'CON' : '/dev/tty';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
178 0 ▶ open( STDIN, '<', $term ) or throw $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $term or throw($!) 0 0 0
179  
180 0 return Pinto::Editor->edit( document => $document );
181 }
182  
183 #-----------------------------------------------------------------------------
184  
185 249 ▶sub colorize {
Subroutine colorize: called CC=4 SCAR=13.9
Pod undocumented
186 249 my ( $self, $string, $color_number ) = @_;
187  
188 249 ▶ return '' if not $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 249 249
189 249 ▶ return $string if not defined $color_number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color_number 157 92 249
190 92 ▶ return $string if not $self->color;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->color 87 5 92
191  
192 5 my $color = $self->get_color($color_number);
193  
194 5 return $color . $string . Term::ANSIColor::color('reset');
195 }
196  
197 #-----------------------------------------------------------------------------
198  
199 5 ▶sub get_color {
Subroutine get_color: called CC=3 SCAR=11.3
Pod undocumented
200 5 my ( $self, $color_number ) = @_;
201  
202 5 ▶ return '' if not defined $color_number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color_number 0 5 5
203  
204 5 my $color = $self->palette->[$color_number];
205  
206 5 ▶ throw "Invalid color number: $color_number" if not defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 5 5
207  
208 5 return Term::ANSIColor::color($color);
209 }
210  
211 #-----------------------------------------------------------------------------
212  
213 4 ▶sub find_editor {
Subroutine find_editor: called CC=5 SCAR=16.1
Pod undocumented
214 4 my ($self) = @_;
215  
216 # Try unsing environment variables first
217 4 for my $env_var (qw(PINTO_EDITOR VISUAL EDITOR)) {
218 9 ▶ return $ENV{$env_var} if $ENV{$env_var};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{$env_var} 3 6 9
219 }
220  
221 # Then try typical editor commands
222 1 for my $cmd (qw(nano pico vi)) {
223 3 my $found_cmd = which($cmd);
224 3 ▶ return $found_cmd if $found_cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $found_cmd 0 3 3
225 }
226  
227 1 return;
228 }
229  
230 #-----------------------------------------------------------------------------
231  
232 63 my %color_map = ( warning => 1, error => 2 );
233 63 ▶while ( my ( $level, $color ) = each %color_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %color_map) 126 63 189
234 89 ▶ around $level => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
235 89 my ( $orig, $self, $msg, $opts ) = @_;
236 89 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
089
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
237 89 $opts->{color} = $color;
238 89 return $self->$orig( $msg, $opts );
239 126 };
240 }
241  
242 #-----------------------------------------------------------------------------
243  
244 63 __PACKAGE__->meta->make_immutable;
245  
246 #-----------------------------------------------------------------------------
247 63 1;
248  
249 __END__
« blib/lib/Pinto/Action/Verify.pm ↑ Summary blib/lib/Pinto/Editor.pm »