blib/lib/App/GitHooks/Terminal.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 71.4% 10 / 14 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 79.0% 64 / 81 CC 11 SCAR 13.3
CC
11
Cov
80%
get_width
7.7
is_utf8
5.1
is_interactive
3.0
CRAP 3.8
« blib/lib/App/GitHooks/CommitMessage.pm ↑ Summary blib/lib/App/GitHooks/Plugin.pm »
1 package App::GitHooks::Terminal;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # External dependencies.
7 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Term::Encoding ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 =head1 NAME
13  
14 App::GitHooks::Terminal - Information about the current terminal in which App::GitHook is running.
15  
16  
17 =head1 VERSION
18  
19 Version 1.7.3
20  
21 =cut
22  
23 35 our $VERSION = '1.7.3';
24  
25  
26 =head1 SYNOPSIS
27  
28 use App::GitHooks::Terminal;
29  
30 my $terminal = App::GitHooks::Terminal->new();
31 my $get_encoding = $terminal->get_encoding();
32 my $get_width = $terminal->get_width();
33 my $is_interactive = $terminal->is_interactive();
34 my $is_utf8 = $terminal->is_utf8();
35  
36  
37 =head1 METHODS
38  
39 =head2 new()
40  
41 Return a new C<App::GitHooks::Terminal> object.
42  
43 my $terminal = App::GitHooks::Terminal->new();
44  
45 =cut
46  
47 sub new
48 {
49 48 ▶ my ( $class ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50  
51 48 return bless(
52 {},
53 $class,
54 );
55 }
56  
57  
58 =head2 get_encoding()
59  
60 Determine the current terminal's encoding.
61  
62 my $get_encoding = $terminal->get_encoding();
63  
64 =cut
65  
66 sub get_encoding
67 {
68 21 ▶ my ( $self ) = @_;
Subroutine get_encoding: called CC=2 SCAR=7.0
Pod documented
69  
70 21 ▶ $self->{'encoding'} //= Term::Encoding::term_encoding();
Condition66%2/3
$self->{"encoding"} //= Term::Encoding::term_encoding()
l!l&&r!l&&!r
1200
MC/DC0%0/2
$self->{"encoding"} //= Term::Encoding::term_encoding()
$self->{"encoding"} Term::Encoding::term_encoding()
Truth table66%2/3
A //= B
A= $self->{"encoding"}B= Term::Encoding::term_encoding()
ABresult
1X1
011
000
71  
72 21 return $self->{'encoding'};
73 }
74  
75  
76 =head2 get_width()
77  
78 Get the width (in the number of characters) of the current terminal.
79  
80 my $get_width = $terminal->get_width();
81  
82 =cut
83  
84 sub get_width
85 {
86 24 ▶ my ( $self ) = @_;
Subroutine get_width: called CC=4 SCAR=20.4
Pod documented
87  
88 24 ▶ if ( $self->is_interactive() && !defined( $self->{'width'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_interactive and not defined $self->{"width"}) 0 24 24
Condition33%1/3
$self->is_interactive and not defined $self->{"width"}
!ll&&!rl&&r
2400
MC/DC0%0/2
$self->is_interactive and not defined $self->{"width"}
$self->is_interactive not defined $self->{"width"}
Truth table33%1/3
A and not(B)
A= $self->is_interactiveB= not defined $self->{"width"}
ABresult
0X0
100
111
89 {
90 0 my $output_width = (Term::ReadKey::GetTerminalSize())[0];
91 0 ▶ $output_width //= 80;
Condition0%0/2
$output_width //= 80
l!l
00
MC/DC0%0/1
$output_width //= 80
$output_width
Truth table0%0/2
A
A= $output_width
Aresult
11
00
92 0 $self->{'width'} = $output_width;
93 }
94  
95 24 return $self->{'width'};
96 }
97  
98  
99 =head2 is_interactive()
100  
101 Determine whether the current terminal is interactive or not.
102  
103 my $is_interactive = $terminal->is_interactive();
104  
105 =cut
106  
107 sub is_interactive
108 {
109 43 ▶ my ( $self ) = @_;
Subroutine is_interactive: called CC=3 SCAR=11.0
Pod documented
110  
111 43 ▶ if ( !defined( $self->{'is_interactive'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"is_interactive"}) 17 26 43
112 {
113 17 ▶ $self->{'is_interactive'} = -t STDOUT ? 1 : 0; ## no critic (InputOutput::ProhibitInteractiveTest)
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 17 17
114 }
115  
116 43 return $self->{'is_interactive'};
117 }
118  
119  
120 =head2 is_utf8()
121  
122 Determine if the current terminal supports utf-8.
123  
124 my $is_utf8 = $terminal->is_utf8();
125  
126 Optionally, you can override the utf-8 support by passing an extra boolean
127 argument:
128  
129 $terminal->is_utf8(1); # Force utf-8 output.
130 $terminal->is_utf8(0); # Force non-utf-8 output.
131  
132 =cut
133  
134 sub is_utf8
135 {
136 48 ▶ my ( $self, $value ) = @_;
Subroutine is_utf8: called CC=5 SCAR=16.2
Pod documented
137  
138 48 ▶ if ( defined( $value ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 17 31 48
elsif (not defined $self->{'is_utf8'}) { } 2 29 31
139 {
140 17 ▶ croak "Invalid override value"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(?:0|1)$/ 0 17 17
141 if $value !~ /^(?:0|1)$/;
142  
143 17 $self->{'is_utf8'} = $value;
144 }
145 elsif ( !defined( $self->{'is_utf8'} ) )
146 {
147 2 my $terminal_encoding = $self->get_encoding();
148 2 ▶ $self->{'is_utf8'} = $terminal_encoding =~ /^utf-?8$/xi ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$terminal_encoding =~ /^utf-?8$/xi ? : 0 2 2
149 }
150  
151 48 return $self->{'is_utf8'};
152 }
153  
154  
155 =head1 BUGS
156  
157 Please report any bugs or feature requests through the web interface at
158 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
159 I will be notified, and then you'll automatically be notified of progress on
160 your bug as I make changes.
161  
162  
163 =head1 SUPPORT
164  
165 You can find documentation for this module with the perldoc command.
166  
167 perldoc App::GitHooks::Terminal
168  
169  
170 You can also look for information at:
171  
172 =over
173  
174 =item * GitHub's request tracker
175  
176 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
177  
178 =item * AnnoCPAN: Annotated CPAN documentation
179  
180 L<http://annocpan.org/dist/app-githooks>
181  
182 =item * CPAN Ratings
183  
184 L<http://cpanratings.perl.org/d/app-githooks>
185  
186 =item * MetaCPAN
187  
188 L<https://metacpan.org/release/App-GitHooks>
189  
190 =back
191  
192  
193 =head1 AUTHOR
194  
195 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
196 C<< <aubertg at cpan.org> >>.
197  
198  
199 =head1 COPYRIGHT & LICENSE
200  
201 Copyright 2013-2015 Guillaume Aubert.
202  
203 This program is free software: you can redistribute it and/or modify it under
204 the terms of the GNU General Public License version 3 as published by the Free
205 Software Foundation.
206  
207 This program is distributed in the hope that it will be useful, but WITHOUT ANY
208 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
209 PARTICULAR PURPOSE. See the GNU General Public License for more details.
210  
211 You should have received a copy of the GNU General Public License along with
212 this program. If not, see http://www.gnu.org/licenses/
213  
214 =cut
215  
216 35 1;
« blib/lib/App/GitHooks/CommitMessage.pm ↑ Summary blib/lib/App/GitHooks/Plugin.pm »