File Coverage

blib/lib/App/GitHooks/Terminal.pm
Criterion Covered Total %
statement 34 37 91.8
branch 10 14 71.4
condition 3 8 37.5
subroutine 10 10 100.0
pod 5 5 100.0
total 62 74 83.7


line stmt bran cond sub pod time code
1             package App::GitHooks::Terminal;
2              
3 37     37   18184 use strict;
  37         55  
  37         1256  
4 37     37   181 use warnings;
  37         67  
  37         1183  
5              
6             # External dependencies.
7 37     37   158 use Carp;
  37         53  
  37         2265  
8 37     37   17763 use Term::Encoding ();
  37         23630  
  37         701  
9 37     37   17629 use Term::ReadKey ();
  37         67469  
  37         14272  
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.9.0
20              
21             =cut
22              
23             our $VERSION = '1.9.0';
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 52     52 1 22955 my ( $class ) = @_;
50              
51 52         652 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 23     23 1 79 my ( $self ) = @_;
69              
70 23   66     202 $self->{'encoding'} //= Term::Encoding::term_encoding();
71              
72 23         23427 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     24 1 81 my ( $self ) = @_;
87              
88 24 50 33     120 if ( $self->is_interactive() && !defined( $self->{'width'} ) )
89             {
90 0         0 my $output_width = (Term::ReadKey::GetTerminalSize())[0];
91 0   0     0 $output_width //= 80;
92 0         0 $self->{'width'} = $output_width;
93             }
94              
95 24         131 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     43 1 283 my ( $self ) = @_;
110              
111 43 100       181 if ( !defined( $self->{'is_interactive'} ) )
112             {
113 17 50       109 $self->{'is_interactive'} = -t STDOUT ? 1 : 0; ## no critic (InputOutput::ProhibitInteractiveTest)
114             }
115              
116 43         379 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 49     49 1 134 my ( $self, $value ) = @_;
137              
138 49 100       244 if ( defined( $value ) )
    100          
139             {
140 17 50       106 croak "Invalid override value"
141             if $value !~ /^(?:0|1)$/;
142              
143 17         83 $self->{'is_utf8'} = $value;
144             }
145             elsif ( !defined( $self->{'is_utf8'} ) )
146             {
147 3         10 my $terminal_encoding = $self->get_encoding();
148 3 50       16 $self->{'is_utf8'} = $terminal_encoding =~ /^utf-?8$/xi ? 1 : 0;
149             }
150              
151 49         190 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-2017 Guillaume Aubert.
202              
203             This code is free software; you can redistribute it and/or modify it under the
204             same terms as Perl 5 itself.
205              
206             This program is distributed in the hope that it will be useful, but WITHOUT ANY
207             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
208             PARTICULAR PURPOSE. See the LICENSE file for more details.
209              
210             =cut
211              
212             1;